Professional Documents
Culture Documents
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Objectives
To design and implement a binary search tree (26.2). To represent binary trees using linked data structures (26.2.1). To search an element in binary search tree (26.2.2). To insert an element into a binary search tree (26.2.3). To traverse elements in a binary tree (26.2.4). To delete elements from a binary search tree (26.3). To display binary tree graphically (26.4). To create iterators for traversing a binary tree (26.5). To implement Huffman coding for compressing data using a binary tree (26.6).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Binary Trees
A list, stack, or queue is a linear structure that consists of a sequence of elements. A binary tree is a hierarchical structure. It is either empty or consists of an element, called the root, and two distinct binary trees, called the left subtree and right subtree.
60 G
55
100
45
57 (A)
67
107
A (B)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
55
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
60
55
100
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
60
55
100
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
60
55
100
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
10
60
55
100
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
11
60
55
100
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
12
60
55
100
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
13
following tree.
60
55
100
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
14
following tree.
60
current 100
55
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
15
following tree.
60
current 100
55
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
16
100 false
60
current 55 100
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
17
tree.
current 100
55
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
18
tree.
55
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
19
tree.
parent
55
100 current
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
20
tree.
parent
55
100 current
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
21
tree.
parent
55
100 current
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
22
tree.
55
45
57
67
107
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
23
tree.
parent 100
55
45
57
67
107
24
60
parent 55 100
45
57
67
107
25
60
parent 55 100
45
57
67
107
26
60
parent 55 100
45
57
67
107
101
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
27
60
parent 55 100
45
57
67
107
101
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
28
root
60
55
100
45
57
67
107
59
101
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
29
Tree Traversal
Tree traversal is the process of visiting each node in the tree exactly once. There are several ways to traverse a tree. This section presents inorder, preorder, postorder, depthfirst, and breadth-first traversals. The inorder traversal is to visit the left subtree of the current node first recursively, then the current node itself, and finally the right subtree of the current node recursively. The postorder traversal is to visit the left subtree of the current node first, then the right subtree of the current node, and finally the current node itself.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
30
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
31
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
32
Tree
AbstractTree
AbstractTree<E>
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
33
AbstractTree<E>
m 0
TreeNode<E>
element: E left: TreeNode<E> right: TreeNode<E>
1 Link
BinaryTree<E>
-root: TreeNode<E> -size: int +BinaryTree() +BinaryTree(objects: E[]) +path(e: E): java.util.List The root of the tree. The number of nodes in the tree. Creates a default binary tree. Creates a binary tree from an array of elements. Returns the path of nodes from the root to the node that contains e. The method returns a list or null if e is not in the tree.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
34
TestBinaryTree
Run
35
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Adam
Daniel
Jones
Tom
Postorder: Daniel Adam, Jones, Peter, Tom, Michael, George Preorder: George, Adam, Daniel, Michael, Jones, Tom, Peter
Peter
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
36
To delete an element from a binary tree, you need to first locate the node that contains the element and also its parent node. Let current point to the node that contains the element in the binary tree and parent point to the parent of the current node. The current node may be a left child or a right child of the parent node. There are two cases to consider:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
37
parent current may be a left or right child of parent current current points the node to be deleted
No left child
Subtree
Subtree
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
38
root
root
10
40
40
16
30
80
16
30
80
27
50
27
50
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
39
40
Right subtree . . . . . .
Right subtree
parentOfRightMost
parentOfRightMost
rightMost
leftChildOfRightMost
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
41
10 rightMost 16 30
40
10
40
80
30
80
14
27
50
14
27
50
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
42
Examples
Delete this node George
Daniel
Adam
Michael
Adam
Michael
Daniel
Jones
Tom
Jones
Tom
Peter
Peter
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
43
Examples
Delete this node Daniel
Daniel
Adam
Michael
Michael
Jones
Tom
Jones
Tom
Peter
Peter
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
44
Examples
Daniel Daniel Michael Jones
Jones
Tom
Tom
Peter
Peter
TestBinaryTreeDelete
Run
45
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
46
Tree Visualization
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Iterators
An iterator is an object that provides a uniform way for traversing the elements in a container such as a set, list, binary tree, etc.
interface
java.util.Iterator
+hasNext(): boolean +next(): Object +remove(): void Returns true if the iterator has more elements. Returns the next element in the iterator. Removes from the underlying container the last element returned by the iterator (optional operation).
TestBinaryTreeWithIterator
Run
48
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
1 s
Character M p s i
Frequency 1 2 4 4
p
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
49
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
50
Begin with a forest of trees. Each tree contains a node for a character. The weight of the node is the frequency of the character in the text. Repeat this step until there is only one tree: Choose two trees with the smallest weight and create a new node as their parent. The weight of the new tree is the sum of the weight of the subtrees.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
51
weight: 4 s
weight: 4 i
weight: 1 M
weight: 2 p
weight: 11
weight: 7 weight: 4 i
1 weight: 4 i
weight: 3
weight: 4 s
weight: 1 M
weight: 2 p
0 weight: 1 M
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
52