Binary Search Trees Flashcards

1
Q

Which number(s) are returned by the sequence of queries below?

Insert(3)
Insert(8)
Insert(5)
Insert(10)
Delete(8)
Insert(12)
NearestNeighbors(7)

a) 3
b) 5
c) 8
d) 10
e) 12

A

5 y 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which of the trees satisfies the search tree property?

https://drive.google.com/file/d/1zG0XqWoyEvO0YuyX955khfzL2p1K33JS/view?usp=sharing

a) A
b) B
c) C

A

B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

There is actually a slight bug in the code presented for Next. What is it?

a) Does not correctly handle case where N is the largest element,
b) Does not correctly handle the case when N is the root node
c) Does not correctly handle the case when N is the smallest element
d) Does not correctly handle the case when N has no left child

A

Does not correctly handle case where N is the largest element.

If in the presented code N is the largest element in the tree, you will get an error when finding the RightAncestor after trying to find the grandparent of the root node. To fix this, you would want to modify RightAncestor to return null if the input is null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which tree results when the highlighted node is deleted?

https://drive.google.com/file/d/1JPcsldN17s_LEdsE4fSwIwdP3tMGBjfW/view?usp=sharing

A
B
C

A

C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the ordering of the times that it takes to search for the given nodes?

https://drive.google.com/file/d/1aJlJskZF8wr-tvVGjZH9yewvMS3IoUSx/view?usp=sharing

A<D<B<C
A<B<C<D
C<D<A<B
A<D<C<B
A<B<D<C

A

A<D<B<C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the height of the highlighted node?

https://drive.google.com/file/d/1JonF6WQlEMoebndPDjzHkyEs0-thw3Y6/view?usp=sharing

A

6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which insertion will require the tree to be rebalanced in order to maintain the AVL property?

https://drive.google.com/file/d/19iXqc0x5bT8KaZhDrr2RXV9y9OMo4Eoi/view?usp=sharing

A
B
C
D

A

D

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which of the trees below can be merged with the one above?

https://drive.google.com/file/d/1GzVZc9jLxwnfmZf864YCNEaL0vEp7cbl/view?usp=sharing

A
B
C

A

A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Your colleague proposed a different definition of a binary search tree: it is such binary tree with keys in the nodes that for each node the key of its left child (if exists) is less than its key, and the key of its right child (if exists) is bigger than its key. Is this a good definition for a binary search tree?

No
Yes

A

Correct! Binary search for a key in such tree might not work: try finding the key 2 in the tree below. Your first move would be to go left from the root, because 2<3 However, the key 2 is in the right subtree of the root. The condition that the key of the left child of each node is less than the key of the parent, and the key of the right child is greater than the key of the parent is satisfied for all nodes in the tree. What’s missing is that not just the key of the left child, but the keys of the whole left subtree of each node should be less than the key of the subtree root, and similarly for the right subtree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Suppose we enforce the AVL tree condition only for the root of the tree, but not for all the nodes. Can such tree be unbalanced?
Yes
No

A

Correct! Such tree could have height n/2 where n is the number of nodes: two chains of length n/2 having root as the only common node form such a tree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can the Insert operation be implemented given only Split and Merge operations?
a) Yes. First create a new tree with single key- the key to be inserted. Then merge current tree with the new tree
b) Yes. First create a new tree with single key - the key to be inserted. Then split the current tree by this key. Then merge the left splitted part with the new tree. Then merge the result with the right splitted part
c) NO

A

b) Yes. First create a new tree with single key - the key to be inserted. Then split the current tree by this key. Then merge the left splitted part with the new tree. Then merge the result with the right splitted part

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can the Delete operation be implemented given only Split and Merge operations?

Yes
No

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which node represents the 5th smallest element in the tree?

https://drive.google.com/file/d/1i0RHcm0TkYbIZWrykRWpR6_CgZHqs99-/view?usp=sharing

A
B
C
D
E

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which of the following is the result of splaying the highlighted node?

https://drive.google.com/file/d/1IUjEwnFB1URwFuMLJCELC1J5hEKzVOwF/view?usp=sharing

A
B
C

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is going to happen if you forget to splay the last accessed vertex in the implementation of Find in case the key was not found in the splay tree?

a) The tree will work too slow on some sequences of operations
b) Some of the tree operations will work incorrectly after that.

A

Correct! See this visualization and try to insert many elements in perfect order starting from an empty tree: insert 1, them 2, then 3, and so on. See how the tree grows unbalanced, it is just a chain! However, by now each operation took O(1) time, so it’s ok. Now think what will happen if you look for element 0 in this tree. If you use the visualization, you will see that you will have to go all the way down through the tree and then find out in the end that you didn’t find anything. The tree in the visualization then splays the lowest vertex, and the tree becomes more balanced. But let’s suppose you forgot to implement that - then the tree won’t change after the call to Find If you then try to find 0 again in the tree, you will have to go all the way down again! So, after inserting n elements in the tree in the perfect order, if you look for an element that is smaller than all the keys in the tree n times, then each of the last n operations will take O(n) time, so the tree no longer works in amortized O(logn) time!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What will happen if you splay the node with the smallest key in a splay tree?

The root of the new tree wont have right child
The root of thte noew tree wont have left child
The root of the new tree wont have children
The root of the new tree will have both children

A

Correct! The node with the smallest key will become the root after splaying, and it cannot have a left child, because the key of the left child must be smaller than the key of its parent.

17
Q

What will happen if you select a node N, splay its predecessor P (the node with the largest key smaller than the key of N) then splay the node N itself?

N will be the root, P will be the left child of the root, P wont have a right child
P will be the root
N will be the root, P will be the right child of the root
N will be the root, P will be the left child of the root, P wont have a left child

Correct! After the first splay, P will become the root. After the second splay, N will become the root, and P will become its child, and it will be on the left, because its key is smaller. P won’t have a right child, because a right child of P must have key bigger than the key of P , and also it must have key smaller than the key of N (because it is now in the left subtree of N) , but it can’t happen, because P is the predecessor of N , so there are no keys between the key of P and the key of N.

A

N will be the root, P will be the left child of the root, P wont have a right child