Binary search tree with iterator in C++
Jun 25, 2014 at 9:08pm UTC
Hi, could someone please provide me a code of binary search tree template with implemented iterator for tree traversal from the smallest node to the largest? Some examples, links, advices would help. Thanks
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
template <class T>
struct tree_node
{
tree_node<T>* left;
tree_node<T>* right;
T data;
};
template <class T>
class BinarySearchTree
{
private :
tree_node<T>* root;
public :
BinarySearchTree()
{
root = NULL;
}
bool search(T);
bool isEmpty() const { return root==NULL; }
void print_inorder();
void inorder(tree_node<T>*);
void print_preorder();
void preorder(tree_node<T>*);
void print_postorder();
void postorder(tree_node<T>*);
void insert(T);
void remove(T);
};
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
template <class T>bool BinarySearchTree<T>::search(T d)
{
tree_node<T>* temp = root;
while (temp != NULL)
{
if (temp->data == d)
{
cout<<"BST contains this node" <<endl;
return true ;
}
else
{
if (d > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}
}
cout<<"BST does not contain this node" <<endl;
return false ;
}
template <class T>
void BinarySearchTree<T>::insert(T d)
{
tree_node<T>* t = new tree_node<T>;
tree_node<T>* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
if (isEmpty()) root = t;
else
{
tree_node<T>* curr;
curr = root;
while (curr)
{
parent = curr;
if (t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if (t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
template <class T>void BinarySearchTree<T>::remove(T d)
{
bool found = false ;
if (isEmpty())
{
cout<<" This Tree is empty! " <<endl;
}
tree_node<T>* curr;
tree_node<T>* parent;
curr = root;
while (curr != NULL)
{
if (curr->data == d)
{
found = true ;
break ;
}
else
{
parent = curr;
if (d>curr->data) curr = curr->right;
else curr = curr->left;
}
}
if (!found)
{
cout<<" Data not found! " <<endl;
}
if ((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL
&& curr->right == NULL))
{
if (curr->left == NULL && curr->right != NULL)
{
if (parent->left == curr)
{
parent->left = curr->right;
delete curr;
}
else
{
parent->right = curr->right;
delete curr;
}
}
else
{
if (parent->left == curr)
{
parent->left = curr->left;
delete curr;
}
else
{
parent->right = curr->left;
delete curr;
}
}
}
if ( curr->left == NULL && curr->right == NULL)
{
if (parent->left == curr) parent->left = NULL;
else parent->right = NULL;
delete curr;
}
if (curr->left != NULL && curr->right != NULL)
{
tree_node<T>* chkr;
chkr = curr->right;
if ((chkr->left == NULL) && (chkr->right == NULL))
{
curr = chkr;
delete chkr;
curr->right = NULL;
}
else
{
if ((curr->right)->left != NULL)
{
tree_node<T>* lcurr;
tree_node<T>* lcurrp;
lcurrp = curr->right;
lcurr = (curr->right)->left;
while (lcurr->left != NULL)
{
lcurrp = lcurr;
lcurr = lcurr->left;
}
curr->data = lcurr->data;
delete lcurr;
lcurrp->left = NULL;
}
else
{
tree_node<T>* tmp;
tmp = curr->right;
curr->data = tmp->data;
curr->right = tmp->right;
delete tmp;
}
}
}
}
template <class T>
void BinarySearchTree<T>::print_inorder()
{
inorder(root);
}
template <class T>
void BinarySearchTree<T>::inorder(tree_node<T>* p)
{
if (p != NULL)
{
if (p->left) inorder(p->left);
cout<<" " <<p->data<<" " ;
if (p->right) inorder(p->right);
}
}
template <class T>
void BinarySearchTree<T>::print_preorder()
{
preorder(root);
}
template <class T>
void BinarySearchTree<T>::preorder(tree_node<T>* p)
{
if (p != NULL)
{
cout<<" " <<p->data<<" " ;
if (p->left) preorder(p->left);
if (p->right) preorder(p->right);
}
}
template <class T>
void BinarySearchTree<T>::print_postorder()
{
postorder(root);
}
template <class T>
void BinarySearchTree<T>::postorder(tree_node<T>* p)
{
if (p != NULL)
{
if (p->left) postorder(p->left);
if (p->right) postorder(p->right);
cout<<" " <<p->data<<" " ;
}
}
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
int main()
{
BinarySearchTree<int > b;
int ch,tmp,tmp1,tmp2;
while (1)
{
cout<<endl<<endl;
cout<<" Binary Search Tree Operations " <<endl;
cout<<" ----------------------------- " <<endl;
cout<<" 1. Insertion/Creation " <<endl;
cout<<" 2. In-Order Traversal " <<endl;
cout<<" 3. Pre-Order Traversal " <<endl;
cout<<" 4. Post-Order Traversal " <<endl;
cout<<" 5. Removal " <<endl;
cout<<" 6. Find " <<endl;
cout<<" 7. Exit " <<endl;
cout<<" Enter your choice : " ;
cin>>ch;
switch (ch)
{
case 1 : cout<<" Enter Number to be inserted : " ;
cin>>tmp;
b.insert(tmp);
break ;
case 2 : cout<<endl;
cout<<" In-Order Traversal " <<endl;
cout<<" -------------------" <<endl;
b.print_inorder();
break ;
case 3 : cout<<endl;
cout<<" Pre-Order Traversal " <<endl;
cout<<" -------------------" <<endl;
b.print_preorder();
break ;
case 4 : cout<<endl;
cout<<" Post-Order Traversal " <<endl;
cout<<" --------------------" <<endl;
b.print_postorder();
break ;
case 5 : cout<<" Enter data to be deleted : " ;
cin>>tmp1;
b.remove(tmp1);
break ;
case 6 : cout<<" Enter data to be found : " ;
cin>>tmp2;
b.search(tmp2);
break ;
case 7 : system("pause" );
return 0;
break ;
}
}
}
Topic archived. No new replies allowed.