c - Binary Search Tree Nothing Getting Printed -


i'm trying create unbalanced binary search tree given input sequence of (unsorted) integers.
approach has been recursively find right place each individual node , allocate memory , define data it.
i'm unable debug program despite having scrutinized properly,i can't seem identify problem. input follows:

11 15 6 4 8 5 3 1 10 13 2 11 

the expected output should have been post-order , in-order traversals,but strangely nothing getting printed(except newline i've given in between).

note: question closely linked bst related question asked,but approach entirely different , challenges i'm facing. hence think twice before going neck , closing down topic.

#include <stdio.h> #include <stdlib.h> #include <string.h> #define arrmax 100 /***pointer-based bst implementation, developed abhineet saxena***/   /***the data-type declaration***/ typedef struct node{ int data; struct node* parent; struct node* leftchild; struct node* rightchild; }node;  typedef struct tree {     node* root;     int size; }bstree;  /***method prototypes***/ /*method create tree*/ node* createtree(int[],int,int); void insert(node*,int);  node* createnode(int); void inorder(node* root); void postorder(node *root);  int main(void) {      bstree* bs_tree;     bs_tree=malloc(sizeof(bstree));     bs_tree->root=null;     bs_tree->size=0;     /****taking input****/     int num_elem,iterv;     scanf("%d\n",&num_elem);     int *arr=malloc(sizeof(int)*(num_elem));     for(iterv=0;iterv<num_elem;iterv++)     {         scanf("%d",&arr[iterv]);     }     bs_tree->root=createtree(arr,0,num_elem-1);     postorder(bs_tree->root);      printf("\n");     inorder(bs_tree->root);      return 0; } node* createtree(int marr[],int left,int right) {     int iterv;     node* root;     root=null; //  node** root_ptr;     //*root_ptr=root;     for(iterv=left;iterv<=right;iterv++)     {          insert(root,marr[iterv]);      }     return root; } node* createnode(int key) {         node* tree_node;         tree_node=malloc(sizeof(node));         //printf("used malloc here key: %d\n",key);         tree_node->data=key;         tree_node->leftchild=null;         tree_node->rightchild=null;         tree_node->parent=null;         return tree_node; } void insert(node* root,int key) {         if(root==null)     {         root=createnode(key);         //return root;     }     else if(root->leftchild!=null && root->rightchild!=null)         {             if(key<root->data)                          insert(root->leftchild,key);                         else                         insert(root->rightchild,key);         }         else if(root->leftchild!=null && root->rightchild==null)         {             if(key>root->data)             {                 node* tnode=createnode(key);                 root->rightchild=tnode;                 tnode->parent=root;                 return;             }             else if(key<root->data)                 insert(root->leftchild,key);          }         else if(root->leftchild==null && root->rightchild!=null)         {             if(key<root->data)             {                 node* tnode=createnode(key);                 root->leftchild=tnode;                 tnode->parent=root;                 return;             }             else if(key>root->data)                 insert(root->rightchild,key);         }         else         {             if(key<root->data)             {                 node* tnode=createnode(key);                 root->leftchild=tnode;                 tnode->parent=root;                 return;             }             else if(key>root->data)             {                 node* tnode=createnode(key);                 root->rightchild=tnode;                 tnode->parent=root;                 return;             }         }  }  void inorder(node* bst_tree) {     if(bst_tree!=null)     {         inorder(bst_tree->leftchild);         printf("%d ",bst_tree->data);         inorder(bst_tree->rightchild);     }     else         return; } void postorder(node* bst_tree) {     if(bst_tree!=null)     {         postorder(bst_tree->leftchild);         postorder(bst_tree->rightchild);         printf("%d ",bst_tree->data);     }     else         return; } 

your problem one: in createtree, set root null , call

insert(root, marr[iterv]); 

...and magically expect root non-null after it. need change calling convention:

insert(&root, marr[iterv]); 

...and change signature of insert void insert(node**,int);

then, in insert function, instead of root use *root, , instead of root->something use (*root)->something. tested program these changes , works.

an additional nitpick: integer ranges should left-inclusive , right-exclusive should call createtree in way:

bs_tree->root=createtree(arr,0,num_elem); 

and have loop:

for(iterv=left;iterv<right;iterv++) 

then length of range right-left more convenient.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -