Recitation Nov. 15. HW5: Huffman Encoding/Decoding Task: –Read a text file (i.e. “message.text”) and figure out character frequencies. Don’t forget ‘\n’

Slides:



Advertisements
Similar presentations
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Tyler Robison Summer
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
CS 315 Lecture 15 March 31 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on MinHeap: Insert Delete Converting an array.
CS 315 Lecture 18 Nov 15 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Chapter 9: Huffman Codes
Sorting and Heaps Eugene Weinstein New York University Computer Science Department Data Structures Fall 2008.
Lecture 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Data Compression and Huffman Trees (HW 4) Data Structures Fall 2008 Modified by Eugene Weinstein.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
§3 Binary Heap 1. Structure Property: 【 Definition 】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees.
Lecture Objectives  To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
data ordered along paths from root to leaf
Homework #5 New York University Computer Science Department Data Structures Fall 2008 Eugene Weinstein.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues (Heaps)
Binary Heaps Text Read Weiss, § Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Dan Grossman Spring 2012.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Heaps A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
CSC 2300 Data Structures & Algorithms March 13, 2007 Chapter 6. Priority Queues.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Binary Heaps Fundamental Data Structures and Algorithms Peter Lee February 4, 2003.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
CS 201 Data Structures and Algorithms
Binary Heaps What is a Binary Heap?
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Binary Heaps What is a Binary Heap?
Heaps, Heap Sort, and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
Chapter 9: Huffman Codes
Binary Heaps Text Binary Heap Building a Binary Heap
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
Binary Heaps Priority Queues
BuildHeap The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. The following algorithm will build a.
Heaps and the Heapsort Heaps and priority queues
CE 221 Data Structures and Algorithms
Heaps A heap is a binary tree that satisfies the following properties:
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Heaps Priority Queues.
Heap code in C++ template <class eType>
Priority Queues (Heaps)
Priority Queues Binary Heaps
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Presentation transcript:

Recitation Nov. 15

HW5: Huffman Encoding/Decoding Task: –Read a text file (i.e. “message.text”) and figure out character frequencies. Don’t forget ‘\n’ (newline) and other possible characters. –Build a Huffman Tree (use HW 4) for the found character frequencies. –Encode the file using Huffman Encoding –Decode the Encoded message. –Make Sure Decoding matches original! –Compare length of Huffman encoding to bit-size of original.

HW5 How to count the frequency of each character? What data-structure do we use for encoding? What data-structure do we use for decoding? What is the size of the original file? What is the size of the Huffman encoding of the file?

buildHeap Task: –Start with N items (unordered) and build a heap containing these items. Simple Solution –Start with an empty heap and insert the items one at a time. –Running time O(N log(N)) But, there’s a better way….

buildHeap Create a complete binary tree of the items, but do not care about order. –Using array implementation, just put the items into the array. Run time? Now need to recreate the order property: –The child of any node is greater than the parent.

buildHeap Start from bottom up –Don’t care about lowest level since the leafs are already good heaps. Use percolateDown() method –percolateDown() property: If T has heap property in all nodes but the root then, after calling percolateDown(), T is a heap. Theorem 1: buildHeap works Proof: Induction on tree size.

buildHeap Analyzing the running time: –Perfect binary tree of height h contains 2 h+1 -1 nodes. Theorem: The sum of the heights of the nodes is < 2 h+1 Proof: S = h + 2(h-1) + 4(h-2) +…+2 h-2 (2)+ 2 h-1 (1) = 2 h (1/2 + 2/4 + 3/8 + 4/16 + 5/32 + …+h/2 h ) < 2 h+1

D-airy (Dairy) Heap Take 10 mins

Solution public int parent(int i) { return (i-1)/d; } public int kthChild(int i, int k) { return d*i + k; }

Solution private int smallestChild( int hole ) { int bestChildYet = kthChild(hole, 1); int k = 2; int candidateChild = kthChild(hole, k); while ((k <= d) && (candidateChild < currentSize)) { if (array[candidateChild].compareTo(array[bestChildYet]) < 0) bestChildYet = candidateChild; k++; candidateChild = kthChild(hole, k); } return bestChildYet; }

Solution private void percolateUp ( int hole ) { Comparable tmp = array[hole]; for (; hole > 0 && tmp.compareTo(array[parent(hole)] ) < 0; hole = parent(hole)) array[ hole ] = array[ parent(hole) ]; array[ hole ] = tmp; }

Solution private void percolateDown( int hole ) { int child; Comparable tmp = array[ hole ]; for( ; kthChild(hole, 1) < currentSize; hole = child ) { child = smallestChild(hole); if( array[ child ].compareTo( tmp ) < 0 ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp; }