# 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.

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.

3
Q

What is the complexity of searching a binary tree?

A

O(log n)

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.

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.

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.

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

8
Q

How to delete a node which one child?

A

Connect it’s parent to it’s child

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.

10
Q

How do some programmer’s sidestep deletion?

A

An extra `isDeleted` field?

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