Main Page   Compound List   File List   Compound Members  

bitree.cpp

Go to the documentation of this file.
00001 
00002 #include <stdio.h>
00003 #include "ianstd.h"
00004 #include "bitree.h"
00005 
00026       
00027 
00039 
00040 
00045 binary_tree::bi_tree_ptr binary_tree::bi_tree_init(unsigned int key,
00046                                                    unsigned int val)
00047 {
00048   bi_tree_ptr tmp = (bi_tree_ptr)get_tree_mem();
00049 
00050   tmp->key = key;
00051   tmp->val = val;
00052   tmp->leftptr = NULL;
00053   tmp->rightptr = NULL;
00054   return tmp;
00055 }
00056 
00057 
00064 void binary_tree::bi_tree_insert(bi_tree_ptr &root, 
00065                                  unsigned int key,
00066                                  unsigned int val )
00067 {
00068   if (root == NULL) { // the tree is empty
00069     root = bi_tree_init(key, val);
00070   }
00071   else {
00072     if (root->key > key)
00073       bi_tree_insert(root->leftptr, key, val);     // search down left branch
00074     else
00075       if (root->key < key)
00076         bi_tree_insert(root->rightptr, key, val);  // search down right branch
00077       else { // key == root->key
00078         printf("binary_tree::bi_tree_insert: key %d already found in tree\n",
00079                key );
00080       }
00081   }
00082 } // bi_tree_insert
00083 
00084 
00085 
00086 
00101 binary_tree::bi_tree_ptr binary_tree::unlink(bi_tree_ptr &root)
00102 {
00103   bi_tree_ptr node;
00104 
00105   node = root;
00106   if (root != NULL) {
00107     if (root->leftptr != NULL) {
00108       node = unlink(root->leftptr);
00109     }
00110     else {
00111       if (root->rightptr != NULL) {  // if there is a right branch child
00112         root = root->rightptr;       // replace the node at the end of
00113       }                              // the left branch with this child.
00114       else
00115         root = NULL;  // no children
00116     }
00117   }
00118   return node;
00119 }
00120 
00121 
00152 unsigned int binary_tree::bi_tree_delete(bi_tree_ptr &root, 
00153                                          unsigned int key )
00154 {
00155   unsigned int retval = 0;
00156 
00157   if (root != NULL) {
00158     if (root->key > key)
00159       retval = bi_tree_delete(root->leftptr, key);
00160     else
00161       if (root->key < key)
00162         retval = bi_tree_delete(root->rightptr, key);
00163       else { // the keys are equal
00164         bi_tree_ptr node = NULL;
00165         bi_tree_ptr new_child = NULL;
00166         int num_children = 0;
00167           
00168         node = root;
00169         retval = root->val;
00170 
00171         // Count the children of the node
00172         if (root->leftptr != NULL) {
00173           num_children++;
00174           new_child = root->leftptr;
00175         }
00176         if (root->rightptr != NULL) {
00177           num_children++;
00178           new_child = root->rightptr;
00179         }
00180 
00181         switch (num_children) {
00182         case 0: root = NULL;
00183           break;
00184         case 1: root = new_child;  // Replace the node with its child
00185           break;
00186         case 2: {
00187           bi_tree_ptr tmp1, tmp2;
00188             
00189           // find the next greater node in the tree and unlink it
00190           tmp1 = root->rightptr;
00191           tmp2 = unlink(tmp1);
00192           // Connect the new node to any children, but make sure that 
00193           // we don't point a node at itself.
00194           if (root->leftptr != tmp2)
00195             tmp2->leftptr = root->leftptr;
00196           if (root->rightptr != tmp2)
00197             tmp2->rightptr = root->rightptr;
00198           root = tmp2;
00199         }
00200           break;
00201         } // switch
00202         free_tree_mem( node );
00203       } // keys are equal
00204   }  // root != NULL
00205   return retval;
00206 } // bi_tree_delete
00207 
00208 
00209 /* ------------------- Class functions for debugging --------------- */
00210 
00215 
00216 void binary_tree::print_spaces(unsigned int indent)
00217 {
00218   const int BufSize = 80;
00219   char buf[BufSize+1];
00220   int i;
00221   
00222   if (indent > BufSize)
00223     indent = BufSize;
00224   for (i = 0; i < (int)indent; i++) {
00225     buf[i] = ' ';
00226   }
00227   buf[indent] = '\0';
00228   printf("%s", buf);
00229 }
00230 
00231 
00238 void binary_tree::bi_tree_print(bi_tree_ptr root, unsigned int indent)
00239 {
00240 
00241   if (root != NULL) {
00242     print_spaces(indent);
00243     printf("%d\n", root->key );
00244     if (root->leftptr != NULL)
00245       bi_tree_print(root->leftptr, indent+4);
00246     else {
00247       print_spaces(indent+4);
00248       printf("null\n");
00249     }
00250     if (root->rightptr != NULL)
00251       bi_tree_print(root->rightptr, indent+4);
00252     else {
00253       print_spaces(indent+4);
00254       printf("null\n");
00255     }
00256   }
00257 }
00258 
00259 
00265 void binary_tree::bi_tree_print_sorted(bi_tree_ptr root)
00266 {
00267   if (root != NULL) {
00268     bi_tree_print_sorted(root->leftptr);
00269     printf("%d ", root->key );
00270     bi_tree_print_sorted(root->rightptr);
00271   }
00272 }
00273 
00274 
00275 
00288 unsigned int binary_tree::bi_tree_max_branch(bi_tree_ptr root )
00289 {
00290   unsigned int depth = 0;
00291 
00292   if (root != NULL) {
00293     unsigned int depth_left = 0;
00294     unsigned int depth_right = 0;
00295 
00296     if (root->leftptr != NULL)
00297       depth_left = bi_tree_max_branch(root->leftptr);
00298     if (root->rightptr != NULL)
00299       depth_right = bi_tree_max_branch(root->rightptr);
00300 
00301     // depth = max(depth_left, depth_right)
00302     if (depth_left > depth_right)
00303       depth = depth_left;
00304     else
00305       depth = depth_right;
00306 
00307     depth++;  // Add one for the current node
00308   }
00309 
00310   return depth;
00311 } // binary_tree::bi_tree_max_branch
00312 
00313 
00322 unsigned int binary_tree::bi_tree_cnt(bi_tree_ptr root)
00323 {
00324   unsigned int cnt = 0;
00325 
00326   if (root != NULL) {
00327     cnt = bi_tree_cnt(root->leftptr) + bi_tree_cnt(root->rightptr) + 1;
00328   }
00329 
00330   return cnt;
00331 } // binary_tree::bi_tree_cnt
00332 
00333 
00334 
00335 /* ------------------- Defaults for virtual functions --------------- */
00336 
00341 binary_tree::bi_tree_ptr binary_tree::get_tree_mem(void)
00342 {
00343   printf("binary_tree::get_tree_mem: no memory alloc provided for class\n");
00344   return (bi_tree_ptr)NULL;
00345 }
00346 
00349 void binary_tree::free_tree_mem( bi_tree_ptr addr )
00350 {
00351   printf("binary_tree::free_tree_mem: no memory dealloc provided for class\n");
00352 }

Generated at Tue Jun 10 21:24:10 2003 for Unbalanced Binary Tree Search, Insert and Delete by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001