Largest BST in a Binary Tree in C++

In our school days, we all would have learned about family trees. We would have drawn a tree and written our grandparents names, parents names and then ours. We would have also seen how in the tree, our grandparents would be at the top, since they’re the oldest in the family.

Similarly, we have the concept of binary trees in the programming arena. Binary trees are a type of data structure where we can store data in a hierarchical order (oldest to youngest) and retrieve it easily. One interesting aspect that we will often learn in binary trees would be about binary search tree. 

In this informative blog, we will dig deep into binary search tree, the method to find the largest bst in binary tree and extra concepts to develop theoretical knowledge. 

This information will greatly benefit you if you work hard for an interview.

Without further ado, let’s check out what a binary tree is. 

What is a binary tree?

A binary tree is a data structure that is used to save data. It is utilized as an abstract framework that mimics an information hierarchy.

The tree is made up of nodes that represent the data. The leaf node is at the bottom of the tree, whereas the root node is at the top.

A typical tree data structure has an unlimited number of child and leaf nodes. Yet, when it comes to binary trees, each parent node cannot have more than two child nodes. There are also certain binary tree types, like, 

  • Root binary tree: The tree is made of root nodes and can have only two children nodes.
  • Complete binary tree: It has a root node and other nodes. Each node can have two child nodes. 

Speaking of binary tree, we ought to know its rich benefits, such as,

  • In contrast to arrays or linked lists, we can quickly insert and delete data in binary trees.
  • Binary trees provide for flexible data upkeep and traversal.
  • Numerous data-filled nodes may be conveniently maintained.
  • When compared to linked lists, the tree method is incredibly quick.
  • Data access is not difficult at all.

What is a binary search tree?

A binary search tree ( bst) arranges its components in a certain order. In a Binary search tree, the value of the left node should be lower than the value of the parent node, however, the parent node’s value must be lower to that of its right child node.  This rule is applied recursively to the root’s left and right subtrees.

Here’s an example

                 50

               /       \ 

             40       60

            /  \        /  \

         30  53   40  70

The root node in the above example is 50. If you see the left sub-tree that is 40, it is less than the root node (parent) but the right child node is 60 which is higher than the parent node. This satisfies the BST condition. 

Similarly, for the left child node 40, it has two children nodes 30 and 53. It also satisfies the bst condition. Likewise, the right child node 60’s other two children nodes 40 and 70 also are bst conditioned. 


How to search in a binary tree? 

Searching for a node in a Binary search tree is simple since items in BST are kept in a specified order. To search in a binary tree, you should follow the below steps.

  • Initially, compare the item to be searched with the tree’s root element.
  • Return the node’s position if the root matches the target element.
  • If it is not matched, test if the item is smaller than the root element; if it is, go to the left subtree.
  • If it exceeds the parent node, go to the right subtree.
  • Continue the preceding steps recursively until the result is discovered.
  • If the item cannot be identified or is not located in the tree, return NULL.

Approach to find the largest bst in binary tree

The largest bst in a binary tree denotes the bst within a tree that is comparatively larger than the remaining ones. 

To find that, here’s a problem statement through which will work in this way: 

Input: 

                  50

               /       \ 

             40       60

            /  \        /  \

         30  53   20  70

In this problem, the largest bst in the binary tree would start at the right subtree 60, and the size would be 3 (60, 20, and 70).

To find this, there is an easy method which is tree traversal. We will traverse the tree in the pre-order method. For every node that is encountered, we will check if the subtree that is placed at the node is actually a BST. If it is a binary search tree, we will compute and send it’s size (the subtree that is rooted to the node) as the output.

If it isn’t a BST, we will simply send the BST’s size (maximum) which was produced by the children subtrees as the output. 

In this approach, the time taken would be O(n2). Over here, n, denotes the BST’s size. This approach also needs space that is proportional to the height of the tree for the purpose of stack call.

Hence, if we traverse the tree in a bottom-up approach, where data passes between child nodes first and then to the parent, time can be reduced to O(n). 

Moreover, this will help us to find if the subtree at the nodes is constant time BST.

While doing this approach, always remember the properties of BST which are,

  • The left and right subtree of each node is considered the BST.
  • The reason is because the left subtree’s value will be smaller to that parent node. Right subtree will be larger than the parent node. 

If you follow this approach, you can find the largest BST in a binary tree. 


Extra Learning: What is the longest string chain?

Whenever there are subsequences of words within a string, they’re considered as a long string chain. In this problem, we can insert a character in the previous element of the string (except the first element) so that a new element will be formed. This process is repeated until we get the desired longest string chain. 


Conclusion

And it’s a wrap! We hope you have enjoyed reading about the tree-like data structure, its interesting features and other notable aspects. If you are preparing for an interview, the best tip from us would be to practice hard.

Keep practicing this problem until you are thorough with it. 

All the best!

About Author

Leave a Comment