DanikKUL

3.2

May 3rd, 2022 (edited)
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Осуществить нерекурсивный проход по бинарному дереву,
  5. // чтобы печатался сначала предок, а потом потомок.
  6.  
  7. struct tree{
  8.     int value;
  9.     struct tree *left;
  10.     struct tree *right;
  11. };
  12.  
  13. struct stack{
  14.     struct tree *node;
  15.     struct stack *next;
  16. };
  17.  
  18. void makeTree(struct tree* node){
  19.     node -> value = 100;
  20.     node -> left = (struct tree*) malloc(sizeof(struct tree));
  21.     node -> left -> value = 70;
  22.     node -> left -> left = (struct tree*) malloc(sizeof(struct tree));
  23.     node -> left -> left -> value = 50;
  24.     node -> left -> left -> left = (struct tree*) malloc(sizeof(struct tree));
  25.     node -> left -> left -> left -> value = 10;
  26.     node -> left -> left -> left -> right = (struct tree*) malloc(sizeof(struct tree));
  27.     node -> left -> left -> left -> right -> value = 40;
  28.     node -> left -> left -> right = (struct tree*) malloc(sizeof(struct tree));
  29.     node -> left -> left -> right -> value = 55;
  30.     node -> left -> left -> right -> right = (struct tree*) malloc(sizeof(struct tree));
  31.     node -> left -> left -> right -> right -> value = 60;
  32.     node -> left -> left -> right -> right -> left = (struct tree*) malloc(sizeof(struct tree));
  33.     node -> left -> left -> right -> right -> left -> value = 58;
  34.     node -> left -> left -> right -> right -> right= (struct tree*) malloc(sizeof(struct tree));
  35.     node -> left -> left -> right -> right -> right -> value = 65;
  36.     node -> left -> right = (struct tree*) malloc(sizeof(struct tree));
  37.     node -> left -> right -> value = 90;
  38.     node -> right = (struct tree*) malloc(sizeof(struct tree));
  39.     node -> right -> value = 150;
  40.     node -> right -> right = (struct tree*) malloc(sizeof(struct tree));
  41.     node -> right -> right -> value = 200;
  42.     node -> right -> right -> left = (struct tree*) malloc(sizeof(struct tree));
  43.     node -> right -> right -> left -> value = 170;
  44.     node -> right -> right -> right = (struct tree*) malloc(sizeof(struct tree));
  45.     node -> right -> right -> right -> value = 210;
  46. }
  47.  
  48. void showTree(struct tree* node){
  49.     if(!node){
  50.         printf("error\n");
  51.         exit(1);
  52.     }
  53.     int flag = 1;
  54.     struct stack *stackEl = (struct stack*) calloc(1, sizeof(struct stack));
  55.     stackEl -> node = node;
  56.     struct stack *stackPtr = NULL;
  57.     stackEl -> next = stackPtr;
  58.     printf("Node stores: %d\n", node -> value);
  59.     while (stackEl || node -> right){
  60.         do {
  61.             if (flag && node -> left){
  62.                 node = node -> left;
  63.             } else {
  64.                 if (node -> right){
  65.                     node = node -> right;
  66.                 }
  67.             }
  68.             flag = 1;
  69.             if (node -> left && node -> right){
  70.                 stackPtr = stackEl;
  71.                 stackEl = (struct stack*) malloc(sizeof(struct stack));
  72.                 stackEl -> node = node;
  73.                 stackEl -> next = stackPtr;
  74.             }
  75.             printf("Node stores: %d\n", node -> value);
  76.         } while (node -> left || node -> right);
  77.         if (stackEl){
  78.             node = stackEl -> node;
  79.             stackPtr = stackEl -> next;
  80.             free(stackEl);
  81.         }
  82.         stackEl = stackPtr;
  83.         if (node -> right){
  84.             flag = 0;
  85.         }
  86.     }
  87. }
  88.  
  89. void showTreeRec(struct tree* node, int depth){
  90.     if (node) {
  91.         showTreeRec(node -> right, depth + 1);
  92.     }
  93.     for (int i = 0; i < depth; i++) {
  94.         printf("      ");
  95.     }
  96.     if (node){
  97.         printf("%d\n", node -> value);
  98.     } else {
  99.         printf("#\n");
  100.     }
  101.     if (node){
  102.         showTreeRec(node -> left, depth + 1);
  103.     }
  104. }
  105.  
  106.  
  107. int main(){
  108.     struct tree *root = (struct tree*) malloc (sizeof(struct tree));
  109.     makeTree(root);
  110.     showTreeRec(root, 0);
  111.     showTree(root);
  112.     return 0;
  113. }
Add Comment
Please, Sign In to add comment