Quiz 1
Consider the below left-left rotation pseudo code where the node contains value pointers to left, right child nodes and a height value and 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?
Height(w-left), x-height
What are the worst case and average case complexities of a binary search tree?
O(n), O(logn)
What does the following piece of code do? public void func(Tree root) { func(root.left()); func(root.right()); System.out.println(root.data()); }
Post-order traversal
What does the following piece of code do? public void func(Tree root) { System.out.println(root.data()); func(root.left()); func(root.right()); }
Preorder traversal
What maximum difference in heights between the leaves of an AVL tree is possible?
log(n) where n is the number of nodes
What is the maximum height of an AVL tree with p nodes?
log(p)
How will you find the maximum element in a binary search tree?
public void max(Tree root) { while(root.right() != null) { root = root.right(); } System.out.println(root.data()); }
To restore the AVL property after inserting an element, we start at the insertion point and move towards root of that tree. is this statement true?
true
What is the maximum height of any AVL-tree with 7 nodes? Assume that the height of a tree with a single node is 0.
- 3 Solution: For finding maximum height, the nodes should be minimum at each level. Assuming height as 2, 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 minimum 4 nodes. Assuming height as 3, 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 minimum 7 nodes. Therefore, using 7 nodes, we can achieve maximum height as 3. Following is the AVL tree with 7 nodes and height 3.
What is the specialty about the in-order traversal of a binary search tree?
It traverses in an increasing order
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 Does the above code can check if a binary search tree is an AVL tree?
NO
Which of the following is false about a binary search tree?
None of the mentioned - the following are all true 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
Which of the below diagram is following AVL tree property?
The property of AVL tree is it is height balanced tree with difference of at most 1 between left and right subtrees. - only 2
Which of the following is not an advantage of trees?
Undo/Redo operations in a notepad
What is an AVL tree?
a tree which is balanced and is a height balanced tree
True statements about 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(log 2 n)
Given an empty AVL tree, how would you construct AVL tree when a set of numbers are given without performing any rotations?
find the median of the set of elements given, make it as root and construct the tree
What is the time complexity for finding the height of the binary tree?
h = O(log n)