Data Structure Using C++
Tree Data Structure
Binary Search Tree
(LevelOrder Traverse Using Non Recursive Calls) Binary Search Tree
#include "treeNode.cpp"
#include "QueueTemplate.cpp"
void insert(treeNode* root, int info);
int levelOrder(treeNode *root);
int main ()
{
int arr[20]={14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5};//if size is not initialzed it gives garbage values.
treeNode *root=new treeNode(arr[0]);
int i=1;
while(arr[i]!=NULL)
{
insert(root,arr[i++]);
}
levelOrder(root);
}
int levelOrder(treeNode* root)
{
queue<treeNode*> q;
treeNode* p=root;
if(p==NULL)
{
return 0;
}
q.enqueue(p);
while(!q.isEmpty())
{
p=q.getFront();
cout<<p->getInfo()<<" ";
if(p->getLeft()!=NULL)
{
q.enqueue(p->getLeft());
}
if(p->getRight()!=NULL)
{
q.enqueue(p->getRight());
}
q.dequeue();
}
}
Let me know in the comment section if you have any question.
Previous Post:
(PreOrder | InOrder | PostOrder Traverse Using Recursive Calls) Binary Search Tree In Data Structure Using C++
Comments
Post a Comment