BST tree


#include "BST_CLAS.h"

template<class type>
BST_CLAS<type>::BST_CLAS()
{
root=NULL;
current=NULL;
previous=NULL;
}

template<class type>
node<type> *BST_CLAS<type> :: Return_current()
{
return current;
}

template<class type>
node<type> *BST_CLAS<type> :: Return_previous()
{
return previous;
}

template<class type>
node<type> *BST_CLAS<type> :: Return_root()
{
return root;
}


template<class type>
void BST_CLAS<type> :: insert(node<type> *t,type key)
{
if (root == NULL)
{
node<type> *newnode;
newnode=new node<type>;
root=newnode;
t=newnode;
previous=t;
current=t;
newnode->info=key;
newnode->left=NULL;
newnode->right=NULL;
return;
}
else if (t==NULL)
{
return;
}
else if (t->right==NULL && t->info<key )
{
node<type> *newnode;
newnode=new node<type>;
t->right=newnode;
newnode->info=key;
newnode->left=NULL;
newnode->right=NULL;
return;

}
else if (t->left==NULL && t->info>key )
{
node<type> *newnode;
newnode=new node<type>;
t->left=newnode;
newnode->info=key;
newnode->left=NULL;
newnode->right=NULL;
return;
}

else if (t->info>key)
{
insert(t->left,key);
}
else if (root->info<key)
{
insert(t->right,key);
}
}

template<class type>
void BST_CLAS<type>::Search(type key)
{
if (get(root,key))
{
cout<<"The number "<<current->info<<" found "<<endl;
current=root;
previous=root;
}
else cout<<"The number "<<key<<" is not found \n";


}

template<class type>
bool BST_CLAS<type> :: get(node<type> *t,type key)
{
if (t==NULL)
return 0;
else if (t->info==key)
return 1;
else if (t->info>key)
{
previous=t;
current=t->left;
return get(t->left,key);
}
else
{
previous=t;
current=t->right;
return get(t->right,key);
}
}

template<class type>
void BST_CLAS<type>::delete_key(node<type> *t,type key)
{

if (get(t,key))
{
if (current==previous && current->right==NULL && current->left==NULL )
{
previous=NULL;
current=NULL;
root=NUL L;
}
else if (current->left==NULL && current->right==NULL)
{
if (previous->left==current)
{
previous->left=NULL;
delete current;
}
else
{
previous->right=NULL;
delete current;
}
}

else if (current->left==NULL && current->right!=NULL)
{
if (previous->left==current)
{
previous->left=current->right;
current->right=NULL;
delete current;
}
else
{
previous->right=current->right;
current->right=NULL;
delete current;
}

}

else if (current->right==NULL && current->left!=NULL)
{
if (previous->left==current)
{
previous->left=current->left;
current->left=NULL;
delete current;
}
else
{
previous->right=current->left;
current->left=NULL;
delete current;
}

}

else if (previous->left!=NULL && previous->right!=NULL)
{
node<type> *temp;

previous=current;
current=current->left;
temp=previous;
while (current->right!=NULL)
{
temp=current;
current=current->right;
}
swap(previous->info , current->info);
if (temp->left==current)
temp->left=current->left;
else
temp->right=NULL;
delete current;
}

}
else cout<<key<<" is not found \n";
}

template<class type>
void BST_CLAS<type>::swap(type &x,type &y)
{
type temp=x;
x=y;
y=temp;
}

template<class type>
void BST_CLAS<type>::inorder(node<type> *t)
{
if(t!=NULL)
{
inorder(t->left);
cout<<t->info<<" ";
inorder(t->right);
}
}

template<class type>
void BST_CLAS<type>::preorder(node<type> *t)
{
if(t!=NULL)
{
cout<<t->info<<" ";
inorder(t->left);
inorder(t->right);
}
}

template<class type>
void BST_CLAS<type>::Clear_Tree(node<type>* &t)
{
if (t!=NULL)
{
Clear_Tree(t->left);
Clear_Tree(t->right);
delete t;
t=NULL;
}
}

template<class type>
void BST_CLAS<type>::Copy_Tree( node<type> *Root , node<type> * &copy )
{
if (Root==NULL)
copy=NULL;
else
{
copy=new node<type>;
copy->info=Root->info;
Copy_Tree(Root->left,copy->left);
Copy_Tree(Root->right,copy->right);
}
}

template<class type>
bool BST_CLAS<type>:: tree_is_empty()
{
if (root==NULL)
return 1;
else
return 0;
}

template<class type>
BST_CLAS<type>:: BST_CLAS( BST_CLAS<type> & obj)
{
if (obj.root==NULL)
root=NULL;
else Copy_Tree(obj.root,root);

}

template<class type>
BST_CLAS<type>::~BST_CLAS()
{
Clear_Tree(root);
}

#pragma once
template<class type>
struct node
{
node<type> *left;
node<type> *right;
type info;
};
template<class type>
class BST_CLAS
{
node<type> *root;
node<type> *current;
node<type> *previous;
public:
BST_CLAS();
BST_CLAS(BST_CLAS & obj);
void insert(node<type> *loc,type key);
bool get(node<type> *t,type key);
void delete_key(node<type> *t,type key);
void Search(type key);
node<type> *Return_root();
node<type> *Return_current();
node<type> *Return_previous();
bool tree_is_empty();
void inorder(node<type> *t);
void preorder(node<type> *t);
void swap(type &x,type &y);
void Copy_Tree( node<type> *Root , node<type> * &copy );
void Clear_Tree(node<type> * &t);
~BST_CLAS(void);
};

#include<iostream>
#include<string>
#include"BST_CLAS.cpp"
using namespace std;

int main()
{
BST_CLAS<string> obj;
string num;
cout<<"Insert in the tree as string \nEnter -999 to stop the insertion\n";
cin>>num;
while (num!="-999")
{
obj.insert(obj.Return_root(), num);
cin>>num;
}
cout<<"\n\n";
cout<<"Inorder \n";
obj.inorder(obj.Return_root());
cout<<endl;
cout<<"\nPreorder \n";
obj.preorder(obj.Return_root());
cout<<endl<<endl;
obj.Search("ALI");
cout<<endl;
system("pause");
}
Please edit your post and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.

You forgot to ask a question.
Topic archived. No new replies allowed.