1 / 13

ITEC 2620M Introduction to Data Structures

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.

tammyl
Download Presentation

ITEC 2620M Introduction to Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Stacks and Queues

  3. 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

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

  5. 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

  6. 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

  7. Heaps and Heapsort

  8. Key Points • Heaps • Make a heap • Heapsort

  9. 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.

  10. 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.

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

  12. 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)

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

More Related