Friday, February 10, 2012

Program Pohon Tree

     #include <iostream>
     #include <cstdlib>
   
    using namespace std;
   
    class BinaryTree
    {
        private:
               BinaryTree* left;
               BinaryTree* right;
               char data;
               BinaryTree* root;
   
        public:
            BinaryTree()
            {
               root = NULL;
            }
   
            bool isEmpty() const { return root==NULL; }
            void print_inorder();
            void inorder(BinaryTree*);
            void print_preorder();
            void preorder(BinaryTree*);
            void print_postorder();
            void postorder(BinaryTree*);
            void insert(char);
            void remove(char);
    };
   
    void BinaryTree::insert(char d)
    {
        BinaryTree* t = new BinaryTree;
        BinaryTree* parent;
        t->data = d;
        t->left = NULL;
        t->right = NULL;
        parent = NULL;
   
        if(isEmpty()) root = t;
        else
        {
            BinaryTree* 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::remove(char d)
    {
        bool found = false;
        if(isEmpty())
        {
            cout<<"Data kosong ! "<<endl;
            return;
        }
   
        BinaryTree* curr;
        BinaryTree* parent;
        curr = root;
   
        while(curr != NULL)
        {
             if(curr->data == d)
             {
                found = true;
                break;
             }
             else
             {
                 parent = curr;
                 if(d>curr->data) curr = curr->right;
                 else curr = curr->left;
             }
        }
        if(!found)
             {
            cout<<" Data not found! "<<endl;
            return;
        }
   
        if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL&& curr->right == NULL))
        {
           if(curr->left == NULL && curr->right != NULL)
           {
               if(parent->left == curr)
               {
                 parent->left = curr->right;
                 delete curr;
               }
               else
               {
                 parent->right = curr->right;
                 delete curr;
               }
           }
           else
           {
              if(parent->left == curr)
               {
                 parent->left = curr->left;
                 delete curr;
               }
               else
               {
                 parent->right = curr->left;
                 delete curr;
               }
           }
         return;
        }
             if( curr->left == NULL && curr->right == NULL)
        {
            if(parent->left == curr) parent->left = NULL;
            else parent->right = NULL;
                      delete curr;
                      return;
        }
   
        if (curr->left != NULL && curr->right != NULL)
        {
            BinaryTree* chkr;
            chkr = curr->right;
            if((chkr->left == NULL) && (chkr->right == NULL))
            {
                curr = chkr;
                delete chkr;
                curr->right = NULL;
            }
            else
            {
   
                if((curr->right)->left != NULL)
                {
                    BinaryTree* lcurr;
                    BinaryTree* lcurrp;
                    lcurrp = curr->right;
                    lcurr = (curr->right)->left;
                    while(lcurr->left != NULL)
                    {
                       lcurrp = lcurr;
                       lcurr = lcurr->left;
                    }
            curr->data = lcurr->data;
                    delete lcurr;
                    lcurrp->left = NULL;
               }
               else
               {
                   BinaryTree* tmp;
                   tmp = curr->right;
                   curr->data = tmp->data;
               curr->right = tmp->right;
                   delete tmp;
               }
   
            }
             return;
        }
   
    }
   
    void BinaryTree::print_inorder()
    {
      inorder(root);
    }
   
    void BinaryTree::inorder(BinaryTree* 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(BinaryTree* 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(BinaryTree* p)
    {
        if(p != NULL)
        {
            if(p->left) postorder(p->left);
            if(p->right) postorder(p->right);
            cout<<" "<<p->data<<" ";
        }
        else return;
    }
   
    int main()
    {
        system("color f0");
        BinaryTree b;
        int banyak;
        int ch,tmp1;
        char tmp;
        while(1)
        {
          cout<<"\n1.Masukan Node ";
          cout<<"\n2.Kunjungan In-Order";
          cout<<"\n3.Kunjungan Pre-Order";
          cout<<"\n4.Kunjungan Post-Order";
          cout<<"\n5.Delete";
          cout<<"\n6.Exit";
          cout<<"\n";
``          cout<<"\n";
          cout<<"Masukkan pilihanmu : ";
          cin>>ch;
          switch(ch)
           {
               case 1 : cout<<" Masukkan banyak node : ";
                        cin>>banyak;
                        for(int i=0;i<banyak;i++){
                        cin>>tmp;
                        b.insert(tmp);
                        }
   
                        break;
               case 2 : cout<<endl;
                        cout<<" Kunjungan In-Order"<<endl;
                        cout<<" -------------------"<<endl;
                        b.print_inorder();
                        break;
               case 3 : cout<<endl;
                        cout<<" Kunjungan Pre-Order"<<endl;
                        cout<<" -------------------"<<endl;
                        b.print_preorder();
                        break;
               case 4 : cout<<endl;
                        cout<<" Kunjungan Post-Order"<<endl;
                        cout<<" --------------------"<<endl;
                        b.print_postorder();
                        break;
               case 5 : cout<<" Masukkan data yang dihapus : ";
                        cin>>tmp1;
                        b.remove(tmp1);
                        break;
               case 6 :
                        return 0;
   
           }
           cout<<endl;
           system("pause");
           system("cls");
        }
        system("PAUSE");
        return EXIT_SUCCESS;
    }


download program antrian


download aplikasi perpustakaan

No comments:

Post a Comment