Find Next Smallest & Greatest Key in BST

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Element
Click For Summary
SUMMARY

This discussion focuses on finding the next smallest and greatest keys in a Binary Search Tree (BST) using in-order traversal. The correct approach involves checking the right and left children of the node in question. Specifically, if a node has a right child, the next greatest key is the leftmost child of that right subtree. Conversely, if a node has a left child, the next smallest key is the rightmost child of that left subtree. Backtracking to parent nodes is necessary when the node lacks the appropriate children.

PREREQUISITES
  • Understanding of Binary Search Tree (BST) structure
  • Knowledge of in-order traversal methodology
  • Familiarity with tree node properties and relationships
  • Basic programming skills for implementing tree algorithms
NEXT STEPS
  • Implement the algorithm to find the next greatest key in a BST
  • Implement the algorithm to find the next smallest key in a BST
  • Explore tree traversal techniques beyond in-order, such as pre-order and post-order
  • Study balancing techniques for Binary Search Trees, such as AVL or Red-Black Trees
USEFUL FOR

Software developers, computer science students, and anyone interested in data structures and algorithms, particularly those working with Binary Search Trees.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

We consider a Binary search tree and we want to find the element with the next greatest and the next smallest key than the key of a node v.

For example, how can we find the next of the node to which q1 points in the in-order traversal?

How will we find the next of the node to which q2 points, in the in-order traversal?

View attachment 3877Will it be as follows?

If we have a pointer to the root, the element with the next smallest key will be the rightmost child of the left subtree of the root and the element with the next greatest key will be the leftmost child of the right subtree of the root.

If we have a pointer to a node v of which the key is smaller than this of the root, then the rightmost child of the left subtree of v will have the key with the next smallest element and the first right child will have the next greatest key.

If we have a pointer to a node v of which the key is greater than this of the root, then the rightmost child of the left subtree of v will have the key with the next smallest element and the leftmost child of the right subtree will have the next greatest key.

Or have I understood it wrong? (Thinking)
 

Attachments

  • pPUa3.png
    pPUa3.png
    5.5 KB · Views: 104
Last edited:
Technology news on Phys.org


Hello!

Your understanding of finding the next greatest and smallest keys in a binary search tree is generally correct. However, there are a few clarifications that can be made.

First, it is important to note that in-order traversal in a binary search tree follows the pattern of left subtree, current node, right subtree. So, in order to find the next greatest and smallest keys, we need to consider the nodes in the order of left, current, and then right.

In the case of q1, if the node to which it points has a right child, then the next greatest key will be the leftmost child of that right subtree. If the node does not have a right child, then we need to backtrack to its parent and check if it is a left child. If it is, then the parent node will have the next greatest key. If it is a right child, we need to backtrack further until we find a node that is a left child, and that node will have the next greatest key.

For q2, we need to follow a similar approach. If the node to which it points has a left child, then the next smallest key will be the rightmost child of that left subtree. If the node does not have a left child, then we need to backtrack to its parent and check if it is a right child. If it is, then the parent node will have the next smallest key. If it is a left child, we need to backtrack further until we find a node that is a right child, and that node will have the next smallest key.

I hope this helps clarify any confusion. Let me know if you have any further questions.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
Replies
14
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K