Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program of traversing a binary tree in inorder iteratively

Posted By: Easy Tutor     Category: C++ Programming     Views: 9865

Write a program to traverse a binary tree in inorder iteratively.

Code for Program of traversing a binary tree in inorder iteratively in C++ Programming

# include <iostream.h>
# include <conio.h>

int no,key,level=0,flag=1;
struct bstree
{
 int no;
 struct bstree *left;
 struct bstree *right;
}*list,*head;


class bst
{
 struct bstree *list;
 public:
    struct bstree *root;
    bst()
    {
     list=NULL;
     root=NULL;
    }
    void create_bstree(struct bstree *list);
    void display_bstree(struct bstree *list);
    void search_bstree(struct bstree *list);
};

void bst :: search_bstree(struct bstree *list)
{
 if(list!=NULL)
 {

 if(key == list->no)
 {
    cout<<"Key Found at Level :" <<level;
    flag=0;
    return;
 }


 if(key < list->no)
 {
    level++;
    search_bstree(list->left);
 }

 if(key > list->no)
 {
    level++;
    search_bstree(list->right);
 }
 }
 else
 {
  return;
 }
}

void bst :: display_bstree(struct bstree *list)
{

   if(list->left!=NULL)
   {
     display_bstree(list->left);
   }

   cout<<list->no<<"\t";

   if(list->right!=NULL)
   {
    display_bstree(list->right);
   }
   return;
}



void bst :: create_bstree(struct bstree *list)
{
  if(root==NULL)
  {
   cout<<endl<<"Enter -1 to Stop ..."<<endl;
   cout<<"Enter Number :";
   cin>>no;
    if(no==-1)
    {
         return;
    }
    else
    {
        list=new bstree;
        list->left=NULL;
        list->right=NULL;
        root=list;
        list->no=no;
    }
  }
  else
  {
     if(no==-1)
     {
      return;
     }
     if(no < list->no)
     {
       if(list->left!=NULL)
       {
        create_bstree(list->left);

       }
       else
       {
     list->left=new bstree;
     list=list->left;
     list->left=NULL;
     list->right=NULL;
     list->no=no;
       }
       if(no==-1)
       return;


     }

     if(no>list->no)
     {
    if(list->right!=NULL)
    {
          create_bstree(list->right);
    }
    else
    {
      list->right=new bstree;
      list=list->right;
      list->left=NULL;
      list->right=NULL;
      list->no=no;
    }
    if(no==-1)
    return;
     }
  }
    cout<<endl<<"Enter -1 to Stop ..."<<endl;
    cout<<"Enter Number :";
    cin>>no;
    if(no==-1)
    {
    return;
    }
    else
    {
    create_bstree(root);
    }
}
void main()
{
   clrscr();
   bst bst1;
   bst1.create_bstree(bst1.root);
   cout<<endl;
   cout<<"Inorder"<<endl;
   bst1.display_bstree(bst1.root);
   cout<<endl<<"Enter Number to search :";
   cin>>key;
   bst1.search_bstree(bst1.root);
   if(flag==1)
   {
    cout<<endl<<"Search key not Found in the tree"<<endl;
   }
   getch();
}
  
Share: 


Didn't find what you were looking for? Find more on Program of traversing a binary tree in inorder iteratively Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program of traversing a binary tree in inorder iteratively is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!