Professional Documents
Culture Documents
Chapter 11 Object-Oriented Design Chapter 20 Lists, Stacks, Queues, Trees, and Heaps
Chapter 21 Generics
Chapter 22 Java Collections Framework Chapter 19 Recursion Chapter 23 Algorithm Efficiency and Sorting
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Objectives
To describe what a data structure is (20.1). To explain the limitations of arrays (20.1). To implement a dynamic list using an array (20.2.1). To implement a dynamic list using a linked structure (20.2.2 Optional). To implement a stack using an array list (20.3). To implement a queue using a linked list (20.3). To implement a binary search tree (20.4).
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Limitations of arrays
Once an array is created, its size cannot be altered. Array provides inadequate support for inserting, deleting, sorting, and searching operations.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Lists
A list is a popular data structure to store data in sequential order. For example, a list of students, a list of available rooms, a list of cities, and a list of books, etc. can be stored using lists. The common operations on a list are usually the following: Retrieve an element from this list. Insert a new element to this list. Delete an element from this list. Find how many elements are in this list. Find if an element is in this list. Find if this list is empty.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
MyAbstractList
#size: int #MyAbstractList() #MyAbstractList(objects: Object[]) +add(o: Object) : void +add(o: Object) : void +isEmpty(): boolean +size(): int The size of the list. Creates a default list. Creates a list from an array of objects. Implements the add method. Implements the isEmpty method. Implements the size method.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
MyList MyAbstractList
10
Array Lists
Array is a fixed-size data structure. Once an array is created, its size cannot be changed. Nevertheless, you can still use array to implement dynamic data structures. The trick is to create a new larger array to replace the current array if the current array cannot hold new elements in the list. Initially, an array, say data of Object[] type, is created with a default size. When inserting a new element into the array, first ensure there is enough room in the array. If not, create a new array with the size as twice as the current one. Copy the elements from the current array to the new array. The new array now becomes the current array.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
11
Insertion
Before inserting a new element at a specified index, shift all the elements after the index to the right and increase the list size by 1.
Before inserting e at insertion point i 0 1 i i+1 ei+1 k-1 k ek-1 ek data.length -1 e0 e 1 e ei-1 ei
Insertion point
k+1
e0 e1
ei-1 e
ek-1 ek data.length -1
12
e inserted here
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Deletion
To remove an element at a specified index, shift all the elements after the index to the left by one position and decrease the list size by 1.
Before deleting the element at index i 0 1 i i+1 ei+1 k-1 k ek-1 ek data.length -1 e0 e 1 ei-1 ei
e0 e1
ei-1 ei+1
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
13
Implementing MyArrayList
MyAbstractList MyArrayList
-data: Object[] +MyArrayList() Creates a default array list.
MyArrayList
TestList
Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
14
Linked Lists
Since MyArrayList is implemented using an array, the methods get(int index) and set(int index, Object o) for accessing and modifying an element through an index and the add(Object o) for adding an element at the end of the list are efficient. However, the methods add(int index, Object o) and remove(int index) are inefficient because it requires shifting potentially a large number of elements. You can use a linked structure to implement a list to improve efficiency for adding and remove an element anywhere in a list.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
15
16
17
MyLinkedList
MyAbstractList
Node
element: Object next: Node
1 Link m 1
MyLinkedList
-first: Node -last: Node +LinkedList() Creates a default linked list.
+LinkedList(objects: Object[]) Creates a linked list from an array of objects. +addFirst(o: Object): void Adds the object to the head of the list. +addLast(o: Object): void +getFirst(): Object +getLast(): Object +removeFirst(): Object +removeLast(): Object Adds the object to the tail of the list. Returns the first object in the list. Returns the last object in the list. Removes the first object from the list. Removes the last object from the list.
My LinkedList
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
18
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
19
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
20
last
next
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
21
Delete this node first e1 next ei next ei+1 next ek next last
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
22
23
24
MyStack
MyQueue
-list: MyLinkedList
MyQueue
+enqueue(element: Object): void Adds an element to this queue. +dequeue(): Object +getSize(): int Removes an element from this queue. Returns the number of elements from this queue.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
25
TestStackQueue
Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
26
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. Examples of binary trees are shown in Figure 20.18.
60 G
55
100
45
57 (A)
67
107
A (B)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
27
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
28
55
45
57
67
107
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
29
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
30
55
100
45
57
67
107
59
101
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
31
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, then the current node itself, and finally the right subtree of the current node. 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, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
32
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
33
BinaryTree
-root: TreeNode +BinaryTree()
BinaryTree
Creates a default binary tree.
+BinaryTree(objects: Object[]) Creates a binary tree from an array of objects. +insert(o: Object): boolean Adds an element to the binary tree. +inorder(): void +preorder(): void +postorder(): void Prints the nodes in inorder traversal. Prints the nodes in preorder traversal. Prints the nodes in postorder traversal.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
34
BinaryTree
Run
35
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
36
42 32 22 29 14 39 33 32 22 29
39 42 14 22 32
42 39 14 33 22 32 29
42
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
37
Representing a Heap
For 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)/2. For 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).
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10][11][12][13]
62 42 32 22 29 14 39 33 17 44 30 9 59 13
[10][11] 62 42 59 32 39 44 13 22 29 14 33 17 30 9
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
38
Rebuilding a Heap
9 42 32 22 29 14 39 33 17 44 30 59 13 32 22 29 14 42 39 33 17 44 30 59 9 13
59 42 32 22 29 14 39 33 17 9 30 44 13 32 22 29 14 42 39 33
59 44 30 17 9 13
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
39
59 42 32 22 29 14 39 33 17 9 30 44 13 32 22 29 14 42 39 33
59 44 30 17 9 13
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
40
59 42 32 22 29 14 39 33 17 44 30 88 13 32 22 29 14 42 39 33
88 59 44 17 30 13
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Heap
TestHeap
Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
42
Priority Queue
A 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.
MyPriorityQueue
-heap: Heap +enqueue(element: Object): void +dequeue(): Object +getSize(): int Adds an element to this queue. Removes an element from this queue. Returns the number of elements from this queue.
MyPriorityQueue TestPriorityQueue
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Run
43