l11-graphs

l11-graphs - CS112 Data Structures Lecture 11 Graphs CS112...

This preview shows pages 1–15. Sign up to view the full content.

CS112: Slides for Prof. Steinberg ʼ s lecture 1 Lecture 11 CS112: Data Structures CS112: Data Structures Lecture 11 Graphs

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 2 Lecture 11 Upcoming Schedule Upcoming Schedule Wed, July 20: 6-8:30 work on Proj. 3 with Binh & me here to help (bring laptops!) Mon, July 25: 6-7 recitation 7:10 - 8:30 review Wed, July 27: 6 - 7:20 Midterm 2 (info to be posted)
CS112: Slides for Prof. Steinberg ʼ s lecture 3 Lecture 11 Review: Priority Queues Review: Priority Queues Each data item has a priority Add items to queue in any order Highest Priority First Out add A:5, B:3, C:6 remove C add D:8 remove D, remove A

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 4 Lecture 11 Implement as an array Implement as an array Unsorted or sorted: insert or delete is O(n) Can we find a data structure that gives O(insert + delete) bettern than O(n)?
CS112: Slides for Prof. Steinberg ʼ s lecture 5 Lecture 11 Heap Heap A heap is a way to implement a priority queue with O(log n) complexity A heap is a complete binary tree all levels except maybe the last are full last level filled from left to right good bad

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 6 Lecture 11 Heap Heap The number at a node is greater than the number at any descendant 8 7 4 5 6 1 good bad 8 7 4 9 6 1
CS112: Slides for Prof. Steinberg ʼ s lecture 7 Lecture 11 Heap Insert Heap Insert Add node at end of last level Move up restoring order 8 7 3 5 6 1 9 8 7 3 5 9 1 6 9 7 3 5 8 1 6 9 7 3 5 8 1 6 4 9 7 4 5 8 1 6 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 8 Lecture 11 Heap Deletion Heap Deletion Copy out data at root Delete last node on last row & put data in root Move down restoring order 6 7 3 5 8 1 8 7 3 5 6 1
CS112: Slides for Prof. Steinberg ʼ s lecture 9 Lecture 11 Heap Deletion Heap Deletion Compare current node and two children if current node largest, stop if left node largest swap current and left ditto if right largest 1 7 3 5 6 7 1 3 5 6 7 5 3 1 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 10 Lecture 11 Heap Representation Heap Representation Store heap in an array For node at index j, children are at 2j+1 and 2j+2 Root at index 0 7 5 3 1 6 7 5 6 3 1 0 1 2 3 4
CS112: Slides for Prof. Steinberg ʼ s lecture 11 Lecture 11 Building a Heap from an Array Building a Heap from an Array Go from last non-leaf to index 0 At each node, do filter-down Work at a node is O(height of node) In a complete binary tree, majority of nodes close to bottom, so adds up to O(n)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 12 Lecture 11 Building a Heap from an Array Building a Heap from an Array 3 11 5 7 22 18 14 3 11 18 7 22 5 14 3 22 18 7 11 5 14 3 11 18 7 22 5 14
CS112: Slides for Prof. Steinberg ʼ s lecture 13 Lecture 11 Building a Heap from an Array Building a Heap from an Array 22 11 18 7 3 5 14 3 22 18 7 11 5 14 22 3 18 7 11 5 14

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 14 Lecture 11 Sorting with trees Sorting with trees Sort with heap or AVL tree:
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern