ITEC 2620M Introduction to Data Structures

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Dr. Andrew Wallace PhD BEng(hons) EurIng
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Grammars and Parsing  Recursive Definitions of Properties.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself. 
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Heaps & Priority Queues
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
HeapSort 25 March HeapSort Heaps or priority queues provide a means of sorting: 1.Construct a heap, 2.Add each item to it (maintaining the heap.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
"Teachers open the door, but you must enter by yourself. "
Partially Ordered Data ,Heap,Binary Heap
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Chapter 11 Heap.
Recitation 3 (Heap Sort and Binary Search Tree)
COSC160: Data Structures: Lists and Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Chapter 15 Lists Objectives
Heaps, Heap Sort and Priority Queues
Priority Queues and Heaps
October 30th – Priority QUeues
Hashing Exercises.
Source: Muangsin / Weiss
March 31 – Priority Queues and the heap
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
Cse 373 April 26th – Exam Review.
November 1st – Exam Review
CMSC 341 Lecture 13 Leftist Heaps
ITEC 2620M Introduction to Data Structures
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
structures and their relationships." - Linus Torvalds
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 8 – Binary Search Tree
structures and their relationships." - Linus Torvalds
Heapsort Heap & Priority Queue.
i206: Lecture 14: Heaps, Graphs intro.
CMSC 341 Lecture 14 Priority Queues & Heaps
B-Tree Insertions, Intro to Heaps
Heaps and the Heapsort Heaps and priority queues
Hassan Khosravi / Geoffrey Tien
ITEC 2620M Introduction to Data Structures
Computer Science 2 Heaps.
ITEC 2620M Introduction to Data Structures
"Teachers open the door, but you must enter by yourself. "
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Priority Queues (Chapter 6.6):
CS6045: Advanced Algorithms
Priority Queues & Heaps
Topic 5: Heap data structure heap sort Priority queue
CSE 12 – Basic Data Structures
DATA STRUCTURE.
Priority Queues (Chapter 6):
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
structures and their relationships." - Linus Torvalds
Presentation transcript:

ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zyang/itec2620m.htm Office: DB 3049

Stacks and Queues

Stack-based Recursion Computers use stacks to manage function calls/function returns. When a function is called, data is pushed onto the stack When a function finishes, data is popped off the stack Rather than using the computer’s stack, we can use our own to implement recursion! Binary tree traversal

Stacks and Recursion Any recursive algorithm can be implemented non-recursively. Do you have to use a function that calls itself? No. You can use a stack to implement recursion Do you have to use a recursive algorithm? Yes. How do you traverse a binary tree without a recursive algorithm?

Queues A queue is a “First-In, First-Out” = “FIFO” buffer. e.g. line-ups people enter from the back of the line people are served (exit) from the front of the line When elements are added, they are enqueued to the back. When elements are removed, they are dequeued from the front. enqueue() and dequeue() are the two defining functions of a queue. Example Link-based Queue

Priority Queues Previous queues were based on entry order (e.g. LIFO, FIFO). Priority queues are based on item value. Stacks and queues aren’t designed for searches. BST could work, but there is more overhead than we need. don’t need completely sorted queue – only need first element

Heaps and Heapsort

Key Points Heaps Make a heap Heapsort

Heaps Complete binary tree Partially ordered max heap – the value of every node is greater than its children min heap – the value of every node is smaller than its children No relationship between siblings, only ancestors (up-down) BST has left-right relationships.

Removing the Minimum Value Minimum value is at root, so we should remove this element. How do we maintain a complete binary tree? Put last value at the root and sift down Switch the value with the smallest of its children – continue Heap property is maintained.

Inserting a new value How do we maintain a complete binary tree? Put value at the bottom and sift up Heap property is maintained.

Complexity Analysis Remove minimum Insert Best  value stays at depth 1  O(1) Worst  value goes back to bottom  O(logn) Average  value goes half way O(logn) Insert Best  value stays at bottom  O(1) Worst  value goes to top  O(logn)

Making a Heap Array representation Make a heap Analysis of building a heap Heapsort