Advertisement
shabbyheart

Untitled

Jun 30th, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.29 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Node{
  4. public:
  5.     int key;
  6.     Node *leftChild;
  7.     Node *rightChild;
  8.     Node(int value)
  9.     {
  10.         key = value;
  11.         leftChild = rightChild  = NULL;
  12.     }
  13. };
  14.  
  15. class Tree{
  16. public:
  17.     Node *root;
  18.     Tree()
  19.     {
  20.         root = NULL;
  21.     }
  22.     void insertt(int value)
  23.     {
  24.         Node *newNode = new Node(value);
  25.  
  26.         if( root == NULL)
  27.         {
  28.             root = newNode;
  29.             return;
  30.         }
  31.         else
  32.         {
  33.             Node *currentNode = root;
  34.             Node *parrentNode = NULL;
  35.  
  36.             while(currentNode != NULL)
  37.             {
  38.                 parrentNode = currentNode;
  39.                 if(value <= currentNode ->key)
  40.                 {
  41.                     currentNode = currentNode -> leftChild;
  42.                 }
  43.                 else
  44.                 {
  45.                     currentNode = currentNode -> rightChild;
  46.                 }
  47.             }
  48.             //newNode -> parent = parrentNode;
  49.             if(value <= parrentNode -> key)
  50.             {
  51.                 parrentNode -> leftChild = newNode;
  52.             }
  53.             else
  54.             {
  55.                 parrentNode -> rightChild = newNode;
  56.             }
  57.         }
  58.     }
  59.     void printInorder(Node *temp)
  60.     {
  61.         if(temp != NULL)
  62.         {
  63.             printInorder( temp -> leftChild );
  64.             cout<<temp->key<<" ";
  65.             printInorder( temp -> rightChild);
  66.         }
  67.     }
  68.  
  69.     ///Search function without Recursion
  70. //    void Search(Node *node,int value)
  71. //    {
  72. //        if( node == NULL)
  73. //        {
  74. //            cout<<"value is not found";
  75. //            return;
  76. //        }
  77. //        while( node != NULL){
  78. //            if( node-> key == value)
  79. //            {
  80. //                cout<<"value is "<<node->key<<" found";
  81. //                return ;
  82. //            }
  83. //            if( value < node->key)
  84. //                node = node->leftChild;
  85. //            else
  86. //                node = node->rightChild;
  87. //        }
  88. //        cout<<"Not found"<<endl;
  89. //    }
  90.     ///Search function with recusive
  91.     Node * Search(Node *node,int value)
  92.     {
  93.         if( node == NULL)
  94.         {
  95.             //cout<<"value is not found";
  96.             return node;
  97.         }
  98.         if( node-> key == value)
  99.         {
  100.             //cout<<"value is "<<node->key<<" found";
  101.             return node;
  102.         }
  103.         if( value < node->key)
  104.             Search( node->leftChild, value);
  105.         else
  106.             Search(node->rightChild , value);
  107.     }
  108.     int minimum( Node *node)
  109.     {
  110.         if( node->leftChild == NULL)
  111.         {
  112.             return node->key;
  113.         }
  114.         minimum( node->leftChild);
  115.     }
  116.     int maximum( Node *node )
  117.     {
  118.         if( node->rightChild == NULL)
  119.         {
  120.             //cout<<"Maximum Value is: " <<node->key<<endl;
  121.             return node->key;
  122.         }
  123.         maximum( node->rightChild);
  124.     }
  125.     /// Successor hosce jar succssor ber korbo tar theke boro sob cheye choto number.
  126.     /// key value tar right thakle right er sob ceye choto number ta successor.
  127.     /// right child na thakle
  128.     /// key ta kono parent er left child hole parent ta successor;
  129.     /// key ta right child hole parent er parent ta successor.
  130.     int  successor(Node *root,int value)
  131.     {
  132.         Node *temp = Search(root,value);
  133.         if(temp == NULL)
  134.         {
  135.             return 0;
  136.         }
  137.         if( temp->rightChild != NULL)
  138.             return minimum(temp->rightChild);
  139.  
  140.         else
  141.         {
  142.                 Node *successor= NULL;
  143.                 Node *parent = root;
  144.             while( temp->key != parent->key )
  145.             {
  146.                 if( temp->key <= parent->key)
  147.                 {
  148.                     successor = parent;
  149.                     parent = parent->leftChild;
  150.                 }
  151.                 else
  152.                     parent = parent->rightChild;
  153.             }
  154.             return successor->key;
  155.         }
  156.     }
  157.     Node * deleteNode( Node *root, int value)
  158.     {
  159.         if( root == NULL)
  160.         {
  161.             return root;
  162.         }
  163.         else if( value < root->key)
  164.             root->leftChild=deleteNode( root->leftChild, value);
  165.         else if( value > root->key)
  166.         {
  167.             root->rightChild = deleteNode( root->rightChild, value);
  168.         }
  169.         else
  170.         {
  171.             /// No child
  172.             if( root -> leftChild == NULL  &&  root->rightChild == NULL)
  173.             {
  174.                 cout<<endl<<root->key<<endl;
  175.                 delete(root);
  176.                 root = NULL;
  177.             }
  178.             ///One Child
  179.             else if( root -> leftChild == NULL)
  180.             {
  181.                 Node *temp = root;
  182.                 root = root->rightChild;
  183.                 delete temp;
  184.             }
  185.             /// One child
  186.             else if( root -> rightChild == NULL){
  187.                 Node *temp = root;
  188.                 root = root->leftChild;
  189.                 delete temp;
  190.             }
  191.             /// Two Child
  192.             else
  193.             {
  194.                 int succesor = minimum( root->rightChild);
  195.                 Node *temp = Search(root,succesor);
  196.                 root->key = temp -> key;
  197.                 root-> rightChild = deleteNode( root->rightChild, temp->key);
  198.             }
  199.         }
  200.         return root;
  201.     }
  202.  
  203. };
  204.  
  205. int main()
  206. {
  207.     Tree t;
  208.     cout<<"How many value you want to insert: ";
  209.     int n;
  210.     cin>>n;
  211.     while(n--)
  212.     {
  213.         int key;
  214.         cin>>key;
  215.         t.insertt(key);
  216.     }
  217.     t.printInorder(t.root);
  218.     cout<<endl<<"which Value you want to search ";
  219.     int value;
  220.     cin>>value;
  221.  
  222.  
  223.     Node *find = t.Search( t.root,value);
  224.     if(find == NULL)
  225.     {
  226.         cout<<"Value is not found "<<endl;
  227.     }
  228.     else
  229.     {
  230.         cout<<find->key<<" is found "<<endl;
  231.     }
  232.  
  233.  
  234.  
  235.     cout<<endl<<"Minimum Value is: "<<t.minimum(t.root)<<endl;
  236.     cout<<endl<<"Maximum Value is: "<<t.maximum(t.root)<<endl;
  237.  
  238.     cout<<"which value successor you want to find: ";
  239.     cin>>value;
  240.     cout<<endl<<"Successor is: "<<t.successor(t.root, value );
  241.     cout<<"which data you want to delete";
  242.     cin>>value;
  243.     t.deleteNode(t.root,value);
  244.     cout<<endl;
  245.     t.printInorder(t.root);
  246.     return 0;
  247. }
  248. /// Tree Input 9 15 8 18 6 10 4 7 9 12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement