m2skills

bst cpp

May 31st, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.54 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class node{
  6.     protected:
  7.         int element;
  8.         node* left;
  9.         node* right;
  10.        
  11.     public:
  12.        
  13.         //constructor that accepts only element
  14.         node(int element){
  15.             this->element = element;
  16.             this->left = NULL;
  17.             this->right = NULL;
  18.         }
  19.        
  20.         //constructor that accepts element, left Link, Right Link
  21.         node(int element, node* leftLink, node* rightLink){
  22.             this->element = element;
  23.             this->left = leftLink;
  24.             this->right = rightLink;
  25.         }
  26.  
  27.         //method to update data of the node
  28.         void updateData(int element){
  29.             this->element = element;
  30.         }
  31.  
  32.         //method to update the left Link of the node
  33.         void updateLeftLink(node* temp){
  34.             this->left = temp;
  35.         }
  36.  
  37.         //method to update the right link of the node
  38.         void updateRightLink(node* temp){
  39.             this->right = temp;
  40.         }
  41.    
  42.         //method that returns the element of the node
  43.         int getElement(){
  44.             return this->element;
  45.         }
  46.  
  47.         //method that returns left Link
  48.         node* getLeftNode(){
  49.             return this->left;
  50.         }
  51.  
  52.         //method that returns the right Link
  53.         node* getRightNode(){
  54.             return this->right;
  55.         }
  56. };
  57.  
  58. //binary search tree class
  59. class BST{
  60.     protected:
  61.         node* root;
  62.        
  63.     public:
  64.        
  65.         //constructor for bst
  66.         BST(){
  67.             root = NULL;
  68.         }
  69.  
  70.         //returns true if the root node is null
  71.         bool isEmpty(){
  72.             return(root == NULL);
  73.         }
  74.        
  75.         //returns root node
  76.         node* getRoot(){
  77.             return root;
  78.         }
  79.        
  80.         void insert(int element){          
  81.             node* temp = new node(element);
  82.             //if tree is empty put it at root node
  83.             if(root == NULL){
  84.                 root = temp;
  85.             }
  86.             else{
  87.                 bool inserted = false;
  88.                 //creating a node pointer to traverse the tree
  89.                 node* p = root;
  90.  
  91.                 //keep looping while the node is not inserted
  92.                 while(not inserted){
  93.                     //if element of the new node is less than the current node than insert it to the left
  94.                     if(p->getElement() > temp->getElement()){
  95.                         if(p->getLeftNode() == NULL){
  96.                             p->updateLeftLink(temp);
  97.                             inserted = true;
  98.                         }
  99.                         else{
  100.                             p = p->getLeftNode();
  101.                         }
  102.                     }
  103.                     //if element of the new node is greater than the current node than insert it to the right
  104.                     else if(p->getElement() < temp->getElement()){
  105.                         if(p->getRightNode() == NULL){
  106.                             p->updateRightLink(temp);
  107.                             inserted = true;
  108.                         }
  109.                         else{
  110.                             p = p->getRightNode();
  111.                         }
  112.                     }
  113.                 }
  114.             }
  115.         }
  116.        
  117.         // method to delete a element from the bst
  118.         // initially tempNode is assigned as the root node
  119.         bool deleteElement(int element,node* tempNode){
  120.            
  121.             static bool removed = false;
  122.             static node* parent;
  123.             int data = 0;
  124.             if(tempNode == NULL){
  125.                 return false;
  126.             }
  127.             data = tempNode->getElement();
  128.            
  129.             // if data is less than the node data then traverse left
  130.             if(data > element){
  131.                 parent = tempNode;
  132.                 tempNode = tempNode->getLeftNode();
  133.                 deleteElement(element,tempNode);
  134.             }
  135.             // if data greater than the node data then traverse right
  136.             if(data < element){
  137.                 parent = tempNode;
  138.                 tempNode = tempNode->getRightNode();
  139.                 deleteElement(element,tempNode);
  140.             }
  141.             // if data is equal to the node data
  142.             if(data == element){
  143.            
  144.                 // deleting a leaf node
  145.                 if(tempNode->getLeftNode() == NULL && tempNode->getRightNode() == NULL){
  146.                    
  147.                     if(parent->getElement() > tempNode->getElement()){
  148.                         parent->updateLeftLink(NULL);
  149.                     }
  150.                    
  151.                     else if(parent->getElement() < tempNode->getElement()){
  152.                         parent->updateRightLink(NULL);
  153.                     }
  154.                     delete tempNode;
  155.                 }
  156.                
  157.                 // deleting a node with left subtree
  158.                 else if(tempNode->getLeftNode() != NULL && tempNode->getRightNode() == NULL){
  159.  
  160.                     if(parent->getElement() > tempNode->getElement()){
  161.                         parent->updateLeftLink(tempNode->getLeftNode());
  162.                     }
  163.  
  164.                     else if(parent->getElement() < tempNode->getElement()){
  165.                         parent->updateRightLink(tempNode->getLeftNode());
  166.                     }
  167.                     delete tempNode;
  168.                 }
  169.                
  170.                 // deleting a node with right subtree
  171.                 else if(tempNode->getLeftNode() == NULL && tempNode->getRightNode() != NULL){
  172.                     if(parent->getElement() > tempNode->getElement()){
  173.                         parent->updateLeftLink(tempNode->getRightNode());
  174.                     }
  175.  
  176.                     else if(parent->getElement() < tempNode->getElement()){
  177.                         parent->updateRightLink(tempNode->getRightNode());
  178.                     }
  179.                     delete tempNode;
  180.                 }
  181.  
  182.                 //deleting root node
  183.                 else if(tempNode->getLeftNode() != NULL && tempNode->getRightNode() != NULL && tempNode->getElement() == root->getElement()){
  184.  
  185.                     node* q = tempNode->getLeftNode();
  186.                     node* r = tempNode->getRightNode();
  187.                     root = r;
  188.                     while(r->getLeftNode()!= NULL){
  189.                         r = r->getLeftNode();
  190.                     }
  191.                     r->updateLeftLink(q);
  192.                     delete tempNode;
  193.                 }
  194.  
  195.                 // deleting a node with both left and right subtree
  196.                 else if(tempNode->getLeftNode() != NULL && tempNode->getRightNode() != NULL){
  197.  
  198.                     if(parent->getElement() > tempNode->getElement()){
  199.                         parent->updateLeftLink(tempNode->getRightNode());
  200.                     }
  201.  
  202.                     else if(parent->getElement() < tempNode->getElement()){
  203.                         parent->updateRightLink(tempNode->getRightNode());
  204.                     }
  205.  
  206.                     node* q = tempNode->getLeftNode();
  207.                     node* r = tempNode->getRightNode();
  208.                     while(r->getLeftNode()!= NULL){
  209.                         r = r->getLeftNode();
  210.                     }
  211.                     r->updateLeftLink(q);
  212.  
  213.                     delete tempNode;
  214.  
  215.                 }
  216.             }
  217.             return removed;
  218.         }
  219.  
  220.         // method that returns true if the data is present in the bst
  221.         bool search(int data){
  222.             bool found = false;
  223.            
  224.             // creating a temp node to traverse the binary tree
  225.             node* tempNode = root;
  226.             while(tempNode != NULL){
  227.            
  228.                 // if data is less than the node element then search on the left hand side
  229.                 if(data < tempNode->getElement()){
  230.                     tempNode = tempNode->getLeftNode();
  231.                 }
  232.                 // if data is greater than the node element search on the right hand side
  233.                 else if(data > tempNode->getElement()){
  234.                     tempNode = tempNode->getRightNode();
  235.                 }
  236.                 // if data is equal to the node element then return true
  237.                 else if(data == tempNode->getElement()) {
  238.                     found = true;
  239.                     cout<<"\n\nThe Data "<<data<<" is found in the BST.";
  240.                     return true;
  241.                 }
  242.             }
  243.             // if data is not found then return false
  244.             if(not found){
  245.                 cout<<"\n\nThe Data "<<data<<" is not found in the BST.";
  246.                 return false;
  247.             }
  248.         }
  249.        
  250.         // method that returns max element of the binary search tree
  251.         // max element appears in the right most node of the bst
  252.         int Max(){
  253.             if(isEmpty()){
  254.                 cout<<"The tree is empty!!";
  255.                 return -99999;      // assuming -99999 never appeares in the tree
  256.             }
  257.             else{
  258.                 // creating a temp node to traverse the tree
  259.                 node* tempNode = root;
  260.                 // keep traversing to the right node of the bst
  261.                 while(tempNode->getRightNode() != NULL){
  262.                     tempNode = tempNode->getRightNode();
  263.                 }
  264.                 return tempNode->getElement();
  265.             }
  266.         }
  267.        
  268.         // method that returns min element of the binary search tree
  269.         // max element appears in the left most node of the bst
  270.         int Min(){
  271.             if(isEmpty()){
  272.                 cout<<"The tree is empty!!";
  273.                 return 99999;       // assuming 99999 never appeares in the tree
  274.             }
  275.             else{
  276.                 // creating a temp node to traverse the tree
  277.                 node* tempNode = root;
  278.                 // keep traversing to the left node of the bst
  279.                 while(tempNode->getLeftNode() != NULL){
  280.                     tempNode = tempNode->getLeftNode();
  281.                 }
  282.                 return tempNode->getElement();
  283.             }
  284.         }
  285.        
  286.         // recursive program to display tree using Preorder traversal
  287.         void displayPreorder(node* n){
  288.            
  289.             if(n == NULL){
  290.                 return;
  291.             }
  292.            
  293.             cout<<n->getElement()<<"  ";
  294.             displayPreorder(n->getLeftNode());
  295.             displayPreorder(n->getRightNode());
  296.            
  297.         }
  298.        
  299.         // recursive program to display tree using Inorder traversal
  300.         void displayInorder(node* n){
  301.            
  302.             if(n == NULL){
  303.                 return;
  304.             }
  305.            
  306.             displayInorder(n->getLeftNode());
  307.             cout<<n->getElement()<<"  ";
  308.             displayInorder(n->getRightNode());
  309.            
  310.         }
  311.        
  312.         // recursive program to display tree using Postorder traversal
  313.         void displayPostorder(node* n){
  314.            
  315.             if(n == NULL){
  316.                 return;
  317.             }
  318.            
  319.             displayPostorder(n->getLeftNode());
  320.             displayPostorder(n->getRightNode());
  321.             cout<<n->getElement()<<"  ";
  322.         }
  323.        
  324.         // method to display all the leaf nodes
  325.         // using inorder traversal to traverse the tree and print all leaf nodes
  326.         void displayLeafNodes(node* n){
  327.             if(n == NULL){
  328.                 return;
  329.             }
  330.            
  331.             displayLeafNodes(n->getLeftNode());
  332.            
  333.             // leaf node has both left and right links as null
  334.             // checking if both links are null then it is a leaf node
  335.             if(n->getLeftNode() == NULL && n->getRightNode() == NULL){
  336.                 cout<<n->getElement()<<"  ";
  337.  
  338.             }
  339.            
  340.             displayLeafNodes(n->getRightNode());
  341.         }
  342.        
  343.         // method to find the common ancestor of 2 data elements
  344.         // both the elements passed to the method must be present in the BST
  345.         void commonAncestor(node* n, int e1, int e2){
  346.            
  347.             int data = n->getElement();
  348.             static bool printParent = false;
  349.             // if both are greater than the node data then traverse right
  350.             if(data < e1 && data < e2){
  351.                 commonAncestor(n->getRightNode(),e1,e2);
  352.             }
  353.            
  354.             // if both are less than node data then traverse left
  355.             else if(data > e1 && data > e2){
  356.                 commonAncestor(n->getLeftNode(),e1,e2);
  357.             }
  358.  
  359.             // if any element is equal to data then the parent element is the common ancestor
  360.             else if(data == e1 || data == e2){
  361.                 printParent = true;
  362.                 return;
  363.             }
  364.             // if both above cases are not satisfied then the current node is the common ancestor node
  365.             else{
  366.                 cout<<"\n\nThe Local common ancestor for "<<e1<<" and "<<e2<<" is : "<<n->getElement();
  367.                 return;
  368.             }
  369.  
  370.             if(printParent == true){
  371.                 cout<<"\n\nThe Local common ancestor for "<<e1<<" and "<<e2<<" is : "<<n->getElement();
  372.                 // reset static printParent variable
  373.                 printParent = false;
  374.             }
  375.            
  376.         }
  377.        
  378.         // method to find the height of bst
  379.         // in this method q always marks the root node
  380.         int height(node *p)
  381.         {
  382.             //
  383.             static int pHeight=0,tempHeight=0,x=0;
  384.             // if the node is null then return
  385.             if(p==NULL)
  386.             {
  387.                 return 0;
  388.             }
  389.             else
  390.             {
  391.  
  392.                 //else first traverse to the left
  393.                 x=height(p->getLeftNode());
  394.                
  395.                 tempHeight++;
  396.                 // if we are back at the root node marked by q
  397.                 // then check if the depthtree is less that deep
  398.                 if(p==this->getRoot())
  399.                 {
  400.                     if(pHeight < tempHeight)
  401.                     {
  402.                         pHeight = tempHeight;
  403.                         tempHeight = 0;
  404.                     }
  405.                 }
  406.                 x=height(p->getRightNode());
  407.  
  408.             }
  409.             return pHeight+1;
  410.         }
  411.        
  412.         void printNodeAtDistance(node* p,int distance, int count){
  413.  
  414.             if(p == NULL){
  415.                 return;
  416.             }
  417.  
  418.             if(count > distance){
  419.                 return;
  420.             }
  421.             if(count == distance){
  422.                 cout<<p->getElement()<<"  ";
  423.             }
  424.             printNodeAtDistance(p->getLeftNode(),distance,count+1);
  425.             printNodeAtDistance(p->getRightNode(),distance,count+1);
  426.         }
  427.  
  428.         // method to delete the largest element of the tree
  429.         void deleteMax(){
  430.             int element = this->Max();
  431.             this->deleteElement(element, this->getRoot());
  432.         }
  433.  
  434.         // method to delete the smallest element of the tree
  435.         void deleteMin(){
  436.             int element = this->Min();
  437.             this->deleteElement(element, this->getRoot());
  438.         }
  439. };
  440.  
  441. int main()
  442. {
  443.     BST b1;
  444.     b1.insert(7);
  445.     b1.insert(29);
  446.     b1.insert(25);
  447.     b1.insert(36);
  448.     b1.insert(71);
  449.     b1.insert(24);
  450.     b1.insert(5);
  451.     b1.insert(9);
  452.     b1.insert(1);
  453.  
  454.     node* root = b1.getRoot();
  455.     cout<<"The Root element of the BST is : "<<root->getElement()<<endl;
  456.  
  457.     cout<<"\nThe Preorder traversal of the BST is : ";
  458.     b1.displayPreorder(root);
  459.  
  460.     cout<<"\n\nThe Postorder traversal of the BST is : ";
  461.     b1.displayPostorder(root);
  462.  
  463.     cout<<"\n\nThe Inorder traversal of the BST is : ";
  464.     b1.displayInorder(root);
  465.  
  466.     cout<<"\n\nThe Leaf nodes of the BST are : ";
  467.     b1.displayLeafNodes(root);
  468.  
  469.     cout<<"\n\nThe Largest element of the BST is : "<<b1.Max();
  470.  
  471.  
  472.     cout<<"\n\nThe Smallest element of the BST is : "<<b1.Min();
  473.  
  474.     //searching an element in the Linked List
  475.     b1.search(24);
  476.     b1.search(77);
  477.  
  478.     cout<<"\n\nThe Height of the BST is : "<<b1.height(b1.getRoot());
  479.  
  480.     b1.commonAncestor(root,24,5);
  481.     b1.commonAncestor(root,1,5);
  482.     b1.commonAncestor(root,25,36);
  483.  
  484.     cout<<"\n\nThe Elements at a distance of 2 from the root of the BST are : ";
  485.     b1.printNodeAtDistance(root,2,0);
  486.  
  487.     //deleting leaf element
  488.     b1.deleteElement(1,root);
  489.  
  490.     //deleting root node
  491.     b1.deleteElement(7,root);
  492.  
  493.     cout<<"\n";
  494.     root = b1.getRoot();
  495.     b1.displayPreorder(root);
  496.  
  497.     b1.deleteMin();
  498.     b1.deleteMax();
  499.     cout<<"\n\n";
  500.     b1.displayPreorder(root);
  501. }
Add Comment
Please, Sign In to add comment