Chapter 8: Binary Trees Flashcards

1
Q

Why use binary trees?

A

Benefits of an ordered array and linked list: search is fast like an array and insertion and deletion is fast like a linked list.

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

How do you implement search for a binary tree?

A

Call sval the value to be found in the tree. Start at the root node, root = current. While current != sval, look at each of current’s children: child_right and child_left. If sval > child_left, current = child_right else current = child_left. If current == null return null. Finally return current.

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

What is the complexity of searching a binary tree?

A

O(log n)

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

How do you implement inorder binary tree traversal?

A

Create a recursive function which takes a node as an argument. In the outer call this node will be the root. The traverse function does 3 things in order: visits the left child, executes the traversal function (e.g. print node) and calls traverse on the right node.

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

How are pre- and post-order traversal implemented

A

The same, just with the inorder operations swapped: preorder is visit, call left, call right. postorder is call left, call right, visit.

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

Why should we study how to delete a node?

A

Deletion is important in many tree applications, and studying the details builds character.

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

How to delete a node which has no children?

A

Parent’s pointer is moved to null, Java’s garbage collection will take care of the data cleanup

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

How to delete a node which one child?

A

Connect it’s parent to it’s child

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

How do you find the in-order successor of a node (e.g. the node with the next highest value)

A

We are looking for the smallest the set which is larger than the current node. Starting at the current node visit the right node, which is greater than the current node. Then visit left children until there are no more left children to visit.

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

How do some programmer’s sidestep deletion?

A

An extra isDeleted field?

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

How do you delete a node with 2 children

A
  1. replace successor with it’s right subtree, 2. Keep the right child of the to be deleted node n it’s proper place by making it the right child of the successor, 3. make the successor the right node of the to be deleted’s parent
How well did you know this?
1
Not at all
2
3
4
5
Perfectly