Advertisement
Guest User

c - binary tree

a guest
Dec 21st, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include <time.h>
  5. #include <string.h>
  6.  
  7. #ifndef DEBUG
  8. #define DEBUG(...) printf(__VA_ARGS__)
  9. #endif
  10.  
  11. #define MAX_TREE_STRING 100
  12. #define MAX_NODES 100
  13. // sirina i visina 2D polja koje se koristi za funkciju print_tree
  14. #define WIDTH 80
  15. #define HEIGHT 6
  16.  
  17. typedef struct node {
  18.     char val;
  19.     struct node *left;
  20.     struct node *right;
  21. } Node;
  22.  
  23. int broj_cvorova (Node *node) {  //broj cvorova u lijevom podstablu + broj cvorova u desnom podstablu + root
  24.    
  25.     if (node==NULL) return 0;
  26.    
  27.     return 1 + broj_cvorova(node->left) + broj_cvorova(node->right);       
  28. }
  29.  
  30.  
  31. char najveci_element(Node *node) {
  32.     if (node == NULL) return 0;
  33.     int left, right;
  34.    
  35.     left = najveci_element(node->left);
  36.     right = najveci_element(node->right);
  37.    
  38.     if (left > right) return node->val > left ? node->val : left;
  39.     else return node->val > right ? node->val : right;
  40. }
  41.  
  42.  
  43. char najmanji_element(Node *node) {
  44.     if (node == NULL) return 0;
  45.     int left, right;
  46.    
  47.     left = najveci_element(node->left);
  48.     right = najveci_element(node->right);
  49.    
  50.     if (left < right) return node->val > left ? node->val : left;
  51.     else return node->val < right ? node->val : right;
  52. }
  53.  
  54.  
  55. void zrcalno_stablo(Node *node) {
  56. }
  57.  
  58.  
  59. int identicna(Node *n1, Node *n2) {
  60. }
  61.  
  62. int sprint_tree(Node *tree, int is_left, int offset, int depth, char s[HEIGHT][WIDTH]) {
  63.     char b[HEIGHT];
  64.     int i, left, right, width = 5;
  65.  
  66.     if (!tree) return 0;
  67.     sprintf(b, "( %c )", tree->val);
  68.     left = sprint_tree(tree->left, 1, offset, depth+1, s);
  69.     right = sprint_tree(tree->right, 0, offset+left+width, depth+1, s);
  70.     for (i=0; i<width; i++)
  71.         s[depth][offset+left+i] = b[i];
  72.     if (depth) {
  73.         if (is_left) {
  74.             for (i=0; i<width+right; i++)
  75.                 s[depth-1][offset+left+width/2+i] = '-';
  76.         } else {
  77.             for (i=0; i<left+width; i++)
  78.                 s[depth-1][offset-width/2+i] = '-';
  79.         }
  80.         s[depth-1][offset+left+width/2] = '.';
  81.     }
  82.     return left+width+right;
  83. }
  84.  
  85. void ispisi_stablo(Node *root) {
  86.     char print_format[6], empty_line[WIDTH], s[HEIGHT][WIDTH];
  87.     int i;
  88.     sprintf(print_format, "%%%ds", WIDTH-1);
  89.     for (i=0; i<HEIGHT; i++)
  90.         sprintf(s[i], print_format, " ");
  91.  
  92.     sprint_tree(root, 0, 0, 0, s);
  93.  
  94.     sprintf(empty_line, print_format, " ");
  95.     for (i=0; i<HEIGHT; i++) {
  96.         if (strcmp(s[i],empty_line))
  97.             printf("%s\n", s[i]);
  98.     }
  99. }
  100.  
  101. Node *create_tree(char *tree_string) {
  102.     Node *queue[MAX_NODES];
  103.     Node *trenutni, *parent;
  104.     int tail, head, i;
  105.  
  106.     tail = head = -1;
  107.     trenutni = parent = NULL;
  108.  
  109.     queue[0] = NULL;
  110.     for(i=0; i<strlen(tree_string); i++) {
  111.         if (tree_string[i]!=';') {
  112.             if (tree_string[i]=='-') continue;
  113.             trenutni = malloc(sizeof(Node));
  114.             trenutni->val = tree_string[i];
  115.             trenutni->left = trenutni->right = NULL;
  116.  
  117.             queue[++tail] = trenutni;
  118.             if (parent && tree_string[i-1]==';') {
  119.                 parent->left = trenutni;
  120.             } else if (i>0) {
  121.                 parent->right = trenutni;
  122.             }
  123.         } else {
  124.             parent = queue[++head];
  125.         }
  126.     }
  127.     return queue[0];
  128. }
  129.  
  130. int main () {
  131.     Node *root=NULL, *root2=NULL;
  132.     int menu_choice;
  133.     char c, tree_string[MAX_TREE_STRING];
  134.  
  135.     setbuf(stdout, NULL);
  136.     do {
  137.         menu_choice = 0;
  138.         DEBUG("\n1 Kreiraj stablo\n2 Ispisi stablo\n3 Najveci element\n4 Najmanji element\n5 Broj cvorova\n6 Zrcalno stablo\n7 Identicno\n8 Izlaz\n");
  139.         scanf("%d", &menu_choice);
  140.         switch (menu_choice) {
  141.             case 1:
  142.                 DEBUG("Unesite stablo kao niz alfanumerickih znakova odvojenih sa znakom ;\n");
  143.                 scanf(" %s", tree_string);
  144.                 root = create_tree(tree_string);
  145.                 break;
  146.             case 2:
  147.                 ispisi_stablo(root);
  148.                 break;
  149.             case 3:
  150.                 printf("%c\n", najveci_element(root));
  151.                 break;
  152.             case 4:
  153.                 printf("%c\n", najmanji_element(root));
  154.                 break;
  155.             case 5:
  156.                 printf("%d\n", broj_cvorova(root));
  157.                 break;
  158.             case 6:
  159.                 zrcalno_stablo(root);
  160.                 break;
  161.             case 7:
  162.                 DEBUG("Unesite stablo za usporedbu kao niz alfanumerickih znakova odvojenih sa znakom ;\n");
  163.                 scanf(" %s", tree_string);
  164.                 root2 = create_tree(tree_string);
  165.                 printf("%c\n", identicna(root, root2) ? 'Y' : 'N');
  166.                 break;
  167.             case 8:
  168.                 break;
  169.             default:
  170.                 while((c = getchar()) != '\n' && c != EOF);
  171.         }
  172.     } while(menu_choice!=8);
  173.     return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement