C   13
void insertTree TreeElementType E
Guest on 11th July 2024 08:33:09 AM


  1. #include <stdio.h>
  2. #include "Tree.h"
  3.  
  4. void insertTree( TreeElementType E, Tree *T )
  5. /*  pre: T is a binary search tree */
  6. /* post: E is in T and T is still a binary search tree */
  7. /* NB. T is a reference parameter and the tree is changed as a side-effect */
  8.  { if( *T == NULL )
  9.     { *T = (Tree)malloc(sizeof(Node)); /*new leaf*/
  10.       (*T)->left = (*T)->right = (Tree)NULL;                  /*L*/
  11.       TreeElementMove(E, &((*T)->elt));                       /*.*/
  12.     }                                                         /*A*/
  13.    else/*T not NULL*/                                         /*l*/
  14.       if( TreeElementLess( E, (*T)->elt) )                    /*l*/
  15.          insertTree( E, &((*T)->left) );                      /*i*/
  16.       else if( TreeElementLess((*T)->elt, E) )                /*s*/
  17.          insertTree( E, &((*T)->right) );                     /*o*/
  18.    /* else  equal, in this implementation do nothing */       /*n*/
  19.  }/*insertTree*/
  20.  
  21. /* Insert a New Element E into a Binary Search Tree T. */

Raw Paste

Login or Register to edit or fork this paste. It's free.