Priority Queues and Huffman Trees

Similar documents
Expression Trees and the Heap

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

Review of the Lectures 21-26, 30-32

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

ITI Introduction to Computing II

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

ITI Introduction to Computing II

The heap is essentially an array-based binary tree with either the biggest or smallest element at the root.

CSCI 104 Binary Trees / Priority Queues / Heaps. Mark Redekopp Michael Crowley

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CMSC 341 Lecture 15 Leftist Heaps

CMSC 341 Lecture 15 Leftist Heaps

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

CSCI Trees. Mark Redekopp David Kempe

the Queue queue ADT using the STL queue designing the simulation simulation with STL queue using STL list as queue using STL vector as queue

CS106X Handout 35 Winter 2018 March 12 th, 2018 CS106X Midterm Examination

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

CMSC 341 Leftist Heaps

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

Chapter 20: Binary Trees

Queue Implementations

CS24 Week 8 Lecture 1

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Priority Queues (Heaps)

Tables, Priority Queues, Heaps

PRIORITY QUEUES AND HEAPS

Priority Queues and Binary Heaps

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Priority queues. Priority queues. Priority queue operations

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

CSE 214 Computer Science II Heaps and Priority Queues

Programming II (CS300)

CSE100. Advanced Data Structures. Lecture 12. (Based on Paul Kube course materials)

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

CMSC 341 Lecture 10 Binary Search Trees

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Priority Queues. 04/10/03 Lecture 22 1

Priority Queues (Heaps)

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

Chapter 9. Priority Queue

INF2220: algorithms and data structures Series 1

LECTURE 11 TREE TRAVERSALS

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

CS102 Binary Search Trees

Name CPTR246 Spring '17 (100 total points) Exam 3

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

CmpSci 187: Programming with Data Structures Spring 2015

Data Structures in Java

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

PRIORITY QUEUES AND HEAPS

Algorithms and Data Structures

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

CSE100. Advanced Data Structures. Lecture 13. (Based on Paul Kube course materials)

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

void insert( Type const & ) void push_front( Type const & )

Binary Trees and Binary Search Trees

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Heaps. Heaps. A heap is a complete binary tree.

CSE373 Fall 2013, Midterm Examination October 18, 2013

TREES Lecture 10 CS2110 Spring2014

Programming II (CS300)

Templates and Vectors

15 July, Huffman Trees. Heaps

Winter 2018 March 12 th, 2018 CS106X Midterm Examination Solution

Lecture Notes on Priority Queues

CS2 Algorithms and Data Structures Note 6

SFU CMPT Lecture: Week 9

Heaps. A complete binary tree can be easily stored in an array - place the root in position 1 (for convenience)

PRIORITY QUEUES AND HEAPS

3137 Data Structures and Algorithms in C++

Data Structures and Algorithms

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

list<t>::const_iterator it1 = lst.begin(); // points to first element list<t>::const_iterator it2 = lst.begin(); // points to second element it2++;

EE 368. Weeks 5 (Notes)

Fall, 2015 Prof. Jungkeun Park

Data Structures and Algorithms for Engineers

Final Exam Solutions PIC 10B, Spring 2016

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Binary Heaps in Dynamic Arrays

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

CS106X Handout 39 Autumn 2012 November 28 th, 2012 CS106X Midterm Examination II

Algorithms Dr. Haim Levkowitz

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

- Alan Perlis. Topic 24 Heaps

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Largest Online Community of VU Students

16 Greedy Algorithms

CS302 Midterm Exam Answers & Grading James S. Plank September 30, 2010

TREES Lecture 12 CS2110 Spring 2018

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

TREES. Trees - Introduction

Transcription:

Priority Queues and Huffman Trees 1 the Heap storing the heap with a vector deleting from the heap 2 Binary Search Trees sorting integer numbers deleting from a binary search tree 3 Huffman Trees encoding messages a recursive tree creation algorithm MCS 360 Lecture 26 Introduction to Data Structures Jan Verschelde, 27 October 2017 Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 1 / 32

Priority Queues and Huffman Trees 1 the Heap storing the heap with a vector deleting from the heap 2 Binary Search Trees sorting integer numbers deleting from a binary search tree 3 Huffman Trees encoding messages a recursive tree creation algorithm Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 2 / 32

a heap as a vector A heap is a binary tree: top 90 65 70 21 60 bottom 27 17 90 65 70 21 60 27 17 0 1 2 3 4 5 6 For node at p: left child is at 2p + 1, right child is at 2p + 2. Parent of node at p is at (p 1)/2. Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 3 / 32

Priority Queues and Huffman Trees 1 the Heap storing the heap with a vector deleting from the heap 2 Binary Search Trees sorting integer numbers deleting from a binary search tree 3 Huffman Trees encoding messages a recursive tree creation algorithm Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 4 / 32

popping from a heap 90 65 70 21 60 27 17 We replace the top first with the bottom, and then swap as long as parent less than largest child: 17 70 70 65 70 65 17 65 27 21 60 27 21 60 27 21 60 17 Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 5 / 32

the class Heap class Heap { private: std::vector<int> h; int index_to_bottom; int max_child( int p ); // returns index of largest // child or -1 if no child void swap_from_top( int p ); // swaps the element at p with // its largest child if that child // has value larger than the parent Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 6 / 32

largest child int Heap::max_child( int p ) { if(index_to_bottom <= p) return -1; else { int left = 2*p+1; int right = 2*p+2; if(left > index_to_bottom) return -1; else { if(right > index_to_bottom) return left; else return (h[left] > h[right])? } } } left : right; Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 7 / 32

popping void Heap::pop() { if(index_to_bottom == 0) index_to_bottom--; else { h[0] = h[index_to_bottom--]; swap_from_top(0); } } void Heap::swap_from_top( int p ) { if(index_to_bottom == -1) return; int c = max_child(p); if(c == -1) return; if(h[c] > h[p]) { int t = h[p]; h[p] = h[c]; h[c] = t; swap_from_top(c); } } Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 8 / 32

sorting with a heap 90 65 70 21 60 27 17 70 65 27 21 60 17 65 60 27 21 17 17 60 21 27 27 21 17 21 17 17 Terminology: a heap is a complete binary tree. Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 9 / 32

the STL priority_queue A heap implements a priority queue. #include <queue> using namespace std; int main() { priority_queue<int> q; Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 10 / 32

push(), top(), and pop() Pushing n random numbers: for(int i=0; i<n; i++) { int r = 10+rand() % 90; q.push(r); } Sorting with top and pop: vector<int> result; for(; q.size() > 0; q.pop()) result.push_back(q.top()); The numbers in result are in decreasing order. Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 11 / 32

Priority Queues and Huffman Trees 1 the Heap storing the heap with a vector deleting from the heap 2 Binary Search Trees sorting integer numbers deleting from a binary search tree 3 Huffman Trees encoding messages a recursive tree creation algorithm Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 12 / 32

the Abstract Data Type of a binary search tree abstract <typename T> binary_search_tree; /* A binary search tree is a binary tree where everything less than the data element is in the left subtree and everyting else is in the right subtree. */ abstract void insert ( binary_search_tree t, T item ); postcondition: if empty(t), then after insert(t, item) we have item = data_element(t), it not empty(t), then: if item < data_element(t), then item is in left_subtree(t), if item >= data_element(t), then item is in right_subtree(t); Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 13 / 32

Sorting Numbers using a Tree Consider the sequence 4, 5, 2, 3, 8, 1, 7 Insert the numbers in a tree: Rules to insert x at node N: if N is empty, then put x in N 4 2 5 1 3 8 7 if x < N, insert x to the left of N if x N, insert x to the right of N Recursive printing: left, node, right sorts the sequence. Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 14 / 32

finding smallest element Recursive algorithm to find smallest element? Tree Tree::smallest() const { if(root == NULL) return NULL; else if(root->left == NULL) return Tree(root); else { Tree L = this->get_left(); return L.smallest(); } } Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 15 / 32

Priority Queues and Huffman Trees 1 the Heap storing the heap with a vector deleting from the heap 2 Binary Search Trees sorting integer numbers deleting from a binary search tree 3 Huffman Trees encoding messages a recursive tree creation algorithm Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 16 / 32

deleting the root Assume the binary search tree is not empty. We want to delete the root node. right or left child is null: r S r S removing root r leads to subtree S left or right child is null: 2 1 3 4 3 1 4 otherwise... Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 17 / 32

otherwise...the general case Removing the root: 2 1 5 3 6 4 3 1 5 4 6 Algorithm: 1 Find the smallest element of the right child. 2 Replace the root with that smallest element. 3 Update the parent of node of smallest element. Observe that the smallest element has no left child. Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 18 / 32

finding the parent node We have already code to find the smallest element. Needed: find the parent of the smallest node. The base cases: Node* Tree::find_parent_node(int item) const { if(root == NULL) return NULL; else if(root->data == item) return NULL; else { // general case on the next slide } Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 19 / 32

finding the parent in general else bool found = false; Node *parent = root; Node *child; do { if(item < parent->data) child = parent->left; else child = parent->right; if(child == NULL) return NULL; else if(child->data == item) found = true; else parent = child; } while(!found); return parent; Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 20 / 32

removing any item Knowing the removal of the root of a binary search tree, canweworkwitha localroot? Given an item that occurs in the search tree: 1 Find the item and its parent. 2 Consider the item as a local root node. 3 Update the appropriate child of the parent with the tree that has the item removed. Why does this approach work? Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 21 / 32

Priority Queues and Huffman Trees 1 the Heap storing the heap with a vector deleting from the heap 2 Binary Search Trees sorting integer numbers deleting from a binary search tree 3 Huffman Trees encoding messages a recursive tree creation algorithm Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 22 / 32

binary codes A Huffman tree is a binary tree with data at leaves. Turn left: add 0, turn right: add 1 to code. Vowels e, a, o are more frequent than o and u. binary code 0 1 e 0 0 0 1 0 1 a 0 1 e a o 0 o 1 0 1 i 1 1 0 i u u 1 1 1 Decode bit string by walking the tree, encode: use table. Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 23 / 32

encoding messages A is the alphabet {a 1, a 2,...,a n } of symbols, a message M is a sequence of elements of A. f M (a i ) is the frequency of the symbol a i occurring in M d H (a i ) = depth of a i in Huffman tree = #bits in encoding of a i #bits of M as encoded by Huffman tree H: M H = f M (a) d H (a) a A Goal: find optimal Huffman tree H for message M. Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 24 / 32

Priority Queues and Huffman Trees 1 the Heap storing the heap with a vector deleting from the heap 2 Binary Search Trees sorting integer numbers deleting from a binary search tree 3 Huffman Trees encoding messages a recursive tree creation algorithm Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 25 / 32

a recursive idea For two symbols, n = 2: a 1 0, a 2 1. Order symbols along their frequencies in message M: f M (a 1 ) f M (a 2 ) f M (a 3 ) f M (a n ) Replace a 1 and a 2 by a 1,2, f M (a 1,2 )=f M (a 1 )+f M (a 2 ), then M n 1 is the message M over {a 1,2, a 3,...,a n } and let H n 1 be the optimal Huffman tree to encode M n 1. A Huffman tree H for M is then obtained via a 1,2 0 1 a 1 a 2 Claim: this H obtained recursively is optimal for M. Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 26 / 32

running the algorithm f 5 ( ) e 103 a 64 o 63 i 57 u 23 f 4 ( ) e 103 iu 80 a 64 o 63 f 3 ( ) ao 127 e 103 iu 80 f 2 ( ) eiu 183 ao 127 0 1 ao eiu Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 27 / 32

running the algorithm f 5 ( ) e 103 a 64 o 63 i 57 u 23 f 4 ( ) e 103 iu 80 a 64 o 63 f 3 ( ) ao 127 e 103 iu 80 0 1 ao 0 1 e iu Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 28 / 32

running the algorithm f 5 ( ) e 103 a 64 o 63 i 57 u 23 f 4 ( ) e 103 iu 80 a 64 o 63 0 1 0 1 0 1 a o e iu Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 29 / 32

running the algorithm f 5 ( ) e 103 a 64 o 63 i 57 u 23 0 1 0 1 0 1 a o e 0 1 i u Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 30 / 32

Notes on Huffman Trees A Huffman tree is a full binary tree: every node has two nonempty children. Optimal Huffman trees are not unique. Elements of an algorithm for Huffman Trees: Make frequency table of symbols in a text. Nodes in priority queue are of type struct containing string for the symbol and int for count. Recursive tree creation algorithm. Going forward: contract the frequency table. Returns from the recursive calls refines the tree. Symbols occurring with least frequency will appear separately only in the last stage of the algorithm. Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 31 / 32

Summary + Exercises Ended chapter 8 on trees. Exercises: 1 Write an iterative version of the smallest() method to return the smallest element in a binary search tree. 2 Give code to use a STL priority queue to create a frequency table for lower case letters in a string. 3 Define a binary search tree for strings and use it to store words that appear in a text on file. A word is separated by one or more spaces. 4 Define the algorithm to decode a message (given as bit string) using a Huffman tree. Introduction to Data Structures (MCS 360) Priority Queues and Huffman Trees L-26 27 October 2017 32 / 32