Data Structure Using C++
Tree Data Structure
String Binary Search Tree
#include"treeNodeTemplate.cpp" void insert(treeNode<char*> *node, char* val); void inOrder (treeNode<char*> *rootNode); int main () { char* arr[20]={"babble","fable","jacket","backup","eagle","daily",
"bandit","abandon","abash","accuse","economy","adhere","advise",
"gain",
"cease","debunk","feeder","genius","fetch","chain"};
treeNode<char*> *root= new treeNode<char*>(arr[0]);
int i=1;
while(i<20)
{
insert(root,arr[i++]);
}
inOrder(root);
}
void insert(treeNode<char*> *node, char* val)
{
treeNode<char*> *ptr=new treeNode<char*>(val);
treeNode<char*> *p,*q;
p=q=node;
while((strcmp(val,p->getInfo())!=0)&&q!=NULL)
{
p=q;
if(strcmp(val,p->getInfo())>0)
{
q=q->getRight();
}
else
{
q=q->getLeft();
}
}
if(strcmp(val,p->getInfo())==0)
{
cout<<"Attempt to insert duplicate."<<p->getInfo()<<endl;
delete ptr;
}
else if(strcmp(val,p->getInfo())>0)
{
p->setRight(ptr);
}
else
{
p->setLeft(ptr);
}
}
void inOrder (treeNode<char*> *rootNode)
{
if(rootNode!=NULL)
{
inOrder(rootNode->getLeft());
cout<<rootNode->getInfo()<<endl;
inOrder(rootNode->getRight());
}
}
Let me know in the comment section if you have any question.
Previous Post:
AVL Tree | Adelson-Velskii and Landis Tree In Data Structure Using C++
Next Post:
Comments
Post a Comment