BinaryTree C++

 The following post is an example of Binary Tree Program C + +
I am working on this program when lab data structure
in this program there are functions such as enter Node, Print In-Order, Print Pre-Order, and Order Print Post
The program, please See Running and Source The program also


#Source Program
#include <cstdlib>
#include <iostream>

using namespace std;

class BinaryTree{
      private :
struct tree_node
        {
           tree_node* left;
           tree_node* right;
           char data;
        };
        tree_node* root;
  
    public:
        BinaryTree()
        {
           root = NULL;
        }
     
        bool isEmpty() const { return root==NULL; }
        void print_inorder();
        void inorder(tree_node*);
        void print_preorder();
        void preorder(tree_node*);
        void print_postorder();
        void postorder(tree_node*);
        void insert(char);
        void remove(char);
        void menu();
};

void BinaryTree::menu(){
 int in;
    char tmp,tmp1;
start:
      system("cls");
      cout<<endl<<"\t\t\t\tMENU"<<endl<<endl;
      cout<<endl<<"\t1.Masukan Node Di Sepasang Pohon (Binary Tree)";
      cout<<endl<<"\t2. Hasil In-Order ";
      cout<<endl<<"\t3. Hasil Pre-Order";
      cout<<endl<<"\t4. Hasil Post-Order";
      cout<<endl<<"\t5.Exit";
      cout<<endl<<endl;
      cout<<"Masukkan pilihan : ";
      cin>>in;
     
      
           if(in==1){
           int n;
           cout<<"masukan banyak data : ";
           cin>>n;
           cout<<" Masukkan Node pohon dengan sepasi : ";
           for(int i=0;i<n;i++){
                    cin>>tmp;
                    insert(tmp);
                    }
                    cout<<endl;
                    system("pause");goto start;}
           else if (in==2){cout<<endl;
                    cout<<" Hasil In-Order "<<endl;
                    cout<<" -------------------"<<endl;
                    print_inorder();
                    cout<<endl;
                    system("pause");goto start;}
                   
           else if (in==3){cout<<endl;
                    cout<<" Hasil Pre-Order"<<endl;
                    cout<<" -------------------"<<endl;
                    print_preorder();
                    cout<<endl;
                    system("pause");goto start;}
                  
           else if(in==4){ cout<<endl;
                    cout<<" Hasil Post-Order "<<endl;
                    cout<<" --------------------"<<endl;
                    print_postorder();
                    cout<<endl;
                    system("pause");goto start;}
           else if(in==5){
                cout<<"terimaksih ";
                cout<<endl;
                }
                   
           else{cout<<" masukan salah ";cout<<endl<<endl; system("pause");goto start;
                    }
                    }
                   
                   
void BinaryTree::insert(char d)
{
    tree_node* t = new tree_node;
    tree_node* parent;
    t->data = d;
    t->left = NULL;
    t->right = NULL;
    parent = NULL;
  
    if(isEmpty()) root = t;
    else
    {
        tree_node* curr;
        curr = root;
        while(curr)
        {
            parent = curr;
            if(t->data > curr->data) curr = curr->right;
            else curr = curr->left;
        }

        if(t->data < parent->data)
           parent->left = t;
        else
           parent->right = t;
    }
}


void BinaryTree::print_inorder()
{
  inorder(root);
}

void BinaryTree::inorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) inorder(p->left);
        cout<<" "<<p->data<<" ";
        if(p->right) inorder(p->right);
    }
    else return;
}

void BinaryTree::print_preorder()
{
    preorder(root);
}

void BinaryTree::preorder(tree_node* p)
{
    if(p != NULL)
    {
        cout<<" "<<p->data<<" ";
        if(p->left) preorder(p->left);
        if(p->right) preorder(p->right);
    }
    else return;
}

void BinaryTree::print_postorder()
{
    postorder(root);
}

void BinaryTree::postorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) postorder(p->left);
        if(p->right) postorder(p->right);
        cout<<" "<<p->data<<" ";
    }
    else return;
}

int main()
{
    BinaryTree b;
    b.menu();           
     
    system("PAUSE");
    return EXIT_SUCCESS;
}

#running Program
































please create your own subsequently developed ability you friend ..
Happy coding


Posted in , . Bookmark the permalink. RSS feed for this post.

Leave a Reply

Pages

Search

Sandiah Notes Template Sandroid11.blogspot.com.