0% found this document useful (0 votes)
4 views

Binary Search Tree (1)

The document contains a C++ implementation of a binary search tree (BST) with various functionalities such as insertion, deletion, searching, and traversals (inorder, preorder, postorder). It defines a structure for the BST nodes and a class for the BST that includes methods to manage the tree and perform operations. The main function provides a menu-driven interface for users to interact with the BST.

Uploaded by

tesfayeleta20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Binary Search Tree (1)

The document contains a C++ implementation of a binary search tree (BST) with various functionalities such as insertion, deletion, searching, and traversals (inorder, preorder, postorder). It defines a structure for the BST nodes and a class for the BST that includes methods to manage the tree and perform operations. The main function provides a menu-driven interface for users to interact with the BST.

Uploaded by

tesfayeleta20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <iostream>

using namespace std;


struct BSTNodeType{
int item;
BSTNodeType *leftchild;
BSTNodeType *rightchild;
};
class binarySearchTree{
private:
BSTNodeType *root;
public:
bool isTreeEmpty();
bool searchBST(int);
void insertBST(int);
void deleteBST(int);
void deleteItem(BSTNodeType * &p);
void inorderTraversal(BSTNodeType *p);
void inorder();
void postorderTraversal(BSTNodeType *p);
void postorder();
void preorderTraversal(BSTNodeType *p);
void preorder();
int heightOfTree();
int height(BSTNodeType *p);
int findMin();
int small(BSTNodeType *p);
int findMax();
int large(BSTNodeType *p);
int numberofNode();
int countNode(BSTNodeType *p);
int numberofChild();
int countChild(BSTNodeType *p);
void destroy(BSTNodeType *p);
binarySearchTree();
~binarySearchTree();
};
binarySearchTree::~binarySearchTree(){
destroy(root);
}
void binarySearchTree::destroy(BSTNodeType *node){
if(isTreeEmpty()){
cout<<"\n Tree is Empty";
}else{
destroy(node->leftchild);
destroy(node->rightchild);
delete node;
node=NULL;
}
}
binarySearchTree::binarySearchTree(){
root=NULL;
}
bool binarySearchTree::isTreeEmpty(){
return root==NULL;
}
void binarySearchTree::insertBST(int newitem){
BSTNodeType *temp=new BSTNodeType;
BSTNodeType *current,*prevcurrent;
temp->item=newitem;
temp->leftchild=NULL;
temp->rightchild=NULL;

if(isTreeEmpty()){
root=temp;
}else{
current=root;
while(current!=NULL){
prevcurrent=current;
if(current->item==newitem){
cout<<"\n The item is already in the tree, duplicated item is not
allowed";
return;
}else if(current->item>newitem){
current=current->leftchild;
}else{
current=current->rightchild;
}
}
if(prevcurrent->item > newitem)
prevcurrent->leftchild=temp;
else
prevcurrent->rightchild=temp;
}
cout<<"\n Data is successfully stored ";
}
bool binarySearchTree::searchBST(int key){
BSTNodeType *current;
bool found=false;
if(isTreeEmpty()){
cout<<"\n Tree is empty ";
}else{
current=root;
while(current!=NULL){
if(current->item==key)
found=true;
else if(current->item > key)
current=current->leftchild;
else
current=current->rightchild;
}
}
return found;
}
void binarySearchTree::deleteBST(int delete_key){
BSTNodeType *current;
BSTNodeType *prevcurrent;
bool found=false;
if(isTreeEmpty()){
cout<<"\n Tree is Empty";
}else{
current=root;
prevcurrent=root;
while(current!=NULL && !found){
if(current->item==delete_key){
found=true;
}else{
prevcurrent=current;
if(current->item > delete_key)
current=current->leftchild;
else
current=current->rightchild;
}
}
if(found){
if(current==root)
deleteItem(root);
else if(prevcurrent->item > delete_key)
deleteItem(prevcurrent->leftchild);
else
deleteItem(prevcurrent->rightchild);
}else{
cout<<"\n Item to be deleted is not found ";
}
}
}
void binarySearchTree::deleteItem(BSTNodeType* &removeNode){
BSTNodeType *current, *prevcurrent, *temp;
if(removeNode->leftchild ==NULL && removeNode->rightchild==NULL){
temp=removeNode;
removeNode=NULL;
delete temp;
}else if(removeNode->leftchild == NULL){
temp=removeNode;
removeNode=temp->rightchild;
delete temp;
}else if(removeNode->rightchild == NULL){
temp=removeNode;
removeNode=temp->leftchild;
delete temp;
}else{
current=removeNode->leftchild;
prevcurrent=NULL;
while(current->rightchild!=NULL){
prevcurrent=current;
current=current->rightchild;
}
removeNode->item=current->item;
if(prevcurrent==NULL)
removeNode->leftchild=current->leftchild;
else
prevcurrent->rightchild=current->leftchild;

delete current;
}
}
void binarySearchTree::inorder(){
inorderTraversal(root);
}
void binarySearchTree::inorderTraversal(BSTNodeType *parent){
if(parent!=NULL){
inorderTraversal(parent->leftchild);
cout<<"\t"<<parent->item;
inorderTraversal(parent->rightchild);
}
}
void binarySearchTree::preorder(){
preorderTraversal(root);
}
void binarySearchTree::preorderTraversal(BSTNodeType *parent){
if(parent!=NULL){
cout<<"\t"<<parent->item;
inorderTraversal(parent->leftchild);
inorderTraversal(parent->rightchild);
}
}
void binarySearchTree::postorder(){
postorderTraversal(root);
}
void binarySearchTree::postorderTraversal(BSTNodeType *parent){
if(parent!=NULL){
inorderTraversal(parent->leftchild);
inorderTraversal(parent->rightchild);
cout<<"\t"<<parent->item;
}
}

