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) {
00069 root = bi_tree_init(key, val);
00070 }
00071 else {
00072 if (root->key > key)
00073 bi_tree_insert(root->leftptr, key, val);
00074 else
00075 if (root->key < key)
00076 bi_tree_insert(root->rightptr, key, val);
00077 else {
00078 printf("binary_tree::bi_tree_insert: key %d already found in tree\n",
00079 key );
00080 }
00081 }
00082 }
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) {
00112 root = root->rightptr;
00113 }
00114 else
00115 root = NULL;
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 {
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
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;
00185 break;
00186 case 2: {
00187 bi_tree_ptr tmp1, tmp2;
00188
00189
00190 tmp1 = root->rightptr;
00191 tmp2 = unlink(tmp1);
00192
00193
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 }
00202 free_tree_mem( node );
00203 }
00204 }
00205 return retval;
00206 }
00207
00208
00209
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
00302 if (depth_left > depth_right)
00303 depth = depth_left;
00304 else
00305 depth = depth_right;
00306
00307 depth++;
00308 }
00309
00310 return depth;
00311 }
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 }
00332
00333
00334
00335
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 }