Page 136 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 136

Unit 6: Binary Search Tree and AVL Trees





                        return(p);                                                              Notes
                      }
                    /* this code is for deleting a node with no child*/
                    if(x->lchild == NULL && x->rchild == NULL)
                     {
                        if(y->lchild == x)
                           y->lchild = NULL ;
                        else
                           y->rchild = NULL;
                        free(x);
                        return(p);
                     }
                 }
             }
             /*an iterative function to print the binary tree in inorder*/
             void inorder1(struct tnode *p)
             {
              struct tnode *stack[100];
              int top;
              top = −1;
             if(p != NULL)
              {
                 top++;
                 stack[top] = p;
                 p = p->lchild;
                 while(top >= 0)
                    {
                       while ( p!= NULL)/* push the left child onto stack*/
                        {
                                    top++;
                              stack[top] =p;
                              p = p->lchild;
              }
                   p = stack[top];
                   top-;
                   printf(“%d\t”,p->data);
                   p = p->rchild;
                   if ( p != NULL) /* push right child*/
                      {
                          top++;
                      stack[top] = p;
                          p = p->lchild;




                                           LOVELY PROFESSIONAL UNIVERSITY                                   131
   131   132   133   134   135   136   137   138   139   140   141