Page 123 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 123

Advanced Data Structure and Algorithms




                    Notes          When temp1 becomes nil, the new node is inserted as a left child of the node pointed to by temp2
                                   if data value of the node pointed to by temp2 is greater than data value supplied as parameter.
                                   Otherwise the new node is inserted as a right child of node pointed to by temp2. Therefore the
                                   insert procedure is
                                   void insert(tnode *p, int val)
                                   {
                                      tnode *temp1, *temp2;
                                      if (p == NULL)
                                      {
                                          p = new(tnode);
                                          p->data = val;
                                          p->1child = NULL;
                                          p->rchild = NULL;
                                      }
                                      else
                                      {
                                          temp1 = p;
                                          while(temp1 != NULL)
                                          {
                                             temp2 = temp1;
                                             if(temp1->data > val)
                                             temp1 = temp1->1eft;
                                             else
                                             temp1 = temp1->right;
                                          }
                                          if(temp2->data > val)
                                          {
                                             temp2->left = new(tnode);
                                             temp2 = temp2->left;
                                             temp2->data = val;
                                             temp2->left = NULL;
                                             temp2->right= NULL;
                                          }
                                          else
                                          {
                                             temp2->right = new(tnode);
                                             temp2 = temp2->right;
                                             temp2->data = val;
                                             temp2->left = NULL;
                                             temp2->right = NULL;
                                          }
                                      }
                                   }





          118                              LOVELY PROFESSIONAL UNIVERSITY
   118   119   120   121   122   123   124   125   126   127   128