quiz 2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Consider the below formations of red-black tree. All the above formations are incorrect for it to be a red-black tree. then what may be the correct order? a) 50-black root, 18-red left subtree, 100-red right subtree b) 50-red root, 18-red left subtree, 100-red right subtree c) 50-black root, 18-black left subtree, 100-red right subtree d) 50-black root, 18-red left subtree, 100-black right subtree

a) 50-black root, 18-red left subtree, 100-red right subtree Explanation: Considering all the properties of red-black tree, 50 must be the black root and there are two possibilities for subtrees. one is option a and other is making all nodes of the tree to be black.

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? a) Height(w-left), x-height b) Height(w-right), x-height c) Height(w-left), x d) Height(w-left)

a) Height(w-left), x-height Explanation: In the code we are trying to make the left rotation and so we need to find maximum of those two values.

What does the following piece of code do? public void func(Tree root) { System.out.println(root.data()); func(root.left()); func(root.right()); } a) Preorder traversal b) In-order traversal c) Post-order traversal

a) Preorder traversal Explanation: In a preorder traversal, first the parent is visited, then the left child and finally the right child.

What is the special property of red-black trees and what root should always be? a) a color which is either red or black and root should always be black color only b) height of the tree c) pointer to next node d) a color which is either green or black

a) a color which is either red or black and root should always be black color only Explanation: An extra attribute which is a color red or black is used. root is black because if it is red then one of red-black tree property which states that number of black nodes from root to null nodes must be same, will be violated.

What is an AVL tree? a) a tree which is balanced and is a height balanced tree b) a tree which is unbalanced and is a height balanced tree c) a tree with three children d) a tree with at-most 3 children

a) a tree which is balanced and is a height balanced tree Explanation: It is a self-balancing tree with height difference at-most 1.

What are the operations that could be performed in O(logn) time complexity by red-black tree? a) insertion, deletion, finding predecessor, successor b) only insertion c) only finding predecessor, successor d) for sorting

a) insertion, deletion, finding predecessor, successor Explanation: We impose restrictions (refer question 2) to achieve logarithm time complexities.

What maximum difference in heights between the leaves of an AVL tree is possible? a) log(n) where n is the number of nodes b) n where n is the number of nodes c) 0 or 1 d) at-most 1

a) log(n) where n is the number of nodes Explanation: At every level we can form a tree with difference in height between subtrees to be at-most 1 and so there can be log(n) such levels since height of AVL tree is log(n).

When to choose Red-Black tree, AVL tree? a) many inserts, many searches and when managing more items respectively b) many searches, when managing more items respectively and many inserts respectively c) sorting, sorting and retrieval respectively d) retrieval, sorting and retrieval respectively

a) many inserts, many searches and when managing more items respectively Explanation: Red black when frequent inserts and deletes, AVL when less frequent inserts and deletes.

Why we need to a binary tree which is height balanced? a) to avoid formation of skew trees b) to save memory c) to attain faster memory access d) to simplify storing

a) to avoid formation of skew trees Explanation: In real world dealing with random values is often not possible, the probability that u are dealing with non-random values (like sequential) leads to mostly skew trees, which leads to worst case. Hence we make height balance by rotations.

Why do we impose restrictions like . root property is black . every leaf is black . children of red node are black . all leaves have same black a) to get logarithm time complexity b) to get linear time complexity c) to get exponential time complexity d) to get constant time complexity

a) to get logarithm time complexity Explanation: We impose such restrictions to achieve self-balancing trees with logarithmic complexities for insertions, deletions, search.

How can you save memory when storing color information in Red-Black tree? a) using least significant bit of one of the pointers in the node for color information b) using another array with colors of each node c) storing color information in the node structure d) using negative and positive numbering

a) using least significant bit of one of the pointers in the node for color information. Explanation: The node pointers can be used to store color with the help of significant bits. the exceptions of this method are in languages like java where pointers are not used this may not work.

When it would be optimal to prefer Red-black trees over AVL trees? a) when there are more insertions or deletions b) when more search is needed c) when tree must be balanced d) when log(nodes) time complexity is needed

a) when there are more insertions or deletions. Explanation: Though both trees are balanced, when there are more insertions and deletions to make the tree balanced, AVL trees should have more rotations, it would be better to use red-black. but if more search is required AVL trees should be used.

