1 Chapter 25 Trees Iterators Heaps Priority Queues.
-
Upload
kristin-nash -
Category
Documents
-
view
225 -
download
3
Transcript of 1 Chapter 25 Trees Iterators Heaps Priority Queues.
2
Objectives
To design and implement a binary search tree (§25.2). To insert an element to a binary search tree (§25.2.2). To traverse elements in a binary tree (§25.2.3). To delete elements from a binary search tree (§25.3). To create iterators for traversing a binary tree (§25.4). To design and implement a heap (§25.5). To design and implement a priority queue (§25.6).
3
Binary TreesA list, stack, or queue is a linear structure
– 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
55 100
57 67 107 45
G
F R
M T A
(A) (B)
5
Binary Tree Terms root of left (right) subtree of a node is called a left (right) child of the node leaf -
– A node without children binary search tree
– A special type of binary tree is often usefulA binary search tree (with no duplicate elements) has the property that for every node in the tree :
– the value of any node in its left subtree is less than the value of the node and
– the value of any node in its right subtree is greater than the value of the node
The binary trees in Figure 25.1 are all binary search trees. This section is concerned with binary search trees.
6
Representing Binary TreesA binary tree can be represented using a set of linked nodesEach node contains:
– a value and two links named left and right that reference the left child and right child, respectively
60
55 100
57 45 67 107
root class TreeNode<E> {
E element;
TreeNode<E> left;
TreeNode<E> right;
public TreeNode(E o) {
element = o;
}
}
7
Inserting an Element to a Binary Tree
If a binary tree is empty– create a root node with the new element
Otherwise– locate the parent node for the new element node– If the new element is less than the parent element
the node for the new element becomes the left child of the parent
– If the new element is greater than the parent element the node for the new element becomes the right child of the
parent
8
Inserting an Element to a Binary Treeif (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
9
Trace Inserting 101 into the following treeif (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
10
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
11
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
12
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
101 < 60?
13
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
101 > 60?
14
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current parent
101 > 60 true
15
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
parent
101 > 60 true
16
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
parent
101 > 60 true
17
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
parent
101 > 100 false
18
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
parent
101 > 100 true
19
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
parent
101 > 100 true
20
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
parent
101 > 100 true
21
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
parent
101 > 100 true
22
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
parent
101 < 107 true
23
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
current
parent
101 < 107 true
24
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
Since current.left is null,current becomes null
parent
101 < 107 true
25
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
Since current.left is null,current becomes null
parent
current is null now
26
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
Since current.left is null,current becomes null
parent
101 < 107 true
27
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
parent
101
101 < 107 true
28
Trace Inserting 101 into the following tree, cont.if (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
Insert 101 into the following tree.
60
55 100
57 45 67 107
root
parent
101
101 < 107 true
29
Inserting 59 into the Treeif (root == null)
root = new TreeNode(element);
else {
// Locate the parent node
current = root;
while (current != null)
if (element value < the value in current.element) {
parent = current;
current = current.left;
}
else if (element value > the value in current.element) {
parent = current;
current = current.right;
}
else
return false; // Duplicate node not inserted
// Create the new node and attach it to the parent node
if (element < parent.element)
parent.left = new TreeNode(elemenet);
else
parent.right = new TreeNode(elemenet);
return true; // Element inserted
}
60
55 100
57 45 67 107
root
59 101
30
Tree Traversal Tree traversal –
– Process of visiting each node in the tree exactly once– Several ways to traverse a tree
Inorder– visit the left subtree of the current node first, then the current node itself,
and finally the right subtree of the current node Preorder
– visit the left subtree of the current node first, then the right subtree of the current node, and finally the current node itself
Postorder– visit the current node first, then the left subtree of the current node, and
finally the right subtree of the current node depth-first breadth-first traversals
– visit the nodes level by level. First visit the root, then all children of the root from left to right, then grandchildren of the root from left to right, and so on
31
Tree Traversal For example, in the tree in Figure 25.2
– inorder is 45 55 57 59 60 67 100 101 107
– postorder is 45 59 57 55 67 101 107 100 60
– preorder is 60 55 45 57 59 100 67 107 101
– breadth-first traversal is 60 55 100 45 57 67 107 59 101
32
Tree Interface Tree interface
– defines common operations for trees, and the AbstractTree class partially implements Tree
AbstractTreeAbstractTree
«interface»
Tree<E> +search(e: E): boolean
+insert(e: E): boolean
+delete(e: E): boolean
+inorder(): void
+preorder(): void
+postorder(): void
+getSize(): int
+isEmpty(): boolean
+iterator(): java.util.Iterator
Returns true if the specified element is in the tree.
Returns true if the element is added successfully.
Returns true if the element is removed from the tree successfully.
Prints the nodes in inorder traversal.
Prints the nodes in preorder traversal.
Prints the nodes in postorder traversal.
Returns the number of elements in the tree.
Returns true if the tree is empty.
Returns an iterator for traversing the elements.
AbstractTree<E>
TreeTree
33
BinaryTree Class BinaryTree with A concrete BinaryTree class can be defined to extend AbstractTree
BinaryTreeBinaryTree
BinaryTree<E>
-root: TreeNode<E>
-size: int
+BinaryTree()
+BinaryTree(objects: E[])
+path(e: E): java.util.List
1
m TreeNode<E>
element: E
left: TreeNode<E>
right: TreeNode<E>
Link
0
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.
«interface» Tree<E>
AbstractTree<E>
34
Example: Using Binary Trees
Write a program that creates a binary tree using BinaryTreeAdd strings into the binary tree and traverse the tree in –
– inorder– Postorder– preorder
TestBinaryTreeTestBinaryTree RunRun
35
Tree After Insertions
George
Adam Michael
Daniel Jones Tom
root
Peter
Inorder: Adam, Daniel George, Jones, Michael, Peter, Tom
Postorder: Daniel Adam, Jones, Peter, Tom, Michael, George
Preorder: George, Adam, Daniel, Michael, Jones, Tom, Peter
36
Deleting Elements in a Binary Search Tree To delete an element from a binary tree –
– 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
– current node may be a left child or a right child of the parent node
– There are two cases to consider:
37
Deleting Elements in a Binary Search TreeCase 1:
– The current node does not have a left child– Simply connect the parent with the right child of the
current node
parent
current
No left child
Subtree
parent
Subtree
current may be a left or right child of parent
Subtree may be a left or right subtree of parent
current points the node to be deleted
38
Deleting Elements in a Binary Search TreeFor example, to delete node 10 in Figure 25.9(a). Connect the parent of node 10 with the right child of node 10, as shown in Figure 25.9(b).
20
10 40
30 80
root
50
16
27
20
40
30 80
root
50
16
27
39
Deleting Elements in a Binary Search Tree Case 2:
– The current node has a left child– Let rightMost point to the node that contains the
largest element in the left subtree of the current node and parentOfRightMost point to the parent node of the rightMost node
– Note that the rightMost node cannot have a right child, but may have a left child
– Replace the element value in the current node with the one in the rightMost node, connect the parentOfRightMost node with the left child of the rightMost node, and delete the rightMost node, as shown in Figure 25.10(b).
40
Deleting Elements in a Binary Search TreeCase 2 diagram
parent
current
.
.
.
rightMost
parentOfRightMost
parent
.
.
.
parentOfRightMost
Content copied to current and the node deleted
Right subtree Right subtree
current
current may be a left or right child of parent
current points the node to be deleted
The content of the current node is replaced by content by the content of the right-most node. The right-most node is deleted.
leftChildOfRightMost leftChildOfRightMost
41
Deleting Elements in a Binary Search TreeCase 2 example, delete 20
rightMost
20
10 40
30 80
root
50
16
27
16
10 40
30 80
root
50 27 14 14
42
Examples Delete this
node George
Adam Michael
Daniel Jones Tom
Peter
Daniel
Adam Michael
Jones Tom
Peter
44
Examples Daniel
Michael
Jones Tom
Peter
Delete this node
Daniel
Jones
Tom
Peter
TestBinaryTreeDeleteTestBinaryTreeDelete RunRun
45
binary tree time complexity It is obvious that the time complexity for the inorder, preorder, and postorder is O(n) since each node is traversed only once The time complexity for search, insertion and deletion is the height of the treeIn the worst case, the height of the tree is O(n)
47
Iterators 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).
TestBinaryTreeWithIteratorTestBinaryTreeWithIterator RunRun
48
Heap
Heap is a useful data structure for designing efficient sorting algorithms and priority queues A heap is a binary tree with the following properties:
– It is a complete binary tree
– Each node is greater than or equal to any of its children
49
Complete Binary Tree
A binary tree is complete –– if every level of the tree is full except that the last level may not
be full and all the leaves on the last level are placed left-most– binary trees in (a) and (b) are complete– binary trees in (c) and (d) are not complete– binary tree in (a) is a heap– but the binary tree in (b) is not a heap, because the root (39) is
less than its right child (42)
22 29 14 33
32 39
42
22 29 14
32 42
39
22 14 33
32 39
42
22 29
32
42 (a) (b) (c) (d)
51
Representing a HeapFor a node at position i, its left child is at position 2i+1 and its right child is at position 2i+2, and its parent is (i-1)/2For example, the node for element 39 is at position 4, so its left child (element 14) is at 9 (2*4+1), its right child (element 33) is at 10 (2*4+2), and its parent (element 42) is at 1 ((4-1)/2).
22 29 14 33 30 17 9
32 39 44 13
42 59
62 62 42 59 32 39 44 13 22 29 14 33 30 17 9
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10][11][12][13] [10][11]
parent left
right
52
Adding Elements to the Heap
Adding 3, 5, 1, 19, 11, and 22 to a heap, initially empty
3
(a) After adding 3 (b) After adding 5 (c) After adding 1
(d) After adding 19
3
19
5 1
(e) After adding 11
3 5
19
11 1
(f) After adding 22
3 5 1
22
11 19
5
3
5
3 1
53
Rebuild the heap after adding a new node
Adding 88 to the heap
(a) Add 88 to a heap
3 5 1 88
22
11 19
(b) After swapping 88 with 19
3 5 1 19
22
11 88
(b) After swapping 88 with 22
3 5 1 19
88
11 22
54
Removing the Root and Rebuild the Tree
22 29 14 33 30 17 9
32 39 44 13
42 59
62
Removing root 62 from the heap
59
Heap Class
HeapHeap RunRunTestHeapTestHeap
Heap<E>
-list: java.util.ArrayList<E>
+Heap()
+Heap(objects: E[])
+add(newObject: E): void
+remove(): E
+getSize(): int
Creates a default empty heap.
Creates a heap with the specified objects.
Adds a new object to the heap.
Removes the root from the heap and returns it.
Returns the size of the heap.
60
Priority QueueA regular queue is a first-in and first-out data structure. Elements are appended to the end of the queue and are removed from the beginning of the queue. In a priority queue, elements are assigned with priorities. When accessing elements, the element with the highest priority is removed first. A priority queue has a largest-in, first-out behavior. For example, the emergency room in a hospital assigns patients with priority numbers; the patient with the highest priority is treated first.
MyPriorityQueueMyPriorityQueue RunRunTestPriorityQueueTestPriorityQueue
MyPriorityQueue<E>
-heap: Heap<E>
+enqueue(element: E): void
+dequeue(): E
+getSize(): int
Adds an element to this queue.
Removes an element from this queue.
Returns the number of elements from this queue.