cse 12 – basic data structures
DESCRIPTION
- PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/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.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/2.jpg)
2
Today’s Topics
1. Heap implementation issues Heap uniqueness Heapsort in place HW suggestions
2. Back to generic binary trees In-order traversal Pre-order traversal Post-order traversal Level-order traversal (also called “breadth-
first”)
![Page 3: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/3.jpg)
Reading quiz!
![Page 4: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/4.jpg)
1. A node in a binary tree may have ___ children
A. 0 B. 1C. 2D. 3E. Other/none of the above/more than
one of the above
Reading quiz!
![Page 5: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/5.jpg)
2. A preorder traversal visits the current node, performs a preorder traversal of its ___ subtree and then performs a preorder traversal of the its ___ subtree.
A. right, rightB. left, rightC. left, leftD. right, left
Reading quiz!
![Page 6: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/6.jpg)
3. A full binary tree is a tree in which every node other than the leaves has ____ children.
A. 0B. 1C. 2D. 3E. Other/none of the above/more than
one of the above
Reading quiz!
![Page 7: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/7.jpg)
Heap uniqueness
![Page 8: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/8.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 9: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/9.jpg)
Heap outcomes by insert orderCreate a MIN-heap by inserting the elements, one by one, in the order given below for the first letter of your last name:
A-F: {3, 9, 18, 22, 34}
G-L: {3, 33, 18, 9, 34}
M-R: {9, 22, 18, 3, 34}
S-Z: {18, 22, 3, 9, 34}
![Page 10: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/10.jpg)
Heap outcomes by insert orderCreate a MIN-heap by inserting the elements, one by one, in the order given below for the first letter of your last name:
A-F: {18, 9, 34, 3, 22}
G-L: {3, 18, 22, 9, 34}
M-R: {22, 9, 34, 3, 18}
S-Z: {34, 3, 9, 18, 22}
![Page 11: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/11.jpg)
How many distinct min-heaps are possible for the elements {3, 9, 18, 22, 34} ?
A. 1B. 2-4C. 5-8D. 5! (5 factorial)E. Other/none/more
![Page 12: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/12.jpg)
Heapsort
![Page 13: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/13.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[smallest] item from the max-heap[min-heap], so the items come out in sorted order!
THAT’S IT!
![Page 14: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/14.jpg)
Example: using heapsort to print an array in sorted order (note: this is not the in-place version you need for HW)
public static void printSorted(String[] unsortedlist) {
Heap<String> heap = new Heap<String>();for (int i=0; i<unsortedlist.length; i++) {
heap.add(unsortedlist[i]);}while (!heap.isEmpty()) {
System.out.println(heap.remove());}
}
![Page 15: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/15.jpg)
Implementing heapsortDevil’s in the details
![Page 16: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/16.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 17: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/17.jpg)
Build heap by inserting elements one at a time:
![Page 18: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/18.jpg)
Sort array by removing elements one at a time:
![Page 19: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/19.jpg)
Build heap by inserting elements one at a time IN PLACE:
![Page 20: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/20.jpg)
Sort array by removing elements one at a time IN PLACE:
![Page 21: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/21.jpg)
Important tip! The in-place approach will not work if
your test to see if index i is past the end of the heap is to check if heaparray[i] is null.
Things will be in the array that are NOT in the heap!
You need to keep track of an int size (in addition to an int capacity) in order to check where the heap ends!
![Page 22: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/22.jpg)
Binary tree traversals
![Page 23: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/23.jpg)
Binary trees Recall from last time, a binary tree
is any tree where each node has 0, 1, or 2 children
That’s the only restriction Recall: heaps are a special case of binary
trees, and they have two additional restrictions
![Page 24: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/24.jpg)
Trees vs. Lists Lists have an
obvious ordering 1st element is
first, 2nd element is second, …
Trees don’t More than one
reasonable order
![Page 25: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/25.jpg)
Pre-order traversalpreorder(node) { if (node != null){
visit this node preorder(node.left) preorder(node.right)
}}
A. D B E A F C GB. A B D E C F G C. A B C D E F G
D. D E B F G C AE. Other/none/more
![Page 26: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/26.jpg)
Post-order traversalpostorder(node) { if (node != null){
postorder(node.left) postorder(node.right)visit this node
}}
A. D B E A F C GB. A B D E C F G C. A B C D E F G
D. D E B F G C AE. Other/none/more
![Page 27: CSE 12 – Basic Data Structures](https://reader035.vdocuments.site/reader035/viewer/2022062314/56813022550346895d95a582/html5/thumbnails/27.jpg)
In-order traversalinorder(node) { if (node != null){
inorder(node.left)visit this node inorder(node.right)
}}
A. D B E A F C GB. A B D E C F G C. A B C D E F G
D. D E B F G C AE. Other/none/more