Why to prefer red-black trees over AVL trees? a) Because red-black is more rigidly balanced b) AVL tree store balance factor in every node which costs space c) AVL tree fails at scale d) Red black is more efficient

b) AVL tree store balance factor in every node which costs space Explanation: Every node in an AVL tree need to store the balance factor (-1, 0, 1) hence space costs to O(n), n being number of nodes. but in red-black we can use the sign of number (if numbers being stored are only positive) and hence save space for storing balancing information. there are even other reasons where red black is mostly preferred.

What is the specialty about the in-order traversal of a binary search tree? a) It traverses in a non-increasing order b) It traverses in an increasing order c) It traverses in a random fashion d) None of the mentioned

b) It traverses in an increasing order Explanation: As a binary search tree consists of elements lesser than the node to the left and the ones greater than the node to the right, an in-order traversal will give the elements in an increasing order.

Why Red-black trees are preferred over hash tables though hash tables have constant time complexity? a) no, they are not preferred b) because of resizing issues of hash table and better ordering in red-black trees c) because they can be implemented using trees d) because they are balanced

b) because of resizing issues of hash table and better ordering in red-black trees Explanation: Red-black trees have O(logn) for ordering elements in terms of finding first and next elements. also whenever table size increases or decreases in hash table you need to perform rehashing which can be very expensive in real time. also red black stores elements in sorted order rather than input order.

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? a) true b) false

b) false Explanation: It is interesting to note that after insertion, only the path from that point to node or only that subtrees are imbalanced in terms of height.

What is the maximum height of an AVL tree with p nodes? a) p b) log(p) c) log(p)/2 d) p⁄2

b) log(p) Explanation: Consider height of tree to be 'he', then number of nodes which totals to 'p' can be written in terms of height as N(he)=N(he-1)+1+N(he-2). since N(he) which is p can be written in terms of height as the beside recurrence relation which on solving gives N(he)= O(logp) as worst-case height.

What does the following piece of code do? public void func(Tree root) { func(root.left()); func(root.right()); System.out.println(root.data()); } a) Preorder traversal b) In-order traversal c) Post-order traversal d) Level-order traversal

c) Post-order traversal Explanation: In a post-order traversal, first the left child is visited, then the right child and finally the parent.

Which of the following is an application of Red-black trees and why? a) used to store strings efficiently b) used to store integers efficiently c) can be used in process schedulers, maps, sets d) for efficient sorting

c) can be used in process schedulers, maps, sets Explanation: RB tree is used for Linux kernel in the form of completely fair scheduler process scheduling algorithm. It is used for faster insertions, retrievals.

Which of the below diagram is following AVL tree property? a) only i b) only i and ii c) only ii d) none of the mentioned

c) only ii Explanation: The property of AVL tree is it is height balanced tree with difference of at-most 1 between left and right subtrees.

Given an empty AVL tree, how would you construct AVL tree when a set of numbers are given without performing any rotations? a) just build the tree with the given input b) find the median of the set of elements given, make it as root and construct the tree c) use trial and error d) use dynamic programming to build the tree

c) use trial and error Explanation: Sort the given input, find the median element among them, make it as root and construct left and right subtrees with elements lesser and greater than the median element recursively. this ensures the subtrees differ only by height 1.

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

d) None of the mentioned Explanation: All the options hold good for a binary search tree and can be considered as a definition for a BST.

What are the worst case and average case complexities of a binary search tree? a) O(n), O(n) b) O(logn), O(logn) c) O(logn), O(n) d) O(n), O(logn)

d) O(n), O(logn) Explanation: Worst case arises when the tree is skewed(either to the left or right) in which case you have to process all the nodes of the tree giving O(n) complexity, otherwise O(logn) as you process only the left half or the right half of the tree.

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

d) Undo/Redo operations in a notepad Explanation: This is an application of stack.

What is the time complexity for finding the height of the binary tree? a) h = O(loglogn) b) h = O(nlogn) c) h = O(n) d) h = O(log n)

d) h = O(log n) Explanation: The nodes are either a part of left sub tree or the right sub tree, 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).


Kaugnay na mga set ng pag-aaral

Unit 1: Hi, I'm Peter (Go For it A1)

View Set

OSHA: Emergency Action Plans & Fire Protection

View Set

Psychology in Addiction Exam 1 (Chapter 1-5)

View Set

Chapter 16 Marketing & Management

View Set

Med term questions for exam 3 (Digestive system)

View Set