00001
00002 #ifndef BITREE_H
00003
00004 #define BITREE_H
00005
00006 #include <stdlib.h>
00007 #include "ianstd.h"
00008
00009
00031
00032
00065 class binary_tree {
00066 protected:
00067
00068 typedef struct bi_tree_struct {
00069 unsigned int key;
00070 unsigned int val;
00071 bi_tree_struct *leftptr, *rightptr;
00072 } bi_tree;
00073
00074 public:
00075 typedef bi_tree *bi_tree_ptr;
00076
00077 private:
00078 bi_tree_ptr root;
00079
00080 private:
00081 void bi_tree_print(bi_tree_ptr root, unsigned int indent);
00082 void print_spaces(unsigned int indent);
00083 void bi_tree_print_sorted(bi_tree_ptr root);
00084 unsigned int bi_tree_max_branch( bi_tree_ptr root );
00085 unsigned int bi_tree_cnt( bi_tree_ptr root );
00086
00087 public:
00088 void print_tree(void) { bi_tree_print( root, 0 ); }
00089 void print_sorted(void) { bi_tree_print_sorted( root ); }
00090 unsigned int max_branch_size(void) { return bi_tree_max_branch(root); }
00091 unsigned int count_tree(void) { return bi_tree_cnt(root); }
00092
00093 private:
00094 bi_tree_ptr unlink(bi_tree_ptr &root);
00095 bi_tree_ptr bi_tree_init(unsigned int key,
00096 unsigned int val);
00097 void bi_tree_insert(bi_tree_ptr &root,
00098 unsigned int key,
00099 unsigned int val);
00100 unsigned int bi_tree_delete(bi_tree_ptr &root,
00101 unsigned int key );
00102
00103 public:
00104 binary_tree(void) { root = NULL; }
00105
00106 void tree_insert( unsigned int key,
00107 unsigned int val )
00108 { bi_tree_insert(root, key, val); }
00109
00110 unsigned int tree_delete( unsigned int key )
00111 { return bi_tree_delete(root, key ); }
00112
00113 virtual bi_tree_ptr get_tree_mem(void);
00114 virtual void free_tree_mem( bi_tree_ptr node );
00115 };
00116
00117 #endif