Big Java 6

Chapter 17 – Tree Structures

Chapter Goals

real tree - upside down

Basic Tree Concepts

Basic Tree Concepts

Basic Tree Concepts

Trees in Computer Science

Basic Tree Concepts

Basic Tree Concepts

Basic Tree Concepts

Self Check 17.1

What are the paths starting with Anne in the tree shown in Figure 1?
royal family tree

Self Check 17.2

What are the roots of the subtrees consisting of 3 nodes in the tree shown in Figure 1?
royal family tree

 

Self Check 17.5

Describe a recursive algorithm for counting all leaves in a tree.

Self Check 17.6

Using the public interface of the Tree class in this section, construct a tree that is identical to the subtree with root Anne in Figure 1.
royal family tree

Self Check 17.7

Is the size method of the Tree class recursive? Why or why not?

Binary Trees

a mother wih two children

 

 

 

In a binary tree, each node has a left and a right child node.

Binary Trees Examples - Decision Tree

Binary Trees Examples - Decision Tree

Binary Tree Examples - Huffman Tree

Binary Tree Examples - Huffman Tree

Binary Tree Examples - Expression Tree

Balanced Trees

a balance scale

 

 

In a balanced binary tree, each subtree has approximately the same number of nodes.

Balanced Trees

Balanced Trees

A Binary Tree Implementation

A Binary Tree Implementation

A Binary Tree Implementation

A Binary Tree Implementation

Self Check 17.8

Encode ALOHA, using the Huffman code in Figure 5.
huffman tree to encode hawaiian

Self Check 17.9

In an expression tree, where is the operator stored that gets executed last?

Self Check 17.10

What is the expression tree for the expression 3 – 4 – 5?

Binary Search Trees

Binary Search Trees

a binary search tree
Figure 9 A Binary Search Tree

Binary Search Trees

not a binary seach tree
Figure 10 A Binary Tree That Is Not a Binary Search Tree

Binary Search Trees

Binary Search Trees - Insertion

Binary Search Trees - Insertion

Binary Search Trees - Insertion

Binary Search Trees - Insertion

Binary Search Trees - Removal

Binary Search Trees - Removal

Binary Search Trees - Efficiency of the Operations

section_3/BinarySearchTree.java

Your browser does not support this feature

Self Check 17.13

What is the difference between a tree, a binary tree, and a balanced binary tree?

Self Check 17.14

Are the left and right children of a binary search tree always binary search trees? Why or why not?

Self Check 17. 15

Draw all binary search trees containing data values A, B, and C.

Self Check 17.16

Give an example of a string that, when inserted into the tree of Figure 12, becomes a right child of Romeo.
binary search tree with 5 nodes

Self Check 17.17

Trace the removal of the node “Tom” from the tree in Figure 12.
5 nodes

Self Check 17.18

Trace the removal of the node “Juliet” from the tree in Figure 12.
5 nodes

Tree Traversal - Inorder Traversal

Preorder and Postorder Traversals

Preorder and Postorder Traversals

The Visitor Pattern

The Visitor Pattern

Depth-First Search

Breadth-First Search

Breadth-First Search

Tree Iterators

Self Check 17.19

What are the inorder traversals of the two trees in Figure 6 on page 771?
expression tree

Self Check 17.20

Are the trees in Figure 6 binary search trees?

Self Check 17.24

What are the first eight visited nodes in the breadth-first traversal of the tree in Figure 1?
royal family tree

Red-Black Trees

Red-Black Trees

toll booth

 

 

Think of each node of a red-black tree as a toll booth. The total toll to each exit is the same.

Red-Black Trees

Red-Black Trees

Red-Black Trees

Red-Black Trees - Insertion

Red-Black Trees - Insertion

Red-Black Trees - Insertion

Red-Black Trees - Insertion

Red-Black Trees - Removal

Red-Black Trees - Removal

Red-Black Trees - Removal

Red-Black Trees - Removal

Red-Black Trees - Efficiency

red-black tree efficiency

Self Check 17.25

Consider the extreme example of a tree with only right children and at least three nodes. Why can’t this be a red-black tree?
tree with all right children for self check 25

Self Check 17.26

What are the shapes and colorings of all possible red-black trees that have four nodes?

Self Check 17.27

Why does Figure 21 show all possible configurations of a double-red violation?
double red violations for self check 27

Self Check 17.28

When inserting an element, can there ever be a triple-red violation in Figure 21? That is, can you have a red node with two red children? (For example, in the first tree, can t1 have a red root?)
double red violation for self check 28

Self Check 17.29

When removing an element, show that it is possible to have a triple-red violation in Figure 23.
self check 29

Self Check 17.30

What happens to a triple-red violation when the double-red fix is applied?

Heaps

Heaps

Heaps

a building with missing siding at the top

 

 

In an almost complete tree, all layers but one are completely filled.

Heaps

Heaps

Differences from a binary search tree

  1. The shape of a heap is very regular.
    • Binary search trees can have arbitrary shapes.
  2. In a heap, the left and right subtrees both store elements that are larger than the root element.
    • In a binary search tree, smaller elements are stored in the left subtree and larger elements are stored in the right subtree.

Heaps - Insertion

Algorithm to insert a node

  1. Add a vacant slot to the end of the tree.
  2. If the parent of the empty slot if it is larger than the element to be inserted:
    • Demote the parent by moving the parent value into the vacant slot.
    • Move the vacant slot up.
    • Repeat this demotion as long as the parent of the vacant slot is larger than the element to be inserted.
  3. Insert the element into the vacant slot at this point:
    • Either the vacant slot is at the root.
    • Or the parent of the vacant slot is smaller than the element to be inserted.

Heaps - Insertion Step 1

step 1 insert into a heap

Heaps - Insertion Step 2

step 2 heap insertion

Heaps - Insertion Step 3

step 3 heap insertion

Heaps - Removing the Root

Heaps - Removing the Root Steps 1 and 2

Step 1 and 2 removal from heap

Heaps - Removing the Root Step 3

Step 3 removing from heap

Heaps - Efficiency

Heaps

Heaps

section_6/MinHeap.java

Your browser does not support this feature

section_6/WorkOrder.java

Your browser does not support this feature

section_6/HeapDemo.java

Your browser does not support this feature Program Run:

Self Check 17.31

The software that controls the events in a user interface keeps the events in a data structure. Whenever an event such as a mouse move or repaint request occurs, the event is added. Events are retrieved according to their importance. What abstract data type is appropriate for this application?

Self Check 17.32

In an almost-complete tree with 100 nodes, how many nodes are missing in the lowest level?

Self Check 17.33

If you traverse a heap in preorder, will the nodes be in sorted order?

Self Check 17.34

What is the heap that results from inserting 1 into the following?
heap for self check 34 question

Self Check 17.35

What is the result of removing the minimum from the following?
self check 35 question

The Heapsort Algorithm

The Heapsort Algorithm

Tree to Heap

tree to heap
Figure 31 Turning a Tree into a Heap

Tree to Heap

section_7/HeapSorter.java

Your browser does not support this feature

Self Check 17.36

Which algorithm requires less storage, heapsort or merge sort?

Self Check 17.37

Why are the computations of the left child index and the right child index in the HeapSorter different than in MinHeap?

Self Check 17.38

What is the result of calling HeapSorter.fixHeap(a, 0, 4) where a contains 1 4 9 5 3?

Self Check 17.39

Suppose after turning the array into a heap, it is 9 4 5 1 3. What happens in the first iteration of the while loop in the sort method?

Self Check 17.40

Does heapsort sort an array that is already sorted in O(n) time?