int binarySearchTree::numberofNode(){
return countNode(root);
}
int binarySearchTree::countNode(BSTNodeType *node){
if(node==NULL){
return 0;
}else{
return 1 + countNode(node->leftchild) + countNode(node->rightchild);
}
}
int binarySearchTree::numberofChild(){
return countChild(root);
}
int binarySearchTree::countChild(BSTNodeType *node){
if(node==NULL)
return 0;
if(node->leftchild==NULL && node->rightchild==NULL)
return 1;
return countChild(node->leftchild) + countChild(node->rightchild);
}
int binarySearchTree::heightOfTree(){
return height(root);
}
int binarySearchTree::height(BSTNodeType *node){
if(node == NULL)
return 0;
else{
int left=height(node->leftchild);
int right=height(node->rightchild);
return 1 + max(left, right);
}
}
int binarySearchTree::findMin(){
return small(root);
}
int binarySearchTree::small(BSTNodeType *node){
if(isTreeEmpty()){
cout<<"\n Tree is Empty ";
return 0;
}
if(node->leftchild==NULL)
return node->item;
return small(node->leftchild);
}
int binarySearchTree::findMax(){
return large(root);
}
int binarySearchTree::large(BSTNodeType *node){
if(isTreeEmpty()){
cout<<"\n Tree is empty ";
return 0;
}else{
while(node->rightchild!=NULL){
node=node->rightchild;
}
return node->item;
}
}
int main()
{
binarySearchTree obj1;
int op;
Menu:
cout<<"\n\n\n press 1: to INSERT item into Tree";
cout<<"\n press 2: to REMOVE item from tree";
cout<<"\n press 3: to SEARCH item in tree ";
cout<<"\n press 4: to INORDER Traverse tree ";
cout<<"\n press 5: to PREODER Traverse tree ";
cout<<"\n press 6: to POSTORDER Traverse tree ";
cout<<"\n press 7: to Find SMALL item in tree ";
cout<<"\n press 8: to Find LARGE item in tree ";
cout<<"\n press 9: to Find HEIGHT of tree ";
cout<<"\n press 10: to Find total number of node in the tree ";
cout<<"\n press 11: to Find total number of child node in the tree ";
cout<<"\n press 12: to print group member";
cout<<"\n press 0: to EXIT\n\n";
cout<<"\n\n Select the option---";
cin>>op;

switch(op){
case 1:
int item;
cout<<"\n Enter element ";
cin>>item;
obj1.insertBST(item);
break;
case 2:
int deleteitem;
cout<<"\n Enter the item to be deleted ";
cin>>deleteitem;
obj1.deleteBST(deleteitem);
break;
case 3:
int key;
cout<<"\n Enter the element you want to search ";
cin>>key;
obj1.searchBST(key);
break;
case 4:
cout<<"\n Inorder Traversal ";
obj1.inorder();
break;
case 5:
cout<<"\n Preorder Traversal ";
obj1.preorder();
break;
case 6:
cout<<"\n Postorder Traversal ";
obj1.postorder();
break;
case 7:
cout<<"\n The smallest number in the tree is ";
obj1.findMin();
break;
case 8:
cout<<"\n The Largest number in the tree is ";
obj1.findMax();
break;
case 9:
cout<<"\n Height of the tree is "<<(obj1.heightOfTree()-1);
break;
case 10:
cout<<"\n Total number of node in the tree is "<<obj1.numberofNode();
break;
case 11:
cout<<"\n Total number of child node in the tree is
"<<obj1.numberofChild();
break;
case 0:
exit(0);
break;
default:
cout<<"\n Try, Agian";
break;
}
goto Menu;
return 0;
}

You might also like