# Itec 3220 - people.math.yorku.ca

ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zy ang/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 computers stack, we can use our own to implement recursion! Binary tree traversal

3 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? 4 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

5 Priority Queues Previous queues were based on entry order (e.g. LIFO, FIFO). Priority queues are based on item value. Stacks and queues arent designed for searches. BST could work, but there is more

overhead than we need. dont need completely sorted queue only need first element 6 Heaps and Heapsort Key Points Heaps Make a heap Heapsort

8 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 (updown) BST has left-right relationships. 9 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. 10 Inserting a new value How do we maintain a complete binary tree?

Put value at the bottom and sift up Heap property is maintained. 11 Complexity Analysis Remove minimum 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) Average value goes half way O(logn) 12

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

## Recently Viewed Presentations

• Types of Swine Enterprises. Farrow-to-finish farms manage all stages of pig growth and development, from breeding through finishing, to market weight of about 275 pounds.. Farrow-to-nursery farms specialize in the breeding of sows and raising of 40- to 50-pound feeder...
• Segoe Arial Wingdings 2 Segoe Light Times New Roman Segoe Semibold PMingLiU Segoe Condensed Segoe Black Courier New Verdana TechEd06_Template Windows Vista System Integrity Technologies Why? The bad guys are everywhere! Cool stuff! Protect the OS When Running The threats...
• Primate Evolution. As a result of the change in climate and vegetation, adaptive radiation. occurred among the Old World primates. During late Miocene, primates appeared that gave rise to the pongids (orangutan, chimpanzee, and gorilla) and the hominids (human family).
• If a hospital does periodic microbiologic culturing and 20% of sampled endoscopes are positive, what actions should be undertaken (e.g., patient notification with an offer of BBP testing, stool exam for CRE) Trigger based on level of contamination or frequency...
• TONE What it is and how to recognise it … What is tone? The writer's or speaker's attitude toward the subject & the audience. With speaking, tone is easy to hear in a person's voice. With writing, tone is determined...
• Book Antiqua Arial Lucida Sans Wingdings 2 Wingdings Wingdings 3 Calibri Bwgrki Bwgrkl Bookman Old Style Apex 1_Apex The Greek Alphabet Slide 2 Slide 3 Slide 4 Slide 5 Slide 6 Slide 7 Slide 8 Slide 9 Slide 10 Slide...
• Any action that is rude, discourteous, offensive, intimidating, or hostile that interferes with the learning and/or practice environment. Evidence suggests that incivility on American college campuses, ranging from insulting remarks and verbal abuse to violence, is a serious and growing...
• Destructive interference. Occurs when the two waves oscillate the particles of the medium in opposite directions at a given point in space. Causes a decrease in amplitude (and thus energy) at that point. Causes a decrease (or absence!) of light,...