- #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)
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
Last edited: