Heaps & Priority Queues in the C++ STL 2-3 Trees



Similar documents
Binary Heap Algorithms

Linked Lists: Implementation Sequences in the C++ STL

Node-Based Structures Linked Lists: Implementation

Sequences in the C++ STL

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Converting a Number from Decimal to Binary

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

From Last Time: Remove (Delete) Operation

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Big Data and Scripting. Part 4: Memory Hierarchies

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Algorithms and Data Structures

Binary Search Trees CMPSC 122

CSE 326: Data Structures B-Trees and B+ Trees

DATABASE DESIGN - 1DL400

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Trees and Huffman Encoding Binary Search Trees

PES Institute of Technology-BSC QUESTION BANK

Recursion vs. Iteration Eliminating Recursion

Analysis of Algorithms I: Binary Search Trees

6 March Array Implementation of Binary Trees

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Ordered Lists and Binary Trees

How To Create A Tree From A Tree In Runtime (For A Tree)

APP INVENTOR. Test Review

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

TREE BASIC TERMINOLOGIES

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

DATA STRUCTURES USING C

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Lecture Notes on Binary Search Trees

Data Structures and Algorithms

Cpt S 223. School of EECS, WSU

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

A Comparison of Dictionary Implementations

CS711008Z Algorithm Design and Analysis

Exam study sheet for CS2711. List of topics

Full and Complete Binary Trees

10CS35: Data Structures Using C

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Algorithms and Data Structures Written Exam Proposed SOLUTION

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

The ADT Binary Search Tree

M-way Trees and B-Trees

Lecture Notes on Binary Search Trees

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Symbol Tables. Introduction

Chapter 14 The Binary Search Tree

Physical Data Organization

Algorithms and Data Structures

Chapter 13: Query Processing. Basic Steps in Query Processing

Sample Questions Csci 1112 A. Bellaachia

Fast Sequential Summation Algorithms Using Augmented Data Structures

Lecture Notes on Linear Search

Data Structure with C

Binary Heaps. CSE 373 Data Structures

Algorithms. Margaret M. Fleck. 18 October 2010

Data Structures. Level 6 C Module Descriptor

Binary Search Trees (BST)

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Data Structures and Algorithms(5)

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Data Structures Fibonacci Heaps, Amortized Analysis

recursion, O(n), linked lists 6/14

Data Structure [Question Bank]

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Binary Search Trees 3/20/14

Algorithms Chapter 12 Binary Search Trees

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Persistent Binary Search Trees

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Introduction to data structures

International Journal of Software and Web Sciences (IJSWS)

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

Java Software Structures

Algorithms and data structures

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

6. Standard Algorithms

Laboratory Module 6 Red-Black Trees

Review of Hashing: Integer Keys

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Data Structures. Topic #12

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Lecture 2 February 12, 2003

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Transcription:

Heaps & Priority Queues in the C++ STL 2-3 Trees CS 3 Data Structures and Algorithms Lecture Slides Friday, April 7, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell

Unit Overview Tables & Priority Queues Major Topics Introduction to Tables Priority Queues Binary Heap algorithms Heaps & Priority Queues in the C++ STL 2-3 Trees Other balanced search trees Hash Tables Prefix Trees Tables in various languages 7 Apr 2009 CS 3 Spring 2009 2

Review Introduction to Tables Sorted Array Unsorted Array Sorted Linked List Unsorted Linked List Binary Search Tree Balanced (how?) Binary Search Tree Retrieve Logarithmic Linear Linear Linear Linear Logarithmic Insert Linear Constant (?) Linear Constant Linear Logarithmic Delete Linear Linear Linear Linear Linear Logarithmic Idea #: Restricted Table Perhaps we can do better if we do not implement a Table in its full generality. Idea #2: Keep a Tree Balanced Balanced Binary Search Trees look good, but how to keep them balanced efficiently? Idea #3: Magic Functions Use an unsorted array of key-data pairs. Allow array items to be marked as empty. Have a magic function that tells the index of an item. Retrieve/insert/delete in constant time? (Actually no, but this is still a worthwhile idea.) We will look at what results from these ideas: From idea #: Priority Queues From idea #2: Balanced search trees (2-3 Trees, Red-Black Trees, B-Trees, etc.) From idea #3: Hash Tables 7 Apr 2009 CS 3 Spring 2009 3

