6.1 Binary Search Trees Flashcards

1
Q

For search,Insert/delete,Min/Max and Predecessor/Successor operations what is the runtime complexity when implementing via a Binary Search Tree

A

theta(h)

h = height of tree (logn)

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

Definition of height of tree

A

Number of edges on longest root-to-leaf path

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

What is recursive definition for the height of a tree

A

Starting from the root:

1 + max{h(TL), h(TR)}

TL = subtree to the left
TR = subtree to the right

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

Properties of a binary search tree

A

All nodes in the left subtree are less than its root
All nodes in the right subtree are greater than or equal than its root
Max 2 children

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

Describe how an In order Traversal outputs the nodes in value order

A

Recursively traverse the left subtree of the root.

Output the value of the root.

Recursively traverse the right subtree of the root.

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

Running time complexity for In order traversal

A

O(n)

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

What is the output of the in order traversal for this tree

A

dbehafch

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

How would a search for a node be implemented recursively?

A

NodeSearch(key k, Node x = root)

if x = nil or x.key then return x

else if k < x.key then return NodeSearch(k, x.left)

else

return NodeSearch(k, x.right)

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

How would a search for a node be implemented iteratively?

A

while not nil and x.key not k

if k < x.key then x = x.left

else x = x.right

return x

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

What is a successor to a node x

Give its mathematical definition as well

A

node with smallest key that is also greater than x.key

arg min y {y.key|y.key > x.key}

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

what does y = x. p mean

A

x. p means x’s parent so y = x’s parent

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

How do you delete the root (or any node that isnt a leaf)? (has 2 children)

A

Find the roots successor, set that as the new root.

Recurse this operation, deleting the successor that just got placed at the root.

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

How do you delete a leaf node z

A

Determine if z is the left or right child of z. p
Set that left/right pointer to nil

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

How to delete a node z that has one child x

A

Set pointer from z. p to z to point to x

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

What is best case and worst case runtime complexity for a BST?

A

The heigh which

Best case: O(log(n))
Worst case: theta(n) when each node has one child so the height is n

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