Guest User

Untitled

a guest
Feb 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class node
  4. {
  5. int element1,element2,element3;
  6. node *left,*right,*middle,*parent;
  7. public:
  8. node()
  9. {
  10. element1=-1;
  11. element2=-1;
  12. element3=-1;
  13. }
  14. node *insert(node *,int);
  15. node *search(node*,int);
  16. void preorder(node*);
  17. void balance(node *);
  18. };
  19.  
  20. void node::balance(node* root)
  21. {
  22.     node *temp=new node;
  23. if(temp->parent==NULL)//this is the first node
  24.     {
  25.         node *temp1=new node;
  26.         node *temp2=new node;
  27.    
  28.  
  29.         temp1->parent=temp;//linking
  30.         temp1->left=NULL;
  31.         temp1->right=NULL;
  32.         temp1->middle=NULL;
  33.         temp2->parent=temp;//linking
  34.         temp2->left=temp->left;
  35.         temp2->right=NULL;
  36.         temp2->middle=NULL;//NO DUPLICATE VALUES ARE ACCEPTED
  37.         temp1->element1=temp->element1;
  38.         temp2->element1=temp->element3;
  39.         temp->element1=temp->element2;
  40.         temp->element1=-1;
  41.         temp->element3=-1;  
  42.         temp1->element1=-1;
  43.         temp1->element3=-1;
  44.         temp2->element1=-1;
  45.         temp2->element3=-1;
  46.         temp->left=temp1;
  47.         temp->right=temp2;
  48.          
  49.     }
  50.     else//when this is not the first node then we will get three nodes whenever child node splits
  51.     {
  52.        if(temp->parent->left!=NULL&&temp->right!=NULL)//we need to create the middle node
  53.        {
  54.         node *temp1=new node;
  55.      
  56.         int t2=temp->element3;
  57.  
  58.         temp1->parent=temp->parent;//linking
  59.         temp1->left=NULL;
  60.         temp1->right=NULL;
  61.         temp1->middle=NULL;
  62.         temp1->element1=t2;
  63.         temp1->element2=-1;
  64.         temp1->element3=-1;
  65.         temp->element3=-1;
  66.         temp->parent->middle=temp1;//linking
  67.         temp->parent->left=temp;//linking
  68.        
  69.        
  70.        
  71.        }       
  72.        else
  73.        {
  74.         node *temp1=new node;
  75.  
  76.         int t2=temp->element3;
  77.  
  78.         temp1->parent=temp->parent;//linking
  79.         temp1->left=NULL;
  80.         temp1->right=NULL;
  81.         temp1->middle=NULL;
  82.         temp1->element1=t2;
  83.         temp1->element2=-1;
  84.         temp1->element3=-1;
  85.         temp->element3=-1;
  86.         temp->parent->right=temp1;//linking
  87.         temp->parent->left=temp;//linking  
  88.            
  89.            
  90.        }
  91.      int t=temp->element2;
  92.      temp->element2=-1;  
  93.     insert(temp->parent,t); //here parent is not null so we have to send the item upwards
  94.     }        
  95. }
  96.  
  97. node* node::insert(node *root,int item)
  98. {
  99.     cout<<"\n inside the function \n";
  100. node *temp=new node;
  101. temp->parent=NULL;
  102. temp->left=NULL;
  103. temp->right=NULL;
  104. temp->middle=NULL;
  105. cout<<"\n all are initialised \n";     
  106. //if no node exist
  107.  if(root==NULL)
  108.  {
  109.      cout<<"\n  Value inserted \n";
  110.  temp->element1=item;
  111.  return temp;
  112.  }
  113.  
  114. temp=search(root,item);
  115. //when the node has only one node filled
  116.  if(temp->element1!=-1&&temp->element2==-1)
  117.  {
  118.  if(temp->element1<item)
  119.  temp->element2=item;
  120.  else
  121.    {
  122.    int t=temp->element1;
  123.    temp->element1=item;
  124.    temp->element2=t;//for sorting the 2 values
  125.    }
  126.  }
  127.  else if(temp->element1!=-1&&temp->element2!=-1)
  128.  {
  129.     if(item>temp->element1&&item<temp->element2)
  130.     {
  131.     temp->element3=temp->element2;
  132.     temp->element2=item;
  133.     }
  134.     else if(item<temp->element1)
  135.     {
  136.     temp->element3=temp->element2;
  137.     temp->element2=temp->element1;
  138.     temp->element1=item;  
  139.     }
  140.     else if(item>temp->element2)
  141.     temp->element3=item;
  142.    
  143.  
  144. //now calling the balance function
  145.  
  146.     balance(temp);
  147.  }//end of else if
  148.    
  149. return temp;   
  150. }//end of funciton
  151.  
  152. //to search the node where we have to insert
  153. node* node::search(node*root,int item)
  154. {
  155. node *temp=new node;
  156. temp=root;
  157. cout<<"\n inside the search function \n";
  158.     while(temp!=NULL)
  159.     {
  160.       if(temp->element1>item)
  161.       temp=temp->left;
  162.       else if(temp->element1<item)
  163.       {
  164.           if(temp->element2!=-1)
  165.           {
  166.               if(temp->element2>item)
  167.               temp=temp->middle;
  168.               else
  169.               temp=temp->right;
  170.           }
  171.       }
  172.      
  173.     } //end while
  174. return temp;//this is the node where value has to be inserted    
  175.    
  176. }
  177. //display
  178. void node::preorder(node *root){
  179.     if(root==NULL){
  180.         return;
  181.     }
  182.     cout<<root->element1;
  183.     if(root->element2!=-1)
  184.         cout<<root->element2;
  185.     preorder(root->left);
  186.         preorder(root->right);
  187.         preorder(root->middle);
  188.              
  189.  
  190. }
  191.  
  192.  
  193. int main(){
  194.     int n,ch;
  195. node *root=new node,*head=new node;
  196. root=NULL,head=NULL;
  197. node object;
  198.     while(1){
  199.         cout<<"\n1.INSERT \n2.DISPLAY \n3.EXIT\n";
  200.         cout<<"Enter Choice: ";
  201.             cin>>ch;
  202.  
  203.         switch(ch){
  204.         case 1:{
  205.             cout<<"\nEnter the element you want to insert \n";
  206.             cin>>n;
  207.             root=object.insert(root,n);
  208.             cout<<"\n function called \n";
  209.             if(head==NULL)
  210.             head=root;
  211.             break;
  212.         }
  213.         case 2:{
  214.             object.preorder(head);
  215.             break;
  216.         }
  217.         case 3: break;
  218.         }
  219.     }
  220.  
  221. return 0;
  222. }
Add Comment
Please, Sign In to add comment