CS 435 Exam I Review sheet Spring 2022 Note: Learn to write algorithm and Pseudocode on your own. 1. Insert 21, 9, 28,4, 15, 26, 30, 14, 18, 10, 2, 37, 17 into the AVL Tree. 2. When node 50 will be deleted from the below tree, what will be the resultant AVL tree? 3. Insert 8, 18, 5, 15, 17, 25, 40 into the Red-Black Tree. Also, write the properties of the Red-Black Tree. 4. Insert 18, 95, 29, 34, 17, 4, 2, 89 into a Splay Tree. 5. Show how the Splaying techniques (Zig, Zag, Zig-Zag, Zag-Zig, Zig-Zig, ZagZag) works in Splay Trees Insertion and deletion with examples. Multiple Choice: 1. Consider the following AVL tree. Which of the following is an updated AVL tree after insertion of 70? a) b) c) d) None Answer: c 2. When dealing with splay trees the condition for the Zig-Zag step is when X is the left child of P, and P is the right child of G, or When X is the right child of P and P is the left child of G. a. True b. False 3. Consider the pseudo-code: int avl(binarysearchtree root): if(not root) return 0 left_tree_height = avl(left_of_root) if(left_tree_height== -1) return left_tree_height right_tree_height= avl(right_of_root) if(right_tree_height==-1) return right_tree_height Can the above code check if a binary search tree is an AVL tree? a) yes b) no Answer: b Explanation: The condition to check the height difference between the left and right subtrees is missing. if (absolute (left_tree_height – right_tree_height)>1) must be added. 4. Consider the below left-left rotation pseudo-code where the node contains value pointers to left, right child nodes, and a height value, and the Height () function returns height value stored at a particular node. avltree leftrotation(avltreenode z): avltreenode w =x-left x-left=w-right w-right=x x-height=max(Height(x-left),Height(x-right))+1 w-height=max(missing)+1 return w What is missing? a) Height(w-left), x-height b) Height(w-right), x-height c) Height(w-left), x d) Height(w-left) Answer: a Explanation: In the code, we are trying to make the left rotation and so we need to find the maximum of those two values. 5. What is an AVL tree? a) a tree which is balanced and is a height balanced tree b) a tree that is unbalanced and is a height balanced tree c) a tree with three children d) a tree with at most 3 children Answer: a Explanation: It is a self-balancing tree with a height difference of at most 1. 6. Which of the below diagram is following the AVL tree property? i. ii. a) only i b) only i and ii c) only ii d) none of the mentioned Answer: c Explanation: The property of the AVL tree is it is a height-balanced tree with a difference of at most 1 between left and right subtrees. 7. To restore the AVL property after inserting an element, we start at the insertion point and move towards the root of that tree. Is this statement true? a) true b) false Answer: a Explanation: It is interesting to note that after insertion, only the path from that point to node or only those subtrees are imbalanced in terms of height. 8. What is the time complexity for finding the height of the binary tree? a) h = O(1) b) h = O(nlogn) c) h = O(n) d) h = O(log n) Answer: d Explanation: The nodes are either a part of the left subtree or the right subtree, so we don’t have to traverse all the nodes, this means the complexity is lesser than n, in the average case, assuming the nodes are spread evenly, the time complexity becomes O(n). 9. Which of the following is not an advantage of trees? a) Hierarchical structure b) Faster search c) Router algorithms d) Undo/Redo operations in a notepad Answer: d Explanation: This is an application of stack. 10. Which of the following is false about a binary search tree? a) The left child is always lesser than its parent b) The right child is always greater than its parent c) The left and right sub-trees should also be binary search trees d) None of the mentioned Answer: d Explanation: All the options hold good for a binary search tree and can be considered as a definition for a BST. 11. How to search for a key in a binary search tree? a) public Tree search (Tree root, int key) { if( root == null || root.key == key ) { return root; } if( root.key < key ) { return search(root.right,key); } else return search(root.left,key); } b) public Tree search(Tree root, int key) { if( root == null || root.key == key ) { return root; } if( root.key < key ) { return search(root.left,key); } else return search(root.right,key); } c) public Tree search(Tree root, int key) { if( root == null) { return root; } if( root.key < key ) { return search(root.right,key); } else return search(root.left,key); } d) None of the mentioned Answer: a Explanation: As we know that the left child is lesser than the parent if the root’s key is greater than the given key, we look only into the left sub-tree, similarly for the right sub-tree. 12. What is the maximum height of an AVL tree with 7 nodes? Assume that the height of a tree with a single node is 0. a) 2 b) 3 c) 4 d) 5 Solution: For finding maximum height, the nodes should be minimum at each level. Assuming height as 2, the minimum number of nodes required: N(h) = N(h-1) + N(h-2) + 1 N(2) = N(1) + N(0) + 1 = 2 + 1 + 1 = 4. It means height 2 is achieved using a minimum of 4 nodes. Assuming height as 3, the minimum number of nodes required: N(h) = N(h-1) + N(h-2) + 1 N(3) = N(2) + N(1) + 1 = 4 + 2 + 1 = 7. It means height 3 is achieved using a minimum of 7 nodes. Therefore, using 7 nodes, we can achieve a maximum height of 3. Following is the AVL tree with 7 nodes and height 3. 14. True statements about the AVL tree are a) It is a binary search tree. b) Left node and right node differ in height by at most 1 unit c) Worst case time complexity is O(log2n) d) Worst case time complexity is O(n) Answer: a, b and c 13. Why prefer splay trees? a) easier to program b) space efficiency c) easier to program and faster access to recently accessed items d) quick searching Answer: c Explanation: Whenever you insert an element or remove or read an element that will be pushed or stored at the top which facilitates easier access or recently used stuff. 15. Which of the following options is an application of splay trees? a) cache Implementation b) networks c) send values d) receive values Answer: a Explanation: Splay trees can be used for faster access to recently accessed items and hence used for cache implementations.