Page 116 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 116

Unit 5: Trees





              tnode *temp;                                                                      Notes
              if(p != NULL)
              {
                  swaptree(p->1child);
                  swaptree(p->rchild);
                  temp = p->1child;
                  p->1child = p->rchild;
                  p->rchild = temp;
              }
          }
          The following function checks whether the two binary trees are equal or not.
          boolean equal(tnode *p1, tnode *p2)
          {
              boolean ans;
              if((p1 == NULL) && (p2 == NULL))
                  ans = true;
              else
              if(((p1==NULL)&&(p2!=NULL))||((p1!=NULL)&&(p2==NULL)))   ans = false;
              else
                  while((p1 != NULL) && (p2 != NULL))
                  {
                         if(p1 != NULL) && (p2 != NULL))
                         if((equal(p1->1child,p2->1child))
                         ans = equal(p1->rchild,p2->rchild);
                         else
                            ans = false;
                         else
                            ans = false;
                  }
              return(ans);
              }
          The following function creates exact copy of a given binary trees.
          Tnode *copytree(tnode *p)
              {
                  tnode *q;
                  {
                  if(p == NULL)
                     return(NULL);
                  else
                     {




                                           LOVELY PROFESSIONAL UNIVERSITY                                   111
   111   112   113   114   115   116   117   118   119   120   121