Advertisement
hamaXD

NO complete CPT LAB9

Nov 19th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 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 main(){
  17.     TREE t = NULL;
  18.     int i = 0, tmp;
  19.     do{
  20.         printf("N%02d: ", i + 1);
  21.         scanf(" %d", &tmp);
  22.         insert_node(&t,tmp);
  23.         i++;
  24.     }while(tmp > 0);
  25.     preorder_print(t);
  26.     return 0;
  27. }
  28. void insert_node(TREE *tp, int value) {
  29.     if (*tp == NULL) {
  30.         printf("tp == null\n");
  31.         *tp = malloc(sizeof(TREENODE));
  32.         (*tp)->data = value;  
  33.         (*tp)->left = NULL;
  34.         (*tp)->right = NULL;
  35.     }else if (value < (*tp)->data ){
  36.         printf("tp <<<\n");
  37.         insert_node(&((*tp)->left), value);
  38.     }else if (value > (*tp)->data ){
  39.         printf("tp >>>\n");
  40.         insert_node(&((*tp)->right), value);
  41.     }else{
  42.         printf("duplicate node\n");
  43.     }
  44. }
  45.  
  46. int is_empty_tree ( TREE t ){
  47.     if (t == NULL) {
  48.         printf("in empty null\n");
  49.         return 1;
  50.     }else{
  51.         printf("NO in empty null\n");
  52.         return 0;
  53.     }
  54. }
  55. int root_value(TREE t){
  56.     if (!is_empty_tree){
  57.         fprintf(stderr, "Tree is empty \n");
  58.         return -1;
  59.     }else{
  60.         return t->data;
  61.     }
  62. }
  63.  
  64. TREE ls_tree(TREE t){
  65.     return t->left;
  66. }
  67. TREE rs_tree(TREE t){
  68.     return t->right;
  69. }
  70.  
  71.  
  72. void preorder_print(TREE t){
  73.     if (!is_empty_tree(t)) {
  74.         printf("%d\n", root_value(t));
  75.         preorder_print(ls_tree(t));    
  76.         preorder_print(rs_tree(t));
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement