Binary Search Tree, Binary Search Tree height
To Write C++ code to implement a Binary Search Tree and find its height.

Binary Search tree is a binary tree in which each internal node x stores an element such that the element stored in the left sub-tree of x are less than or equal to x and elements stored in the right sub-tree of x are greater than or equal to x. Binary Search Tree can be implemented as a linked data structure in which each node is an object with two pointer fields and a data field. The two pointer fields left and right point to the nodes corresponding to the left child and the right child respectively. NIL in any pointer field signifies that there exists no corresponding child.


The height of the Binary Search Tree equals the number of links from the root node to the deepest node. 

Algorithm:
#include “BSTADT.h”
//BSTADT.h contains the code to implement the BST ADT
class BSTree : public BST<int>, public treenode<int>
{
private:
int l,r,h;
public:
int treeHeight(treenode<int> *tree);
};
//implementation of treeHeight function
int BSTree::treeHeight(treenode<int> *tree)
{
int l,r,h;
if (tree==NULL)
return 0;
else
{
l=treeHeight(tree->left);
r=treeHeight(tree->right);
h-maximum(l,r); //maximum of l and r
return h+1;
}
}


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments:

Thanks
Mukesh Rajput