Binary Search Tree
Aug 21, 2009 at 3:25pm UTC
hi, i am a new member and beginner in c++. i have a question. the code is from an older post from SteakRider.
the code is here or you can look
http://www.cplusplus.com/forum/beginner/1276/.
if you see the code in the remove(T) function we must first use the fuction search(T) to see if the node exist or not. but in the following code in the remove(T) funcion it doesn't call straight the search(T) function but he use the search(T) function code again in the remove(T) function.
question: how can i call straight the search(T) function from the remove(T) without writting the code again ?
thanks and sorry for not speaking so good english.
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
/************************************************************************/
/* Project: Implementation Binary Seach Tree with templeates */
/* Created by Gofur Halmuratov 1.04.2008 */
/************************************************************************/
#include <iostream>
#include <cstdlib>
using namespace std;
template <class T>
class BinarySearchTree
{
private :
struct tree_node
{
tree_node* left;
tree_node* right;
T data;
};
tree_node* root;
public :
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root==NULL; }
void print_inorder();
void inorder(tree_node*);
void print_preorder();
void preorder(tree_node*);
void print_postorder();
void postorder(tree_node*);
void insert(T);
void remove(T);
bool search(T);
};
template <class T>
void BinarySearchTree<T>::insert(T d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
// is this a new tree?
if (isEmpty()) root = t;
else
{
//Note: ALL insertions are as leaf nodes
tree_node* curr;
curr = root;
// Find the Node's parent
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>
bool BinarySearchTree<T>::search(T d)
{
//Locate the element
bool found = false ;
if (isEmpty())
{
cout<<" This Tree is empty! " <<endl;
return false ;
}
tree_node* curr;
tree_node* parent;
curr = root;
parent = (tree_node*)NULL;
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;
}
else
{
cout<<" Data found! " <<endl;
}
return found;
}
template <class T>
void BinarySearchTree<T>::remove(T d)
{
bool found = false ;
if (isEmpty())
{
cout<<" This Tree is empty! " <<endl;
return ;
}
tree_node* curr;
tree_node* parent;
curr = root;
parent = (tree_node*)NULL;
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;
return ;
}
// Node with single child
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 // left child present, no right child
{
if (parent->left == curr)
{
parent->left = curr->left;
delete curr;
}
else
{
parent->right = curr->left;
delete curr;
}
}
return ;
}
//We're looking at a leaf node
if ( curr->left == NULL && curr->right == NULL)
{
if (parent == NULL)
{
delete curr;
} else
if (parent->left == curr) parent->left = NULL;
else parent->right = NULL;
delete curr;
return ;
}
//Node with 2 children
// replace node with smallest value in right subtree
if (curr->left != NULL && curr->right != NULL)
{
tree_node* chkr;
chkr = curr->right;
if ((chkr->left == NULL) && (chkr->right == NULL))
{
curr = chkr;
delete chkr;
curr->right = NULL;
}
else // right child has children
{
//if the node's right child has a left child
// Move all the way down left to locate smallest element
if ((curr->right)->left != NULL)
{
tree_node* lcurr;
tree_node* 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* tmp;
tmp = curr->right;
curr->data = tmp->data;
curr->right = tmp->right;
delete tmp;
}
}
return ;
}
}
template <class T>
void BinarySearchTree<T>::print_inorder()
{
inorder(root);
}
template <class T>
void BinarySearchTree<T>::inorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) inorder(p->left);
cout<<" " <<p->data<<" " ;
if (p->right) inorder(p->right);
}
else return ;
}
template <class T>
void BinarySearchTree<T>::print_preorder()
{
preorder(root);
}
template <class T>
void BinarySearchTree<T>::preorder(tree_node* p)
{
if (p != NULL)
{
cout<<" " <<p->data<<" " ;
if (p->left) preorder(p->left);
if (p->right) preorder(p->right);
}
else return ;
}
template <class T>
void BinarySearchTree<T>::print_postorder()
{
postorder(root);
}
template <class T>
void BinarySearchTree<T>::postorder(tree_node* p)
{
if (p != NULL)
{
if (p->left) postorder(p->left);
if (p->right) postorder(p->right);
cout<<" " <<p->data<<" " ;
}
else return ;
}
int main()
{
BinarySearchTree<int > b;
int ch;
int tmp,tmp1;
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. Search " <<endl;
cout<<" 7. Exit " <<endl;
cout<<" Enter your choice : " ;
cin>>ch;
switch (ch)
{
case 1 : cout<<" Enter data to be inserted : " ;
cin.ignore(1);
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.clear();
cin.ignore(1);
cin>>tmp1;
b.remove(tmp1);
break ;
case 6 : cout<<" Enter data to be searched : " ;
cin.ignore(1);
cin>>tmp;
b.search(tmp);
break ;
case 7 : system("pause" );
return 0;
break ;
}
}
}
Last edited on Aug 21, 2009 at 3:37pm UTC
Aug 21, 2009 at 11:00pm UTC
any help ???
Aug 22, 2009 at 12:02am UTC
try returning a node from your search function.
Topic archived. No new replies allowed.