Find Next Smallest & Greatest Key in BST

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Element
In summary, in a binary search tree, the next greatest key is the leftmost child of the right subtree of a node if it exists, otherwise we need to backtrack to a node that is a left child. Similarly, the next smallest key is the rightmost child of the left subtree of a node if it exists, otherwise we need to backtrack to a node that is a right child.
  • #1
evinda
Gold Member
MHB
3,836
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: 37
Last edited:
Technology news on Phys.org
  • #2


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.
 

Related to Find Next Smallest & Greatest Key in BST

1. What is a BST?

A BST, or Binary Search Tree, is a data structure commonly used in computer science for organizing and storing data in a hierarchical manner. It is a type of binary tree where each node has at most two child nodes, a "left" and a "right" node. The left child node contains a key smaller than the parent node, while the right child node contains a key greater than the parent node. This structure allows for efficient searching, insertion, and deletion operations.

2. How do you find the next smallest key in a BST?

To find the next smallest key in a BST, you need to traverse the tree in a specific manner. Starting from the root node, if the current node has a left child, then the next smallest key will be the rightmost node in the left subtree. If the current node does not have a left child, then you need to traverse up the tree until you find a node that is a right child of its parent. The parent node of this node will be the next smallest key.

3. How do you find the next greatest key in a BST?

Finding the next greatest key in a BST is similar to finding the next smallest key. Starting from the root node, if the current node has a right child, then the next greatest key will be the leftmost node in the right subtree. If the current node does not have a right child, then you need to traverse up the tree until you find a node that is a left child of its parent. The parent node of this node will be the next greatest key.

4. What is the time complexity of finding the next smallest/greatest key in a BST?

The time complexity of finding the next smallest/greatest key in a BST is O(h), where h is the height of the tree. In a balanced BST, the height is approximately log(n), where n is the number of nodes in the tree. Therefore, the time complexity is O(log(n)). In an unbalanced BST, the time complexity can be as bad as O(n), where n is the number of nodes in the tree.

5. Is it possible to find the next smallest/greatest key in a BST in constant time?

No, it is not possible to find the next smallest/greatest key in a BST in constant time. As mentioned earlier, the time complexity is at least O(log(n)) in a balanced BST and can be as bad as O(n) in an unbalanced BST. This is because you need to traverse the tree to find the next smallest/greatest key, and the number of nodes you need to traverse depends on the height of the tree.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
15
Views
3K
  • Programming and Computer Science
Replies
20
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top