Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with this question and thank you. Assume the following notation/oper

ID: 673373 • Letter: P

Question

Please help with this question and thank you.

Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has four 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). The balance factor T.bal is the difference between the height of T.left and that of T.right. Design an algorithm to find the second-smallest element in an AVL tree T. Give it in the form of a function SecondSmallest(T) which returns that key (not a pointer to it). You can assume T has at least two elements and that all its elements are different. The algorithm should not use any key comparisons, and it should perform as few operations as possible.

Explanation / Answer

1) To create standard BST insert for w.
2) The tree is strting from node w, travel up and find the first unbalanced node. Suppose z be the first unbalanced node, y be the child of z that comes on the path from w to z and x be the grandchild of z that comes on the path from w to z.
3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z. There can be 4 possible cases that needs to be handled as x, y and z can be arranged in 4 ways. Following are the possible 4 arrangements:
a) y is left child of z and x is left child of y (Left Left Case)
b) y is left child of z and x is right child of y (Left Right Case)
c) y is right child of z and x is right child of y (Right Right Case)
d) y is right child of z and x is left child of y (Right Left Case)

Following are the operations to be performed in above mentioned 4 cases. In all of the cases, we only need to re-balance the subtree rooted with z and the complete tree becomes balanced as the height of subtreerooted with z becomes same as it was before insertion.

a) Left Left Case:

Left Right Case:

Right Right Case

d) Right Left Case