Advertisement
hamaXD

LAST CPT.

Nov 25th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct treenode {
  5.     struct treenode *left;
  6.     struct treenode *right;
  7.     int data;
  8. };
  9. typedef struct treenode TREENODE;
  10. typedef TREENODE *TREE;
  11.  
  12. void insert_node(TREE *tp, int value);
  13. void preorder_print(TREE t);
  14. int is_empty_tree ( TREE t );
  15. int root_value(TREE t);
  16. int is_perfect_tree(TREE t);
  17. int size(TREE t);
  18. int is_perfect_level(TREE t);
  19. int main(){
  20.     TREE t = NULL;
  21.     int i = 0, tmp;
  22.     do{
  23.         printf("N%02d: ", i + 1);
  24.         scanf(" %d", &tmp);
  25.         if(tmp==0){
  26.             break;
  27.         }
  28.         insert_node(&t,tmp);
  29.         i++;
  30.     }while(tmp > 0);
  31.     preorder_print(t);
  32.     printf(" = %s\n", is_perfect_tree(t) ? "Yes" : "No");
  33.  
  34.     return 0;
  35. }
  36. void insert_node(TREE *tp, int value) {
  37.     if (*tp == NULL) {
  38.     //  printf("tp == null\n");
  39.         *tp = malloc(sizeof(TREENODE));
  40.         (*tp)->data = value;  
  41.         (*tp)->left = NULL;
  42.         (*tp)->right = NULL;
  43.     }else if (value < (*tp)->data ){
  44.         printf(" <<<\n");
  45.         insert_node(&((*tp)->left), value);
  46.     }else if (value > (*tp)->data ){
  47.         printf(" >>>\n");
  48.         insert_node(&((*tp)->right), value);
  49.     }else{
  50.         printf("duplicate node\n");
  51.     }
  52. }
  53.  
  54. int is_empty_tree ( TREE t ){
  55.     if (t == NULL) {
  56.     //  printf("in empty null\n");
  57.         return 1;
  58.     }else{
  59.     //  printf("NO in empty null\n");
  60.         return 0;
  61.     }
  62. }
  63. int root_value(TREE t){
  64.     if (!is_empty_tree){
  65.         fprintf(stderr, "Tree is empty \n");
  66.         return -1;
  67.     }else{
  68.         return t->data;
  69.     }
  70. }
  71.  
  72. TREE ls_tree(TREE t){
  73. //  printf("left\n");
  74.     return t->left;
  75. }
  76. TREE rs_tree(TREE t){
  77. //  printf("right\n");
  78.     return t->right;
  79. }
  80.  
  81.  
  82. void preorder_print(TREE t){
  83.     if (!is_empty_tree(t)) {
  84.        
  85.         printf("%d\n", root_value(t));
  86.         preorder_print(ls_tree(t));  
  87.        
  88.         preorder_print(rs_tree(t));
  89.     }
  90. }
  91.  
  92. int perfect_level(TREE t){
  93.     if(t==NULL)
  94.         return 1;
  95.     else if(t->left==NULL && t->right==NULL)
  96.         return 1;
  97.     else if(t->left==NULL || t->right==NULL)
  98.         return 0;
  99.     else
  100.         return is_perfect_tree(t->left)&&is_perfect_tree(t->right);
  101.     }
  102.  
  103. int is_perfect_tree(TREE t){
  104.     if(perfect_level(t)==1 && size(t->left)==size(t->right))
  105.         return 1;
  106.     else
  107.         return 0;  
  108. }
  109.  
  110. int size(TREE t){
  111.     if(t==NULL)
  112.         return 0;
  113.     else
  114.         return (1 + size(t->left) + size(t->right));
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement