Hello and hii fellows....
I want your help right now in making a BST in which we can perform treeSearch....treeMINIMUM....treeMAXIMUM....treeINSERT....treeSUCCESSOR....treePREDECESSOR....treeDELETE....alongwith INORDER....POSTORDER....PREORDER traversal methods....
I have made a program consistiting of all methods excluding treeDELETE....
I'm ordered to make a tree-class....and a structure for node of a tree....
but im confused how to use those methods in MAIN() function after making Objects of Class-tree....Im a very primary-level programmer....so i don't use different functions for printing or whatever....i hope you'll get it from my code..
Code is:
#include <iostream.h>
#include <conio.h>
#define NULL 0
struct node
{
node* parent;
node* left;
node* right;
int key;
};
class BST
{
private:
node* root;
node array[10];
public:
BST(int data) //Constructor
{
COUT<<"\n Enter element to insert in the tree: \n";
cin>>data;
InsertTree(root,data);
cout<<endl;
}
void INSERTTree(node* i,int k); //Functions declarations
void PreOT(node* i);
void IOT(node* i);
void POT(node* i);
int SEARCHtree(node* i, int k);
int MINtree(node * i);
int MAXtree(node * i);
void DELETETree(node* i,int k);
int treeSuccessor(node * i);
int treePredecessor(node * i);
}; //end class
void BST::INSERTTree(node* i, int k)
{
if(i->key==NULL)
{
i->key = k;
i->left = NULL:
i->right = NULL;
}
else if(k < i->key) //If element is smaller than node
{
INSERTTree(i->left, k);
}
else if(k >= i->key)
{
INSERTTree(i->right, k);
}
}
void BST::PreOT(node* i)
{
if(i != NULL)
{
cout<<(i->key)<< endl;
PreOT(i->left);
PreOT(i->right);
}
}
void BST::IOT(node* i)
{
if(i != NULL)
{
IOT(i->left);
cout<< (i->key)<< endl;
IOT(i->right);
}
}
void BST::POT(node * i)
{
if(i != NULL)
{
POT(i->left);
POT(i->right);
cout<<(i->key)<< endl;
}
}
int BST::SEARCHtree(node* i, int k)
{
if(i==NULL)
{
return 0;
}
else if(k == i->key)
{
return(i->key);
}
else if(k < i->key)
{
return TreeSearch(i->left, k);
}
else(k >= i->key)
{
return TreeSearch(i->right, k);
}
}
int BST::MINtree(node * i)
{
while(i->left != NULL)
{
i=i->left;
}
return (i->key);
}
int BST::MAXtree(node * i)
{
while(i->right !=NULL)
{
i=i->right;
}
return (i->key);
}
int BST::treeSuccessor(node * i)
{
if(i->right != NULL)
{
return TreeMin(i->right);
}
node * y= i->parent;
while(y != NULL <> i= y->right);
{
i=y;
y=y->parent;
}
return (y->key);
}
int BST::treePredecessor(node * i)
{
if(i->left != NULL)
{
return TreeMax(i->left);
}
node * y= i->parent;
while(y != NULL <> i= y->left);
{
i=y;
y=y->parent;
}
return (y->key);
}
int main()
{
clrscr();
BST t1;
t1.POT(node* i) //don't know how to call methods..
getch();
return 0;
}
I think i've made mistakes in functions too..I want to alsohave COUT for printing in INSERTTree method......i don't know what to do....i have just done..whatever my mind thought....
Seeking for your urgent help....but i hope you'll make alterations in this same above code..otherwise i might not be able to get the code
Thanks in advance....!!