We are given a red-black tree T containing n keys and two keys X and Y, key X is stored in node x, wbhile key Y is stored in node y. Give an effective algorithm that determines the smallest key value stored on the path connecting x and y in T. The running time should be O(log n).
Solution
Algorithm
At first we have to find LCA (Least common ancestor) for x and y
nodes, let's call this node L. We can find L using standard search
algorithm for node. Node is least common ancestor of nodes x and y
when (L->key >= X->key and L->key <= Y->key) or
when (L->key <= X->key and L->key >= Y->key),
finding LCA in red-black tree is similar to standard finding
operation and it takes O(log N) time. After fining L we need to
check smallest value on roads between L and X and on road between L
and Y. Smallest key will be stored in node which will be the node
before first right turn (See the Picture).
The LCA will be smallest value on the road from LCA to biggest from X, Y. We can find smallest value for another node with simple traversal and compare it to LCA, see the pseudo code.
Pseudo Code:
#Lets store smaller value in X
If X.key > Y.key
swap X, Y
#search for LCA
L = root
while true
#If L has LCA characteristics we stop
If L.key >= X.key && L.key <= Y.key
break
#If it bigger than X and Y we search in Left subtree
if L.key > X.key
L = L.left
#else we search in right subtree
else
L = L.right
#save LCA key as answer
ans = L.key
#while there is left turn while traversing to X
while L.key > X.key
L = L.left
#compare answer to leftmost node's key
ans = minimum(ans, L.key)
return ans
THANKS.
I hope this helps if you find any problem. Please comment below.
Don't forget to give a thumbs up if you liked it. :)
We are given a red-black tree T containing n keys and two keys X and Y,...
2) a) Consider a modified red-black tree which satisfies all the usual conditions for a red-black tree exceptthat the root may be either black or red. Suppose a modified red-black tree T has a red root node. If we recolor the root node black, is the resulting tree a “normal” red-black tree? b) What is the largest and smallest number of nodes in a red-black tree with black-height k?
1.Fix any tree T on 10 vertices. Draw the recursion tree of the algorithm Find-size-node when run on the input T with a being the root of T. Can you use this to give a bound on the running time of T? 2. Consider the following problem. Check-BST • Input: A binary tree T • Output: 1 if T is a binary search tree, and 0 otherwise. Give an efficient algorithm for this problem. 3.Give a recursive algorithm for the...
Let T be a heap storing n keys. Give an efficient algorithm for reporting all the keys in T that are smaller than or equal to a given query key x (which is not necessarily in T). For example, given the heap of Figure 5.6 and query key x = 7, the algorithm should report 4, 5, 6, 7. Note that the keys do not need to be reported in sorted order. Ideally, your algorithm should run in O(k) time,...
fill in the blank Binary Search Tree AVL Tree Red-Black Tree complexity O(log N), O(N) in the worst case O(log N) O(log N) Advantages - Increasing and decreasing order traversal is easy - Can be implemented - The complexity remains O(Log N) for a large number of input data. - Insertion and deletion operation is very efficient - The complexity remains O(Log N) for a large number of input data. Disadvantages - The complexity is O(N) in the worst case...
(20 points) Suppose you are given a binary search tree T of n nodes (as discussed in class. each node v has v.left, v.right, and v.key). We assume that no two keys in T are equal. Given a value x, the rank operation rank() is to return the rank of x in T, which is defined to be one plus the number of keys of T smaller than 2. For example, if T has 3 keys smaller than r, then...
3. [5 marks] Suppose T is a binary tree that stores an integer key value in each node. Assume the following notation/operations on a binary tree. » the key T.key is the root node's integer key value . the left child T.left is T's left subtree, which is possibly an empty tree (or null) the right child T.right is T's right subtree, which is possibly an empty tree (or null) (a) Write an efficient algorithm FINDMAxPrODuCT(T) in pseudocode that returns...
In the lectures, we studied binary heaps. A min-Heap can be visualized as a binary tree of height with each node having at most two children with the property that value of a node is at most the value of its children. Such heap containing n elements can be represented (stored) as an array with the property Suppose that you would like to construct a & min Heap: each node has at most& children and the value of a node...
Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes The key T.key is the root node's key. The left child T.left is Ts left subtree, which is an AVL tree (possibly E). The right child T.right is T's right subtree, which is an AVL tree (possibly E). (a) 5 marsl Write a function RANGECOUNT(T, lo, hi) to count the number of nodes in an AVL tree with...
Weird recursion tree analysis. Suppose we have an algorithm that on problems of size n, recursively solves two problems of size n/2, with a “local running time” bounded by t(n) for some function t(n). That is, the algorithm’s total running time T(n) satisfies the recurrence relation T(n) ≤ 2T(n/2) + t(n). For simplicity, assume that n is a power of 2. Prove the following using a recursion tree analysis (a) If t(n) = O(n log n), then T(n) = O(n(log...
Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes: • The key T.key is the root node’s key. • The left child T.left is T’s left subtree, which is an AVL tree (possibly E). • The right child T.right is T’s right subtree, which is an AVL tree (possibly E). (a) [5 marks] Write a function RangeCount(T, lo, hi) to count the number of nodes in an...