Review Binary Heap Algorithms [/0] A Binary Heap (usually just Heap) is a complete Binary Tree in which no node has a data item that is less than the data item in either of its children. 56 50 25 5 22 25 3 0 3 2 Logical Structure In practice, we often use Heap to refer to the array-based complete Binary Tree implementation. Physical Structure 56 50 25 5 22 25 3 0 3 2 7 Apr 2009 CS 3 Spring 2009 4

Review Binary Heap Algorithms [2/0] We can use a Heap to implement a Priority Queue. Like a Table, but retrieve/delete only highest key. Retrieve is called getfront. Key is called priority. 56 Insert any key-data pair. Algorithms for the Three Primary Operations 5 GetFront Get the root node. Constant time. Insert 3 0 3 2 50 25 22 25 56 50 25 5 22 25 3 0 3 2 Add new node to end of Heap, trickle up. Logarithmic time if no reallocate-and-copy required. Linear time if it may be required. Note: Heaps often do not manage their own memory, in which case the reallocation will not be part of the Heap operation. Delete Swap first & last items, reduce size of Heap, trickle down root. Logarithmic time. Faster than linear time! 7 Apr 2009 CS 3 Spring 2009 5

Review Binary Heap Algorithms [3/0] To insert into a Heap, add a new node at the end. Then trickle up. If new value is greater than its parent, swap them. Repeat at new position. 56 56 50 25 50 25 5 22 25 5 22 32 3 0 3 2 32 3 0 3 2 25 56 50 32 Done 5 22 25 3 0 3 2 25 7 Apr 2009 CS 3 Spring 2009 6

Review Binary Heap Algorithms [4/0] To delete the root item from a Heap, swap root and last items, and reduce size of Heap by one. Then trickle down the new root item. If the root is all its children, stop. Otherwise, swap the root item with its largest child and recursively fix the proper subtree. 56 2 50 25 50 25 5 22 25 5 22 25 3 0 3 2 3 0 3 50 50 5 2 25 22 25 5 22 25 Done 2 20 3 0 3 3 0 3 7 Apr 2009 CS 3 Spring 2009 7

Review Binary Heap Algorithms [5/0] Heap insert and delete are usually given a random-access range. The item to insert or delete is last item of the range; the rest is a Heap. Action of Heap insert: That s where we want to put the item, initially (right?). Given Heap Action of Heap delete: Item to insert New Heap That s where the swap puts it (right?). Given Heap New Heap Item deleted Note that Heap algorithms can do all their work using swap. This usually allows for both speed and safety. 7 Apr 2009 CS 3 Spring 2009 8

Review Binary Heap Algorithms [6/0] To turn a random-access range (array?) into a Heap, we could do n Heap inserts. Each insert operation is O(log n), and so making a Heap in this way is O(n log n). However, we can make a Heap faster than this. Place each item into a partially-made Heap, in backwards order. Trickle each item down through its descendants. For most items, there are not very many of these. 9 3 4 3 8 9 3 Bottom items: no trickling necessary 9 3 9 3 9 8 4 3 8 4 3 8 4 3 8 4 3 3 9 9 9 8 9 8 8 4 8 4 3 3 4 3 3 4 3 3 3 3 This Heap make method is linear time! 9 4 8 3 3 7 Apr 2009 CS 3 Spring 2009 9

Review Binary Heap Algorithms [7/0] Our last sorting algorithm is Heap Sort. This is a sort that uses Heap algorithms. We can think of it as using a Priority Queue, where the priority of an item is its value except that the algorithm is in-place, using no separate data structure. Procedure: Make a Heap, then delete all items, using the delete procedure that places the deleted item in the top spot. We do a make operation, which is O(n), and n getfront/delete operations, each of which is O(log n). Total: O(n log n). 7 Apr 2009 CS 3 Spring 2009 0

Review Binary Heap Algorithms [8/0] Below: Heap make operation. Next slide: Heap deletion phase. Start 3 2 4 Add 2 3 2 4 4 2 3 4 2 4 Add 4 3 2 4 2 Add 3 2 4 Note: This is what happens in memory. This is just a picture of the logical structure. 4 4 Add 3 Now the entire array is a Heap. 3 4 2 4 3 2 7 Apr 2009 CS 3 Spring 2009 2 2 4 3 3 4

Review Binary Heap Algorithms [9/0] Heap deletion phase: Delete 3 3 2 4 Start Delete 4 4 3 2 4 3 2 2 4 3 2 3 4 2 3 4 2 2 2 3 4 2 3 Delete 2 2 3 4 2 3 4 3 2 4 3 2 Delete (does nothing; can be skipped) 2 3 4 2 3 4 Now the array is sorted. 7 Apr 2009 CS 3 Spring 2009 2

Review Binary Heap Algorithms [0/0] Efficiency Heap Sort is O(n log n). Requirements on Data Heap Sort requires random-access data. Space Usage Heap Sort is in-place. Stability Heap Sort is not stable. Performance on Nearly Sorted Data We have seen these together before (Iterative Merge Sort on a Linked List), but never for an array. Heap Sort is not significantly faster or slower for nearly sorted data. Notes Heap Sort can be generalized to handle sequences that are modified (in certain ways) in the middle of sorting. Recall that Heap Sort is used by Introsort, when the recursion depth of Quicksort exceeds the maximum allowed. 7 Apr 2009 CS 3 Spring 2009 3

Heaps & Priority Queues in the C++ STL Heap Algorithms The C++ STL includes several Heap algorithms. These operate on ranges specified by pairs of random-access iterators. Any random-access range can be a Heap: array, vector, deque, part of these, etc. An STL Heap is a Maxheap with an optional client-specified comparison. Heap algorithms are used by STL Priority Queues (std::priority_queue). Example: std::push_heap (in <algorithm>) inserts into an existing Heap. Called as std::push_heap(first, last). Assumes [first, last) is nonempty, and [first, last-) is already a Heap. Inserts *(last-) into the Heap. Similarly: std::pop_heap Heap delete operation. Puts the deleted element in *(last-). std::make_heap Make a range into a Heap. std::sort_heap Is given a Heap. Does a bunch of pop_heap calls. Calling make_heap and then sort_heap does Heap Sort. std::is_heap Tests whether a range is a Heap. 7 Apr 2009 CS 3 Spring 2009 4

Heaps & Priority Queues in the C++ STL std::priority_queue Introduction The STL has a Priority Queue: std::priority_queue, in <queue>. STL documentation does not call std::priority_queue a container, but rather a container adapter. This is because std::priority_queue is explicitly a wrapper around some other container. You get to pick what that container is. You say std::priority_queue<t, container<t> >. T is the value type. container can be std::vector or std::deque. container<t> can be any standard-conforming random-access sequence container. container defaults to std::vector. You can say just std::priority_queue<t> to get std::priority_queue<t, std::vector<t> >. 7 Apr 2009 CS 3 Spring 2009 5

Heaps & Priority Queues in the C++ STL std::priority_queue Members The member function names used by std::priority_queue are the same as those used by std::stack. Not those used by std::queue. Thus, std::priority_queue has top, not front. Given a variable pq of type std::priority_queue<t>, you can do: pq.top() pq.push(item) item is some value of type T. pq.pop() pq.empty() pq.size() 7 Apr 2009 CS 3 Spring 2009 6

Heaps & Priority Queues in the C++ STL std::priority_queue Comparison How do we specify an item s priority? We really don t need to know an item s priority; we only need to know, given two items, which has the higher priority. Thus, we use a comparison, which defaults to operator<. A third, optional template parameter is a comparison object : std::priority_queue<t, std::vector<t>, compare> Comparison objects work the same as those passed to STL sorting algorithms (std::sort, etc.) and STL Heap algorithms. So, for example, a priority queue of ints whose highest priority items are those with the lowest value, would have the following type: std::priority_queue<int, std::vector<int>, std::greater<int>()> 7 Apr 2009 CS 3 Spring 2009 7

Overview of Advanced Table Implementations We will cover the following advanced Table implementations. Balanced Search Trees Binary Search Trees are hard to keep balanced, so to make things easier we allow more than 2 children: 2-3 Tree Up to 3 children 2-3-4 Tree Up to 4 children Red-Black Tree Binary-tree representation of a 2-3-4 tree Or back up and try a balanced Binary Tree again: AVL Tree Alternatively, forget about trees entirely: Hash Tables Finally, the Radix Sort of Table implementations : Prefix Tree 7 Apr 2009 CS 3 Spring 2009 8

2-3 Trees Introduction & Definition [/3] Obviously (?) a Binary Search Tree is a useful idea. The problem is keeping it balanced. Or at least keeping the height small. It turns out that small height is much easier to maintain if we allow a node to have more than 2 children. But if we do this, how do we maintain the search tree concept? We generalize the idea of an inorder traversal. For each pair of consecutive subtrees, a node has one data item lying between the values in these subtrees. 20 7 2 32 2 4 9 8 23 28 35 7 Apr 2009 CS 3 Spring 2009 9

2-3 Trees Introduction & Definition [2/3] A Binary-Search-Tree style node is a 2-node. This is a node with 2 subtrees and data item. The item s value lies between the values in the two subtrees. In a 2-3 Tree we also allow a node to be a 3-node. This is a node with 3 subtrees and 2 data items. Each of the 2 data items has a value that lies between the values in the corresponding pair of consecutive subtrees. Later, we will look at 2-3-4 trees, which can also have 4-nodes. 2 2 subtrees item ordering 3 subtrees 2 items ordering 3 4 subtrees 3 items ordering 0 2 5 2-node 0 3-node 3 9 3 9 4-node 2 5 0 7 Like a Binary- Search-Tree node 5 7 9 7 7 Apr 2009 CS 3 Spring 2009 20

2-3 Trees Introduction & Definition [3/3] A 2-3 Search Tree (generally we just say 2-3 Tree) is a tree with the following properties. All nodes contain either or 2 data items. If 2 data items, then the first is the second. All leaves are at the same level. 2 4 All non-leaves are either 2-nodes or 3-nodes. They must have the associated order properties. 7 2 9 8 20 32 23 28 35 7 Apr 2009 CS 3 Spring 2009 2

2-3 Trees Operations Traverse & Retrieve How do we traverse a 2-3 Tree? We generalize the procedure for doing an inorder traversal of a Binary Search Tree. For each leaf, go through the items in it. For each non-leaf 2-node: Traverse subtree. Do item. Traverse subtree 2. For each non-leaf 3-node: Traverse subtree. Do item. Traverse subtree 2. Do item 2. Traverse subtree 3. 2 4 7 2 9 8 This procedure lists all the items in sorted order. How do we retrieve by key in a 2-3 Tree? Start at the root and proceed downward, making comparisons, just as in a Binary Search Tree. 3-nodes make the procedure slightly more complex. 20 32 23 28 35 7 Apr 2009 CS 3 Spring 2009 22

2-3 Trees Operations Insert & Delete How do we insert & delete in a 2-3 Tree? These are the tough problems. It turns out that both have efficient [O(log n)] algorithms, which is why we like 2-3 Trees. 7 Apr 2009 CS 3 Spring 2009 23

2-3 Trees Operations Insert [/4] Basic ideas behind the 2-3 Tree insert algorithm: Allow nodes to expand when legal. If a node gets too big (3 items), split the subtree rooted at that node and propagate the middle item upward. If we end up splitting the entire tree, then we create a new root node, and all the leaves advance one level simultaneously. Example : Insert 0. 20 20 7 2 32 7 2 32 2 4 9 8 23 28 35 2 4 9 0 8 23 28 35 7 Apr 2009 CS 3 Spring 2009 24

2-3 Trees Operations Insert [2/4] Example 2: Insert 5. Over-full nodes are blue. 20 20 7 2 32 7 2 32 2 4 9 8 23 28 35 2 4 5 9 8 23 28 35 20 7 20 4 7 2 32 4 2 32 2 5 9 8 23 28 35 2 5 9 8 23 28 35 7 Apr 2009 CS 3 Spring 2009 25

2-3 Trees Operations Insert [3/4] Example 3: Insert 5. Here we see how a 2-3 Tree increases in height. 7 2 7 2 2 4 9 8 2 4 5 9 8 7 4 7 2 4 2 2 5 9 8 2 5 9 8 7 Apr 2009 CS 3 Spring 2009 26

2-3 Trees Operations Insert [4/4] In the middle of a 2-3 Tree insertion, overfull nodes are always leaves or 4-nodes. (Of course 4-nodes are illegal.) This is why we can always split at the middle item. 2-3 Tree insertion can be thought of recursively. Insert into a node. This node will be a leaf the first time. If the node is overfull, split and move the middle item up. We split the subtree rooted at the node. If the node is a leaf, this is easy. If the node is not a leaf, this is more work, but not hard to understand. Moving an item up is inserting into the parent. So we recurse. Or we make increase the height (by making a new root), and we are done. 7 Apr 2009 CS 3 Spring 2009 27

2-3 Trees TO BE CONTINUED 2-3 Trees will be continued next time. 7 Apr 2009 CS 3 Spring 2009 28