m2skills

bst height cpp

May 31st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. // program to find the height of binary search tree
  2.  
  3. #include<iostream>
  4.  
  5. using namespace std;
  6.  
  7. class node{
  8.     protected:
  9.         int element;
  10.         node* left;
  11.         node* right;
  12.        
  13.     public:
  14.        
  15.         //constructor that accepts only element
  16.         node(int element){
  17.             this->element = element;
  18.             this->left = NULL;
  19.             this->right = NULL;
  20.         }
  21.        
  22.         //constructor that accepts element, left Link, Right Link
  23.         node(int element, node* leftLink, node* rightLink){
  24.             this->element = element;
  25.             this->left = leftLink;
  26.             this->right = rightLink;
  27.         }
  28.  
  29.         //method to update data of the node
  30.         void updateData(int element){
  31.             this->element = element;
  32.         }
  33.  
  34.         //method to update the left Link of the node
  35.         void updateLeftLink(node* temp){
  36.             this->left = temp;
  37.         }
  38.  
  39.         //method to update the right link of the node
  40.         void updateRightLink(node* temp){
  41.             this->right = temp;
  42.         }
  43.    
  44.         //method that returns the element of the node
  45.         int getElement(){
  46.             return this->element;
  47.         }
  48.  
  49.         //method that returns left Link
  50.         node* getLeftNode(){
  51.             return this->left;
  52.         }
  53.  
  54.         //method that returns the right Link
  55.         node* getRightNode(){
  56.             return this->right;
  57.         }
  58. };
  59.  
  60. //binary search tree class
  61. class BST{
  62.     protected:
  63.         node* root;
  64.        
  65.     public:
  66.        
  67.         //constructor for bst
  68.         BST(){
  69.             root = NULL;
  70.         }
  71.  
  72.         //returns true if the root node is null
  73.         bool isEmpty(){
  74.             return(root == NULL);
  75.         }
  76.        
  77.         //returns root node
  78.         node* getRoot(){
  79.             return root;
  80.         }
  81.        
  82.         void insert(int element){          
  83.             node* temp = new node(element);
  84.             //if tree is empty put it at root node
  85.             if(root == NULL){
  86.                 root = temp;
  87.             }
  88.             else{
  89.                 bool inserted = false;
  90.                 //creating a node pointer to traverse the tree
  91.                 node* p = root;
  92.  
  93.                 //keep looping while the node is not inserted
  94.                 while(not inserted){
  95.                     //if element of the new node is less than the current node than insert it to the left
  96.                     if(p->getElement() > temp->getElement()){
  97.                         if(p->getLeftNode() == NULL){
  98.                             p->updateLeftLink(temp);
  99.                             inserted = true;
  100.                         }
  101.                         else{
  102.                             p = p->getLeftNode();
  103.                         }
  104.                     }
  105.                     //if element of the new node is greater than the current node than insert it to the right
  106.                     else if(p->getElement() < temp->getElement()){
  107.                         if(p->getRightNode() == NULL){
  108.                             p->updateRightLink(temp);
  109.                             inserted = true;
  110.                         }
  111.                         else{
  112.                             p = p->getRightNode();
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.        
  119.        
  120.         // method to find the height of bst
  121.         // in this method q always marks the root node
  122.         int height(node *p)
  123.         {
  124.             //
  125.             static int pHeight=0,tempHeight=0,x=0;
  126.             // if the node is null then return
  127.             if(p==NULL)
  128.             {
  129.                 return 0;
  130.             }
  131.             else
  132.             {
  133.  
  134.                 //else first traverse to the left
  135.                 x=height(p->getLeftNode());
  136.                
  137.                 tempHeight++;
  138.                 // if we are back at the root node marked by q
  139.                 // then check if the depthtree is less that deep
  140.                 if(p==this->getRoot())
  141.                 {
  142.                     if(pHeight < tempHeight)
  143.                     {
  144.                         pHeight = tempHeight;
  145.                         tempHeight = 0;
  146.                     }
  147.                 }
  148.                 x=height(p->getRightNode());
  149.  
  150.             }
  151.             return pHeight+1;
  152.         }
  153.        
  154. };
  155.  
  156. int main()
  157. {
  158.     BST b1;
  159.     b1.insert(7);
  160.     b1.insert(29);
  161.     b1.insert(25);
  162.     b1.insert(36);
  163.     b1.insert(71);
  164.     b1.insert(24);
  165.     b1.insert(5);
  166.     b1.insert(9);
  167.     b1.insert(1);
  168.  
  169.     node* root = b1.getRoot();
  170.  
  171.     cout<<"The Root element of the BST is : "<<root->getElement()<<endl;
  172.  
  173.     cout<<"\n\nThe Height of the BST is : "<<b1.height(b1.getRoot());
  174.  
  175. }
Add Comment
Please, Sign In to add comment