Binary Heaps Recall Queues Priority Queue ADT Potential

Total Page:16

File Type:pdf, Size:1020Kb

Binary Heaps Recall Queues Priority Queue ADT Potential Recall Queues CSE 326: Data Structures • FIFO: First-In, First-Out Priority Queues – Binary • Some contexts where this seems right? Heaps • Some contexts where some things should be allowed to skip ahead in the line? 1 2 Queues that Allow Line Jumping Priority Queue ADT • Need a new ADT 1. PQueue data : collection of data with • Operations: Insert an Item, priority Remove the “Best” Item 2. PQueue operations – insert – deleteMin 6 2 15 23 insert deleteMin 12 18 3. PQueue property: for two elements in the 45 3 7 queue, x and y, if x has a lower priority value than y, x will be deleted before y 3 4 Applications of the Priority Queue Potential Implementations • Select print jobs in order of decreasing length insert deleteMin • Forward packets on routers in order of urgency Unsorted list (Array) O(1) O(n) • Select most frequent symbols for compression Unsorted list (Linked-List) O(1) O(n) • Sort numbers, picking minimum first Sorted list (Array) O(n) O(1)* •Anythinggreedy Sorted list (Linked-List) O(n) O(1) 5 6 Recall From Lists, Queues, Binary Heap Properties Stacks • Use an ADT that corresponds to your 1. Structure Property needs 2. Ordering Property • The right ADT is efficient, while an overly general ADT provides functionality you aren’t using, but are paying for anyways • Heaps provide O(log n) worst case for both insert and deleteMin, O(1) average insert 7 8 Tree Review More Tree Terminology Tree T Tree T A A depth(B): root(T): leaves(T): B C height(G): B C children(B): degree(B): parent(H): D E F G D E F G siblings(E): branching factor(T): H I H I ancestors(F): descendents(G): subtree(C): J K L M N J K L M N 9 10 Brief interlude: Some Definitions: Heap Structure Property A Perfect binary tree – A binary tree with • A binary heap is a complete binary tree. all leaf nodes at the same depth. All Complete binary tree – binary tree that is completely filled, with the possible exception internal nodes have 2 children. of the bottom level, which is filled left to right. height h Examples: h+1 11 2 –1 nodes 2h –1 non-leaves 5 21 2h leaves 2 9 16 25 1 3 7 10 13 19 22 30 11 12 Representing Complete Why this approach to storage? Binary Trees in an Array 1 A From node i: 23B C 6 7 left child: 45D E F G 81011129 right child: H I J K L parent: implicit (array) implementation: ABCDEFGHI JKL 0 1 2 3 4 5 6 7 8 9 10 11 12 13 13 14 Heap Order Property Heap Operations Heap order property: For every non-root • findMin: node X, the value in the parent of X is • insert(val): percolate up. less than (or equal to) the value in X. • deleteMin: percolate down. 10 10 10 20 80 20 80 20 80 40 60 85 99 40 60 85 99 30 15 50 700 50 700 65 not a heap 15 16 Insert: percolate up Heap – Insert(val) 10 20 80 Basic Idea: 40 60 85 99 1. Put val at “next” leaf position 50 700 65 15 2. Percolate up by repeatedly exchanging node until no longer 10 needed 15 80 40 20 85 99 50 700 65 60 17 18 Insert Code (optimized) void insert(Object o) { int percolateUp(int hole, Heap – Deletemin assert(!isFull()); Object val) { while (hole > 1 && size++; val < Heap[hole/2]) Basic Idea: newPos = Heap[hole] = Heap[hole/2]; percolateUp(size,o); hole /= 2; } 1. Remove root (that is always the min!) Heap[newPos] = o; return hole; } } 2. Put “last” leaf node at root 3. Find smallest child of node 4. Swap node with its smallest child if runtime: needed. 5. Repeat steps 3 & 4 until no swaps needed. (Code in book) 19 20 DeleteMin: percolate down DeleteMin Code (Optimized) 10 Object deleteMin() { int percolateDown(int hole, assert(!isEmpty()); Object val) { 20 15 while (2*hole <= size) { returnVal = Heap[1]; left = 2*hole; right = left + 1; 40 60 85 99 size--; newPos = if (right ≤ size && Heap[right] < Heap[left]) 50 700 65 percolateDown(1, target = right; Heap[size+1]); else 15 Heap[newPos] = target = left; Heap[size + 1]; if (Heap[target] < val) { 20 65 return returnVal; Heap[hole] = Heap[target]; } hole = target; 40 60 85 99 } else 50 700 runtime: break; } 21 (code in book) return hole; 22 } Insert: 16, 32, 4, 69, 105, 43, 2 012345678 CSE 326: Data Structures Binary Heaps 23 24 Building a Heap Building a Heap 12 5 11 3 10 6 9 4 8 1 7 2 • Adding the items one at a time is O(n log n) in the worst case • I promised O(n) for today 25 26 BuildHeap: Floyd’s Method Working on Heaps 12 5 11 3 10 6 9 4 8 1 7 2 • What are the two properties of a heap? Add elements arbitrarily to form a complete tree. – Structure Property Pretend it’s a heap and fix the heap-order property! – Order Property 12 • How do we work on heaps? 5 11 – Fix the structure 3 10 6 9 – Fix the order 4 8 1 7 2 27 28 Buildheap pseudocode BuildHeap: Floyd’s Method private void buildHeap() { for ( int i = currentSize/2; i > 0; i-- ) 12 percolateDown( i ); } 5 11 3 10 6 9 4 8 1 7 2 runtime: 29 30 BuildHeap: Floyd’s Method BuildHeap: Floyd’s Method 12 12 12 5 11 5 11 5 11 3 10 2 9 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 1 7 6 4 8 10 7 6 31 32 BuildHeap: Floyd’s Method BuildHeap: Floyd’s Method 12 12 12 12 5 11 5 11 5 11 5 11 3 10 2 9 3 1 2 9 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 10 7 6 4 8 1 7 6 4 8 10 7 6 12 12 12 5 2 5 2 1 2 3 1 6 9 3 1 6 9 3 5 6 9 33 34 4 8 10 7 11 4 8 10 7 11 4 8 10 7 11 More Priority Queue Finally… Operations 1 • decreaseKey – given a pointer to an object in the queue, reduce its priority value 3 2 Solution: change priority and 4 5 6 9 ____________________________ 12 8 10 7 11 • increaseKey – given a pointer to an object in the queue, increase its priority value runtime: Why do we need a pointer? Why not simply data value? Solution: change priority and 35 36 _____________________________ More Priority Queue Operations Facts about Heaps Observations: • Remove(objPtr) • Finding a child/parent index is a multiply/divide by two – given a pointer to an object in the queue, • Operations jump widely through the heap remove the object from the queue • Each percolate step looks at only two new nodes • Inserts are at least as common as deleteMins Solution: set priority to negative infinity, percolate up to root and deleteMin Realities: • Division/multiplication by powers of two are equally fast • FindMax • Looking at only two new pieces of data: bad for cache! • With huge data sets, disk accesses dominate 37 38 Cycles to access: CPU A Solution: d-Heaps • Each node has d 1 Cache children • Still representable by 3 7 2 array Memory • Good choices for d: 4 8 5 12 11 10 6 9 – (choose a power of two for efficiency) 12 1 3 7 2 4 8 5 121110 6 9 – fit one set of children in Disk a cache line – fit one set of children on 39 40 a memory page/disk One More Operation CSE 326: Data Structures • Merge two heaps Priority Queues Leftist Heaps & Skew Heaps • Add the items from one into another? – O(n log n) • Start over and build it from scratch? –O(n) 41 42 New Heap Operation: Merge Leftist Heaps Given two heaps, merge them into one Idea: heap Focus all heap maintenance work in – first attempt: insert each element of the one small part of the heap smaller heap into the larger. runtime: Leftist heaps: 1. Most nodes are on the left – second attempt: concatenate binary heaps’ arrays and run buildHeap. 2. All the merging work is done on the right runtime: 43 44 Definition: Null Path Length Leftist Heap Properties null path length (npl) of a node x = the number of nodes between x • Heap-order property and a null in its subtree – parent’s priority value is ≤ to childrens’ priority OR npl(x) = min distance to a descendant with 0 or 1 children values – result: minimum element is at the root • npl(null) = -1 ? • npl(leaf) = 0 • npl(single-child node) = 0 ? ? • Leftist property – For every node x, npl(left(x)) ≥ npl(right(x)) Equivalent definitions: 0 1 ? 0 – result: tree is at least as “heavy” on the left as the right 1. npl(x) is the height of largest Are leftist trees… complete subtree rooted at x 0 0 0 2. npl(x) = 1 + min{npl(left(x)), npl(right(x))} complete? 45 balanced? 46 Right Path in a Leftist Tree is Short (#1) Are These Leftist? Claim: The right path is as short as any in the tree. 2 2 0 Proof: (By contradiction) 1 1 1 1 0 Pick a shorter path: D1 < D2 Say it diverges from right path at x x 0 1 0 0 1 0 0 0 1 npl(L) ≤ D1-1 because of the path of 0 0 0 0 0 0 0 0 length D1-1 to null L R D 1 D2 0 npl(R) ≥ D2-1 because every node on Every subtree of a leftist right path is leftist 0 tree is leftist! 0 47 Leftist property at x violated! 48 Right Path in a Leftist Tree is Short (#2) Why do we have the leftist Claim: If the right path has r nodes, then the tree property? has at least 2r-1 nodes.
Recommended publications
  • An Alternative to Fibonacci Heaps with Worst Case Rather Than Amortized Time Bounds∗
    Relaxed Fibonacci heaps: An alternative to Fibonacci heaps with worst case rather than amortized time bounds¤ Chandrasekhar Boyapati C. Pandu Rangan Department of Computer Science and Engineering Indian Institute of Technology, Madras 600036, India Email: [email protected] November 1995 Abstract We present a new data structure called relaxed Fibonacci heaps for implementing priority queues on a RAM. Relaxed Fibonacci heaps support the operations find minimum, insert, decrease key and meld, each in O(1) worst case time and delete and delete min in O(log n) worst case time. Introduction The implementation of priority queues is a classical problem in data structures. Priority queues find applications in a variety of network problems like single source shortest paths, all pairs shortest paths, minimum spanning tree, weighted bipartite matching etc. [1] [2] [3] [4] [5] In the amortized sense, the best performance is achieved by the well known Fibonacci heaps. They support delete and delete min in amortized O(log n) time and find min, insert, decrease key and meld in amortized constant time. Fast meldable priority queues described in [1] achieve all the above time bounds in worst case rather than amortized time, except for the decrease key operation which takes O(log n) worst case time. On the other hand, relaxed heaps described in [2] achieve in the worst case all the time bounds of the Fi- bonacci heaps except for the meld operation, which takes O(log n) worst case ¤Please see Errata at the end of the paper. 1 time. The problem that was posed in [1] was to consider if it is possible to support both decrease key and meld simultaneously in constant worst case time.
    [Show full text]
  • Data Structures and Algorithms Binary Heaps (S&W 2.4)
    Data structures and algorithms DAT038/TDA417, LP2 2019 Lecture 12, 2019-12-02 Binary heaps (S&W 2.4) Some slides by Sedgewick & Wayne Collections A collection is a data type that stores groups of items. stack Push, Pop linked list, resizing array queue Enqueue, Dequeue linked list, resizing array symbol table Put, Get, Delete BST, hash table, trie, TST set Add, ontains, Delete BST, hash table, trie, TST A priority queue is another kind of collection. “ Show me your code and conceal your data structures, and I shall continue to be mystified. Show me your data structures, and I won't usually need your code; it'll be obvious.” — Fred Brooks 2 Priority queues Collections. Can add and remove items. Stack. Add item; remove the item most recently added. Queue. Add item; remove the item least recently added. Min priority queue. Add item; remove the smallest item. Max priority queue. Add item; remove the largest item. return contents contents operation argument value size (unordered) (ordered) insert P 1 P P insert Q 2 P Q P Q insert E 3 P Q E E P Q remove max Q 2 P E E P insert X 3 P E X E P X insert A 4 P E X A A E P X insert M 5 P E X A M A E M P X remove max X 4 P E M A A E M P insert P 5 P E M A P A E M P P insert L 6 P E M A P L A E L M P P insert E 7 P E M A P L E A E E L M P P remove max P 6 E M A P L E A E E L M P A sequence of operations on a priority queue 3 Priority queue API Requirement.
    [Show full text]
  • Priority Queues and Binary Heaps Chapter 6.5
    Priority Queues and Binary Heaps Chapter 6.5 1 Some animals are more equal than others • A queue is a FIFO data structure • the first element in is the first element out • which of course means the last one in is the last one out • But sometimes we want to sort of have a queue but we want to order items according to some characteristic the item has. 107 - Trees 2 Priorities • We call the ordering characteristic the priority. • When we pull something from this “queue” we always get the element with the best priority (sometimes best means lowest). • It is really common in Operating Systems to use priority to schedule when something happens. e.g. • the most important process should run before a process which isn’t so important • data off disk should be retrieved for more important processes first 107 - Trees 3 Priority Queue • A priority queue always produces the element with the best priority when queried. • You can do this in many ways • keep the list sorted • or search the list for the minimum value (if like the textbook - and Unix actually - you take the smallest value to be the best) • You should be able to estimate the Big O values for implementations like this. e.g. O(n) for choosing the minimum value of an unsorted list. • There is a clever data structure which allows all operations on a priority queue to be done in O(log n). 107 - Trees 4 Binary Heap Actually binary min heap • Shape property - a complete binary tree - all levels except the last full.
    [Show full text]
  • Assignment 3: Kdtree ______Due June 4, 11:59 PM
    CS106L Handout #04 Spring 2014 May 15, 2014 Assignment 3: KDTree _________________________________________________________________________________________________________ Due June 4, 11:59 PM Over the past seven weeks, we've explored a wide array of STL container classes. You've seen the linear vector and deque, along with the associative map and set. One property common to all these containers is that they are exact. An element is either in a set or it isn't. A value either ap- pears at a particular position in a vector or it does not. For most applications, this is exactly what we want. However, in some cases we may be interested not in the question “is X in this container,” but rather “what value in the container is X most similar to?” Queries of this sort often arise in data mining, machine learning, and computational geometry. In this assignment, you will implement a special data structure called a kd-tree (short for “k-dimensional tree”) that efficiently supports this operation. At a high level, a kd-tree is a generalization of a binary search tree that stores points in k-dimen- sional space. That is, you could use a kd-tree to store a collection of points in the Cartesian plane, in three-dimensional space, etc. You could also use a kd-tree to store biometric data, for example, by representing the data as an ordered tuple, perhaps (height, weight, blood pressure, cholesterol). However, a kd-tree cannot be used to store collections of other data types, such as strings. Also note that while it's possible to build a kd-tree to hold data of any dimension, all of the data stored in a kd-tree must have the same dimension.
    [Show full text]
  • CS210-Data Structures-Module-29-Binary-Heap-II
    Data Structures and Algorithms (CS210A) Lecture 29: • Building a Binary heap on 풏 elements in O(풏) time. • Applications of Binary heap : sorting • Binary trees: beyond searching and sorting 1 Recap from the last lecture 2 A complete binary tree How many leaves are there in a Complete Binary tree of size 풏 ? 풏/ퟐ 3 Building a Binary heap Problem: Given 풏 elements {푥0, …, 푥푛−1}, build a binary heap H storing them. Trivial solution: (Building the Binary heap incrementally) CreateHeap(H); For( 풊 = 0 to 풏 − ퟏ ) What is the time Insert(푥,H); complexity of this algorithm? 4 Building a Binary heap incrementally What useful inference can you draw from Top-down this Theorem ? approach The time complexity for inserting a leaf node = ?O (log 풏 ) # leaf nodes = 풏/ퟐ , Theorem: Time complexity of building a binary heap incrementally is O(풏 log 풏). 5 Building a Binary heap incrementally What useful inference can you draw from Top-down this Theorem ? approach The O(풏) time algorithm must take O(1) time for each of the 풏/ퟐ leaves. 6 Building a Binary heap incrementally Top-down approach 7 Think of alternate approach for building a binary heap In any complete 98 binaryDoes treeit suggest, how a manynew nodes approach satisfy to heapbuild propertybinary heap ? ? 14 33 all leaf 37 11 52 32 nodes 41 21 76 85 17 25 88 29 Bottom-up approach 47 75 9 57 23 heap property: “Every ? node stores value smaller than its children” We just need to ensure this property at each node.
    [Show full text]
  • Rethinking Host Network Stack Architecture Using a Dataflow Modeling Approach
    DISS.ETH NO. 23474 Rethinking host network stack architecture using a dataflow modeling approach A thesis submitted to attain the degree of DOCTOR OF SCIENCES of ETH ZURICH (Dr. sc. ETH Zurich) presented by Pravin Shinde Master of Science, Vrije Universiteit, Amsterdam born on 15.10.1982 citizen of Republic of India accepted on the recommendation of Prof. Dr. Timothy Roscoe, examiner Prof. Dr. Gustavo Alonso, co-examiner Dr. Kornilios Kourtis, co-examiner Dr. Andrew Moore, co-examiner 2016 Abstract As the gap between the speed of networks and processor cores increases, the software alone will not be able to handle all incoming data without additional assistance from the hardware. The network interface controllers (NICs) evolve and add supporting features which could help the system increase its scalability with respect to incoming packets, provide Quality of Service (QoS) guarantees and reduce the CPU load. However, modern operating systems are ill suited to both efficiently exploit and effectively manage the hardware resources of state-of-the-art NICs. The main problem is the layered architecture of the network stack and the rigid interfaces. This dissertation argues that in order to effectively use the diverse and complex NIC hardware features, we need (i) a hardware agnostic representation of the packet processing capabilities of the NICs, and (ii) a flexible interface to share this information with different layers of the network stack. This work presents the Dataflow graph based model to capture both the hardware capabilities for packet processing of the NIC and the state of the network stack, in order to enable automated reasoning about the NIC features in a hardware-agnostic way.
    [Show full text]
  • Priorityqueue
    CSE 373 Java Collection Framework, Part 2: Priority Queue, Map slides created by Marty Stepp http://www.cs.washington.edu/373/ © University of Washington, all rights reserved. 1 Priority queue ADT • priority queue : a collection of ordered elements that provides fast access to the minimum (or maximum) element usually implemented using a tree structure called a heap • priority queue operations: add adds in order; O(log N) worst peek returns minimum value; O(1) always remove removes/returns minimum value; O(log N) worst isEmpty , clear , size , iterator O(1) always 2 Java's PriorityQueue class public class PriorityQueue< E> implements Queue< E> Method/Constructor Description Runtime PriorityQueue< E>() constructs new empty queue O(1) add( E value) adds value in sorted order O(log N ) clear() removes all elements O(1) iterator() returns iterator over elements O(1) peek() returns minimum element O(1) remove() removes/returns min element O(log N ) Queue<String> pq = new PriorityQueue <String>(); pq.add("Stuart"); pq.add("Marty"); ... 3 Priority queue ordering • For a priority queue to work, elements must have an ordering in Java, this means implementing the Comparable interface • many existing types (Integer, String, etc.) already implement this • if you store objects of your own types in a PQ, you must implement it TreeSet and TreeMap also require Comparable types public class Foo implements Comparable<Foo> { … public int compareTo(Foo other) { // Return > 0 if this object is > other // Return < 0 if this object is < other // Return 0 if this object == other } } 4 The Map ADT • map : Holds a set of unique keys and a collection of values , where each key is associated with one value.
    [Show full text]
  • Programmatic Testing of the Standard Template Library Containers
    Programmatic Testing of the Standard Template Library Containers y z Jason McDonald Daniel Ho man Paul Stro op er May 11, 1998 Abstract In 1968, McIlroy prop osed a software industry based on reusable comp onents, serv- ing roughly the same role that chips do in the hardware industry. After 30 years, McIlroy's vision is b ecoming a reality. In particular, the C++ Standard Template Library STL is an ANSI standard and is b eing shipp ed with C++ compilers. While considerable attention has b een given to techniques for developing comp onents, little is known ab out testing these comp onents. This pap er describ es an STL conformance test suite currently under development. Test suites for all of the STL containers have b een written, demonstrating the feasi- bility of thorough and highly automated testing of industrial comp onent libraries. We describ e a ordable test suites that provide go o d co de and b oundary value coverage, including the thousands of cases that naturally o ccur from combinations of b oundary values. We showhowtwo simple oracles can provide fully automated output checking for all the containers. We re ne the traditional categories of black-b ox and white-b ox testing to sp eci cation-based, implementation-based and implementation-dep endent testing, and showhow these three categories highlight the key cost/thoroughness trade- o s. 1 Intro duction Our testing fo cuses on container classes |those providing sets, queues, trees, etc.|rather than on graphical user interface classes. Our approach is based on programmatic testing where the number of inputs is typically very large and b oth the input generation and output checking are under program control.
    [Show full text]
  • CS 270 Algorithms
    CS 270 Algorithms Week 10 Oliver Kullmann Binary heaps Sorting Heapification Building a heap 1 Binary heaps HEAP- SORT Priority 2 Heapification queues QUICK- 3 Building a heap SORT Analysing 4 QUICK- HEAP-SORT SORT 5 Priority queues Tutorial 6 QUICK-SORT 7 Analysing QUICK-SORT 8 Tutorial CS 270 General remarks Algorithms Oliver Kullmann Binary heaps Heapification Building a heap We return to sorting, considering HEAP-SORT and HEAP- QUICK-SORT. SORT Priority queues CLRS Reading from for week 7 QUICK- SORT 1 Chapter 6, Sections 6.1 - 6.5. Analysing 2 QUICK- Chapter 7, Sections 7.1, 7.2. SORT Tutorial CS 270 Discover the properties of binary heaps Algorithms Oliver Running example Kullmann Binary heaps Heapification Building a heap HEAP- SORT Priority queues QUICK- SORT Analysing QUICK- SORT Tutorial CS 270 First property: level-completeness Algorithms Oliver Kullmann Binary heaps In week 7 we have seen binary trees: Heapification Building a 1 We said they should be as “balanced” as possible. heap 2 Perfect are the perfect binary trees. HEAP- SORT 3 Now close to perfect come the level-complete binary Priority trees: queues QUICK- 1 We can partition the nodes of a (binary) tree T into levels, SORT according to their distance from the root. Analysing 2 We have levels 0, 1,..., ht(T ). QUICK- k SORT 3 Level k has from 1 to 2 nodes. Tutorial 4 If all levels k except possibly of level ht(T ) are full (have precisely 2k nodes in them), then we call the tree level-complete. CS 270 Examples Algorithms Oliver The binary tree Kullmann 1 ❚ Binary heaps ❥❥❥❥ ❚❚❚❚ ❥❥❥❥ ❚❚❚❚ ❥❥❥❥ ❚❚❚❚ Heapification 2 ❥ 3 ❖ ❄❄ ❖❖ Building a ⑧⑧ ❄ ⑧⑧ ❖❖ heap ⑧⑧ ❄ ⑧⑧ ❖❖❖ 4 5 6 ❄ 7 ❄ HEAP- ⑧ ❄ ⑧ ❄ SORT ⑧⑧ ❄ ⑧⑧ ❄ Priority 10 13 14 15 queues QUICK- is level-complete (level-sizes are 1, 2, 4, 4), while SORT ❥ 1 ❚❚ Analysing ❥❥❥❥ ❚❚❚❚ QUICK- ❥❥❥ ❚❚❚ SORT ❥❥❥ ❚❚❚❚ 2 ❥❥ 3 ♦♦♦ ❄❄ ⑧ Tutorial ♦♦ ❄❄ ⑧⑧ ♦♦♦ ⑧ 4 ❄ 5 ❄ 6 ❄ ⑧⑧ ❄❄ ⑧ ❄ ⑧ ❄ ⑧⑧ ❄ ⑧⑧ ❄ ⑧⑧ ❄ 8 9 10 11 12 13 is not (level-sizes are 1, 2, 3, 6).
    [Show full text]
  • Kd Trees What's the Goal for This Course? Data St
    Today’s Outline - kd trees CSE 326: Data Structures Too much light often blinds gentlemen of this sort, Seeing the forest for the trees They cannot see the forest for the trees. - Christoph Martin Wieland Hannah Tang and Brian Tjaden Summer Quarter 2002 What’s the goal for this course? Data Structures - what’s in a name? Shakespeare It is not possible for one to teach others, until one can first teach herself - Confucious • Stacks and Queues • Asymptotic analysis • Priority Queues • Sorting – Binary heap, Leftist heap, Skew heap, d - heap – Comparison based sorting, lower- • Trees bound on sorting, radix sorting – Binary search tree, AVL tree, Splay tree, B tree • World Wide Web • Hash Tables – Open and closed hashing, extendible, perfect, • Implement if you had to and universal hashing • Understand trade-offs between • Disjoint Sets various data structures/algorithms • Graphs • Know when to use and when not to – Topological sort, shortest path algorithms, Dijkstra’s algorithm, minimum spanning trees use (Prim’s algorithm and Kruskal’s algorithm) • Real world applications Range Query Range Query Example Y A range query is a search in a dictionary in which the exact key may not be entirely specified. Bellingham Seattle Spokane Range queries are the primary interface Tacoma Olympia with multi-D data structures. Pullman Yakima Walla Walla Remember Assignment #2? Give an algorithm that takes a binary search tree as input along with 2 keys, x and y, with xÃÃy, and ÃÃ ÃÃ prints all keys z in the tree such that x z y. X 1 Range Querying in 1-D
    [Show full text]
  • Readings Findmin Problem Priority Queue
    Readings • Chapter 6 Priority Queues & Binary Heaps › Section 6.1-6.4 CSE 373 Data Structures Winter 2007 Binary Heaps 2 FindMin Problem Priority Queue ADT • Quickly find the smallest (or highest priority) • Priority Queue can efficiently do: item in a set ›FindMin() • Applications: • Returns minimum value but does not delete it › Operating system needs to schedule jobs according to priority instead of FIFO › DeleteMin( ) › Event simulation (bank customers arriving and • Returns minimum value and deletes it departing, ordered according to when the event › Insert (k) happened) • In GT Insert (k,x) where k is the key and x the value. In › Find student with highest grade, employee with all algorithms the important part is the key, a highest salary etc. “comparable” item. We’ll skip the value. › Find “most important” customer waiting in line › size() and isEmpty() Binary Heaps 3 Binary Heaps 4 List implementation of a Priority BST implementation of a Priority Queue Queue • What if we use unsorted lists: • Worst case (degenerate tree) › FindMin and DeleteMin are O(n) › FindMin, DeleteMin and Insert (k) are all O(n) • In fact you have to go through the whole list • Best case (completely balanced BST) › Insert(k) is O(1) › FindMin, DeleteMin and Insert (k) are all O(logn) • What if we used sorted lists • Balanced BSTs › FindMin and DeleteMin are O(1) › FindMin, DeleteMin and Insert (k) are all O(logn) • Be careful if we want both Min and Max (circular array or doubly linked list) › Insert(k) is O(n) Binary Heaps 5 Binary Heaps 6 1 Better than a speeding BST Binary Heaps • A binary heap is a binary tree (NOT a BST) that • Can we do better than Balanced Binary is: Search Trees? › Complete: the tree is completely filled except • Very limited requirements: Insert, possibly the bottom level, which is filled from left to FindMin, DeleteMin.
    [Show full text]
  • Binary Trees, Binary Search Trees
    Binary Trees, Binary Search Trees www.cs.ust.hk/~huamin/ COMP171/bst.ppt Trees • Linear access time of linked lists is prohibitive – Does there exist any simple data structure for which the running time of most operations (search, insert, delete) is O(log N)? Trees • A tree is a collection of nodes – The collection can be empty – (recursive definition) If not empty, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk, each of whose roots are connected by a directed edge from r Some Terminologies • Child and parent – Every node except the root has one parent – A node can have an arbitrary number of children • Leaves – Nodes with no children • Sibling – nodes with same parent Some Terminologies • Path • Length – number of edges on the path • Depth of a node – length of the unique path from the root to that node – The depth of a tree is equal to the depth of the deepest leaf • Height of a node – length of the longest path from that node to a leaf – all leaves are at height 0 – The height of a tree is equal to the height of the root • Ancestor and descendant – Proper ancestor and proper descendant Example: UNIX Directory Binary Trees • A tree in which no node can have more than two children • The depth of an “average” binary tree is considerably smaller than N, eventhough in the worst case, the depth can be as large as N – 1. Example: Expression Trees • Leaves are operands (constants or variables) • The other nodes (internal nodes) contain operators • Will not be a binary tree if some operators are not binary Tree traversal • Used to print out the data in a tree in a certain order • Pre-order traversal – Print the data at the root – Recursively print out all data in the left subtree – Recursively print out all data in the right subtree Preorder, Postorder and Inorder • Preorder traversal – node, left, right – prefix expression • ++a*bc*+*defg Preorder, Postorder and Inorder • Postorder traversal – left, right, node – postfix expression • abc*+de*f+g*+ • Inorder traversal – left, node, right.
    [Show full text]