CSE 12 – Basic Data Structures
-
Upload
gillian-castillo -
Category
Documents
-
view
17 -
download
1
description
Transcript of CSE 12 – Basic Data Structures
![Page 1: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/1.jpg)
CSE 12 – Basic Data StructuresCynthia Bailey Lee
Some slides and figures adapted from Paul Kube’s CSE 12
CS2 in Java Peer Instruction Materials by Cynthia Lee is
licensed under a Creative Commons Attribution-
NonCommercial 4.0 International License.
Based on a work at http://peerinstruction4cs.org.
Permissions beyond the scope of this license may be available at
http://peerinstruction4cs.org.
![Page 2: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/2.jpg)
2
Today’s Topics1. Priority queues
Unsorted link list implementation Sorted link list implementation Heap implementation
![Page 3: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/3.jpg)
Reading quiz!
![Page 4: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/4.jpg)
A queue is a FIFO data structure whereas a priority queue is _____ data structure.
A. Also a FIFOB. A LIFOC. An HPIFOD. An APIFO
READING QUIZ!
![Page 5: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/5.jpg)
The ____ of a tree is a node with no parent.
A. OrphanB. Leaf nodeC. Super nodeD. Root
READING QUIZ!
![Page 6: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/6.jpg)
For assignment P4, when the add() method is called with a non-null argument, and the size of the Heap12 is equal to the length of its backing store, what should be done?
A. Throw an exceptionB. Double the length of the backing store
arrayC. Create a new node and add it to the
end of the backing store linked listD. Return false
READING QUIZ!
![Page 7: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/7.jpg)
Priority Queues
![Page 8: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/8.jpg)
Priority Queue
Emergency Department waiting room operates as a priority queue
Patients sorted according to seriousness, NOT how long they have waited
![Page 9: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/9.jpg)
Priority queue implementation options
Unsorted linked list Insert new element in front Remove by searching list for highest-
priority item
Sorted linked list Always insert new elements where they
go in priority-sorted order Remove from front
![Page 10: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/10.jpg)
Unsorted linked list Add is FAST
Just throw it in the list at the front
O(1)
Remove/peek is SLOW Hard to find item the
highest priority item—could be anywhere
O(N)
Priority queue implementations
![Page 11: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/11.jpg)
Sorted linked list Add is SLOW
Need to step through the list to find where item goes in priority-sorted order
O(N)
Remove/peek is FAST Easy to find item you
are looking for (first in list)
O(1)
Priority queue implementations
![Page 12: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/12.jpg)
We want the best of both Fast add AND fast remove/peek We will investigate trees as a way to do
this
Priority queue implementations
![Page 13: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/13.jpg)
Binary trees
![Page 14: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/14.jpg)
How many of these are valid binary trees?
A. 0-3B. 4C. 5
D. 6E. 7-8
![Page 15: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/15.jpg)
A node object for binary trees Similar to a linked
list node, it contains a pointer to data, and a pointer to the next elements
Whereas a linked list node has just one next pointer, a binary node tree has two child pointers, left and right
data:
left:
right:
![Page 16: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/16.jpg)
A node object for binary trees
data:
left:
right:
data:
left:
right:
data:
left:
right:
Similar to a linked list node, it contains a pointer to data, and a pointer to the next elements
Whereas a linked list node has just one next pointer, a binary node tree has two child pointers, left and right
![Page 17: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/17.jpg)
Heaps
![Page 18: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/18.jpg)
Heaps Heaps are one kind of binary tree They have a few special restrictions, in
addition to the usual binary tree ADT: Must be complete Ordering of data must obey heap
property Min-heap version: a parent’s data is always ≤
its children’s data Max-heap version: a parent’s data is always
≥ its children’s data
![Page 19: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/19.jpg)
How many of these could be valid heaps?
A. 0-1B. 2C. 3
D. 4E. 5-8
![Page 20: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/20.jpg)
How many of these are valid min-heaps?
A. 0B. 1C. 2D. 3
![Page 21: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/21.jpg)
In how many places could the largest number in this max-heap be located ?
A. 0-2B. 3-4C. 5-6D. 7-8
![Page 22: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/22.jpg)
In how many places could the largest number in this max-heap be located ?
A. 0-2B. 3-4C. 5-6D. 7-8
Max heaps are perfect for priority queues, because we always know where the highest priority item is
![Page 23: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/23.jpg)
In how many places could the number 35 be located in this min-heap?
A. 0-2B. 3-4C. 5-6D. 7-8
5
15
![Page 24: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/24.jpg)
In how many places could the number 35 be located in this min-heap?
A. 0-2B. 3-4C. 5-6D. 7-8
5
15
Min heaps also good for priority queues, if “high priority” in your system actually means low value (i.e. 1 means most important)
![Page 25: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/25.jpg)
Heap in an array
![Page 26: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/26.jpg)
Heap in an array We actually do NOT
typically use a node object to implement heaps
Because they must be complete, they fit nicely into an array, so we usually do that
data:
left:
right:
![Page 27: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/27.jpg)
Heap in an array
For tree of height h, array length is 2h-1 For a node in array index i:
Parent is at array index: A. i – 2B. i / 2C. (i – 1)/2D. 2i
![Page 28: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/28.jpg)
Heap in an array
For tree of height h, array length is 2h-1 For a node in array index i:
Left child is at array index:A. i +1B. i + 2C. 2i D. 2i + 1
![Page 29: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/29.jpg)
Heap in an array
For tree of height h, array length is 2h-1 For a node in array index i:
Parent is at array index: (i – 1)/2 Left child is at array index: 2i + 1 Right child is at array index: 2i + 2
![Page 30: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/30.jpg)
Heap insert and delete
![Page 31: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/31.jpg)
Heap insert
![Page 32: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/32.jpg)
Heap delete
![Page 33: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/33.jpg)
TRUE OR FALSE There is only one configuration of a valid
min-heap containing the elements {34, 22, 3, 9, 18}
A. TRUEB. FALSE
![Page 34: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/34.jpg)
Time cost What is the worst-case time cost for
each heap operation: Add, Remove, Peek?
A. O(n), O(1), O(1)B. O(logn), O(logn), O(1)C. O(n), O(logn), O(logn)D. Other/none/more
![Page 35: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/35.jpg)
Heapsort
![Page 36: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/36.jpg)
Heapsort is super easy1. Insert unsorted elements one at a time
into a heap until all are added2. Remove them from the heap one at a
time (we will always be removing the next biggest item, for max-heap; or next smallest item, for min-heap)
THAT’S IT!
![Page 37: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/37.jpg)
Implementing heapsortDevil’s in the details
![Page 38: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/38.jpg)
We can do the entire heapsort in place in one array Unlike mergesort, we don’t need a
separate array for our workspace We can do it all in place in one array
(the same array we were given as input)
![Page 39: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/39.jpg)
Build heap by inserting elements one at a time:
![Page 40: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/40.jpg)
Sort array by removing elements one at a time:
![Page 41: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/41.jpg)
Build heap by inserting elements one at a time IN PLACE:
![Page 42: CSE 12 – Basic Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062422/56813392550346895d9aa15b/html5/thumbnails/42.jpg)
Sort array by removing elements one at a time IN PLACE: