When searching for information about the Binary Tree, the code on people's blogs are complicated, so here I'm trying to code a very simple Binary Tree.
Am I missing anything? Is there a better way I could be implementing the BinTree::Remove?
#ifndef _BINTREE_H__
#define _BINTREE_H__
using namespace std;
class BinTree{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
};
tree_node* root;
public:
BinTree()
{
root = NULL; //Don't forget The constructor!It's Gonna kill the code
}
void Insert(int);
//int nodes();
bool IsEmpty(){ return root == NULL; }
void print_Preorder();
void Preorder(tree_node *);
bool Search(int);
void Remove(int);
};
void BinTree::Insert(int d){
tree_node * t = new tree_node;
tree_node * parent = NULL;
t->data = d;
t->left = NULL;
t->right = NULL;
if (IsEmpty()){
root = t;
}
else{
tree_node * curr;
curr = root;
while (curr){
parent = curr;
if (t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if (parent->data > t->data)
parent->left = t;
else
parent->right = t;
}
}
void BinTree::print_Preorder()
{
Preorder(root);
}
void BinTree::Preorder(tree_node* p)
{
if (p != NULL)
{
cout << " " << p->data << " ";
if (p->left) Preorder(p->left);
if (p->right) Preorder(p->right);
}
else return;
}
bool BinTree::Search(int d)
{
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;
}
void BinTree::Remove(int d)
{
bool found = false;
if (IsEmpty())
{
cout << " This Tree is empty! " << endl;
return;
}
tree_node* curr;
tree_node* parent;
curr = root;
parent = NULL;
while (curr != NULL)
{
if (curr->data == d)
{
found = true;
break;
}
else
{
parent = curr;
if (d>curr->data) curr = curr->right;//l
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;
}
}
#endif