data structures - BST Insertion issue -
my code bst insertion getting passed 3 test cases , failing other 3 test cases, test case hidden can't figure out i'm going wrong logic. please check , advise me other different test case logic not pass. here's code:
/* node defined typedef struct node { int data; node * left; node * right; }node; */ node * insert(node * root, int value) { node* n; n=root; if(n!=null) {while (true ) { if(n->data > value && n->left!=null){n=n->left;} else if(n->data < value && n->right!=null){n=root->right;} else { node* a=new node(); a->data=value; if(n->data > value ){ n->left=a;break; } else{ n->right=a;break; } } } } else {node* a=new node(); a->data=value; root=a; } return root; }
i write
node* insert(node * root, int value) { node* n= root; node* a=new node(); if( == null ) { cout<<" no memory \n " ; exit(0); } a->data=value; a->left=a->right=null; if(n!=null) {while (true ) { if(n->data > value ) { if( n->left!=null ) {n=n->left;} else { n->left=a; return root; } } else if(n->data < value ) { if ( n->right!=null ) {n=n->right;} else { n->right = a; return root; } } } } else { return a; } }
Comments
Post a Comment