Page 134 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 134

Unit 6: Binary Search Tree and AVL Trees





                      else                                                                      Notes
                  {
                          *y = temp; /*store this pointer as root */
                  if(temp->data > key)
                     temp = temp->lchild;
                  else
                     temp = temp->rchild;
                     }
                    }
                   return(NULL);
             }
             /* A function to delete the node whose data value is given */
             struct tnode *delete(struct tnode *p,int val)
               {
                 struct tnode *x, *y, *temp;
                 x = getptr(p,val,&y);
                 if( x == NULL)
                 {
                   printf(“The node does not exists\n”);
                   return(p);
                 }
                 else
                 {
                 /* this code is for deleting root node*/
                 if( x == p)
                   {
                     temp = x->lchild;
                     y = x->rchild;
                     p = temp;
                     while(temp->rchild != NULL)
                   temp = temp->rchild;
                   temp->rchild=y;
                   free(x);
                   return(p);
                 }
             /* this code is for deleting node having both children */
               if( x->lchild != NULL && x->rchild != NULL)
                  {
                    if(y->lchild == x)
                    {




                                           LOVELY PROFESSIONAL UNIVERSITY                                   129
   129   130   131   132   133   134   135   136   137   138   139