Binary Search Tree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | // bst.cpp
// binary search tree
// FB - 201101263
#include<iostream>
#include<iomanip> //width()
using namespace std;
#define width_unit 5
class Tree
{
private:
class Node
{
public:
int data;
Node *left, *right;
Node(int d=0) //constructor
:data(d), left(NULL), right(NULL) {}
};
Node *root;
Node * trav(int, Node * &);
void chop(Node * N);
void copy(Node * N);
void print(ostream &, Node *, int) const;
void print(Node *, int) const;
public:
Tree(void); //constructor
~Tree(void); //destructor
bool find(int);
void insert(int);
void remove(int);
bool empty(void) const;
Tree(const Tree &); //copy constructor
const Tree & operator=(const Tree &); //assignment operator overload
friend ostream & operator<<(ostream &, const Tree &);
};
Tree::Tree(void)
{
root=NULL;
}
bool Tree::empty(void) const
{
return !root;
}
Tree::Node * Tree::trav(int foo, Node * & par)
{
Node * curr=root;
par=NULL;
while(curr && curr->data != foo)
{
par=curr;
if(foo < curr->data)
curr=curr->left;
else
curr=curr->right;
}
return curr;
}
bool Tree::find(int foo)
{
Node * par=NULL;
Node * curr=trav(foo, par);
return curr;
}
void Tree::insert(int foo)
{
Node * par=NULL;
Node * curr=trav(foo,par);
if(!curr) //no duplicates
{
curr= new Node(foo);
if(!par)
root=curr;
else if(foo < par->data)
par->left=curr;
else
par->right=curr;
}
}
void Tree::remove(const int foo)
{
Node * par=NULL; //parent is null by default
Node * curr=trav(foo,par); //locate the node of the foo
if(curr) //if it is not null then
{
if(curr->left && curr->right) //2 children case
{
Node * tmp=curr;
par=curr;
curr=curr->left;
while(curr->right)
{
par=curr;
curr=curr->right;
}
tmp->data=curr->data;
}
//1 or 0 child case
Node *tmp=(curr->left ? curr->left : curr->right);
if(!par)
root=tmp;
else if(par->data < curr->data)
par->right=tmp;
else
par->left=tmp;
delete curr;
}
}
void Tree::chop(Node *N)
{
if(N)
{
chop(N->left);
chop(N->right);
delete N;
}
}
//destructor
Tree::~Tree(void)
{
chop(root);
}
Tree::Tree(const Tree & T)
{
root=NULL;
copy(T.root);
}
void Tree::copy(Node * N)
{
if(N)
{
insert(N->data);
copy(N->left);
copy(N->right);
}
}
const Tree & Tree::operator=(const Tree & T)
{
if(this != &T)
{
chop(root);
root=NULL;
copy(T.root);
}
return *this;
}
//the recursive tree output
void Tree::print(ostream & ost, Node * curr, int level) const
{
if(curr) //if the current node is not null then
{
print(ost,curr->right,level+1); //try to go to right node
//output the node data w/ respect to its level
ost<<setw(level*width_unit)<<curr->data<<endl;
print(ost,curr->left,level+1); //try to go to left node
}
}
//the recursive tree print
void Tree::print(Node * curr, int level) const
{
if(curr) //if the current node is not null then
{
print(curr->right,level+1); //try to go to right node
//print the node data w/ respect to its level
cout<<setw(level*width_unit)<<curr->data<<endl;
print(curr->left,level+1); //try to go to left node
}
}
ostream & operator<<(ostream &ost, const Tree &t)
{
t.print(ost, t.root, 1);
return ost;
}
//Test
int main()
{
Tree mytree;
mytree.insert(5);
mytree.insert(3);
mytree.insert(2);
mytree.insert(7);
mytree.insert(0);
mytree.insert(2);
cout<<mytree<<endl<<endl;
mytree.remove(0);
cout<<mytree<<endl<<endl;
mytree.remove(5);
cout<<mytree<<endl<<endl;
mytree.insert(9);
mytree.insert(10);
mytree.insert(4);
cout<<mytree<<endl<<endl;
mytree.remove(9);
cout<<mytree<<endl<<endl;
Tree mytree2=mytree; //calls the copy constructor, not the assignment
cout<<mytree2<<endl<<endl;
Tree mytree3;
mytree3=mytree2; //calls the assignment operator overload
cout<<mytree3<<endl<<endl;
return 0;
}
|