Algorithms and Data Structures CS-CO-412

Similar documents
Algorithms and Data Structures CS-CO-412

Data Structures and Algorithms for Engineers

Binary Trees and Huffman Encoding Binary Search Trees

CS 206 Introduction to Computer Science II

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

Binary Trees: Practice Problems

Binary Search Trees Part Two

15 July, Huffman Trees. Heaps

Huffman Coding. Version of October 13, Version of October 13, 2014 Huffman Coding 1 / 27

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.

EE 368. Weeks 5 (Notes)

Topic Binary Trees (Non-Linear Data Structures)

Greedy Algorithms. Alexandra Stefan

Text Compression through Huffman Coding. Terminology

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

Horn Formulae. CS124 Course Notes 8 Spring 2018

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

Greedy Algorithms CHAPTER 16

IX. Binary Trees (Chapter 10)

TREES. Trees - Introduction

Lecture: Analysis of Algorithms (CS )

Data compression.

INF2220: algorithms and data structures Series 1

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

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

Solutions to Midterm 2 - Monday, July 11th, 2009

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Chapter 20: Binary Trees

Review of the Lectures 21-26, 30-32

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

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

Advanced Set Representation Methods

Data Structures and Algorithms

Trees. (Trees) Data Structures and Programming Spring / 28

CS 216 Exam 1 Fall SOLUTION

Binary Trees, Binary Search Trees

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

Efficient Sequential Algorithms, Comp309. Motivation. Longest Common Subsequence. Part 3. String Algorithms

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

6. Finding Efficient Compressions; Huffman and Hu-Tucker

CSE 143 Lecture 22. Huffman Tree

Balanced Search Trees. CS 3110 Fall 2010

Algorithms Dr. Haim Levkowitz

Red-Black, Splay and Huffman Trees

Analysis of Algorithms - Greedy algorithms -

Programming Languages and Techniques (CIS120)

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

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

ASCII American Standard Code for Information Interchange. Text file is a sequence of binary digits which represent the codes for each character.

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Greedy algorithms 2 4/5/12. Knapsack problems: Greedy or not? Compression algorithms. Data compression. David Kauchak cs302 Spring 2012

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

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

Greedy Algorithms and Huffman Coding

TREES Lecture 10 CS2110 Spring2014

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

More Bits and Bytes Huffman Coding

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

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

Binary Trees. Examples:

ITI Introduction to Computing II

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

Tree Data Structures CSC 221

Programming Assignment #4 Binary Trees in C++

Chapter 16: Greedy Algorithm

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

MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, Huffman Codes

Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

ASCII American Standard Code for Information Interchange. Text file is a sequence of binary digits which represent the codes for each character.

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Priority Queues and Huffman Trees

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Algorithms and Data Structures CS-CO-412

Design and Analysis of Algorithms

CS Transform-and-Conquer

ITI Introduction to Computing II

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Data Compression Algorithms

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

14.4 Description of Huffman Coding

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

Greedy Approach: Intro

16 Greedy Algorithms

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

12 Abstract Data Types

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

: Advanced Programming Final Exam Summer 2008 June 27, 2008

Trees. Eric McCreath

Trees, Binary Trees, and Binary Search Trees

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

CS473-Algorithms I. Lecture 11. Greedy Algorithms. Cevdet Aykanat - Bilkent University Computer Engineering Department

Huffman Codes (data compression)

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Transcription:

Algorithms and Data Structures CS-CO-412 David Vernon Professor of Informatics University of Skövde Sweden david@vernon.eu www.vernon.eu Algorithms and Data Structures 1 Copyright D. Vernon 2014

Trees Lecture 9 Algorithms and Data Structures 2 Copyright D. Vernon 2014

Topic Overview Types of trees Binary Tree ADT Binary Search Tree Optimal Code Trees Huffman s Algorithm Height Balanced Trees AVL Trees Red-Black Trees Algorithms and Data Structures 3 Copyright D. Vernon 2014

Applications of Trees First application: coding and data compression We will define optimal variable-length binary codes and code trees We will study Huffman s algorithm which constructs them Huffman s algorithm is an example of a Greedy Algorithms, an important class of simple optimization algorithms Algorithms and Data Structures 4 Copyright D. Vernon 2014

Text, Codes, and Compression Computer systems represent data as bit strings Encoding: transformation of data into bit strings Decoding: transformation of bit strings into data The code defines the transformation Algorithms and Data Structures 5 Copyright D. Vernon 2014

Text, Codes, and Compression For example: ASCII, the international coding standard, uses a 7-bit code HEX Code Character 20 - <space> 41 A 42 B 61 - a Algorithms and Data Structures 6 Copyright D. Vernon 2014

Text, Codes, and Compression Such encodings are called fixed-length or block codes They are attractive because the encoding and decoding is extremely simple For coding, we can use a block of integers or codewords indexed by characters For decoding, we can use a block of characters indexed by codewords Algorithms and Data Structures 7 Copyright D. Vernon 2014

Text, Codes, and Compression For example: the sentence The cat sat on the mat is encoded in ASCII as 1010100 110100 011001 0101... Note that the spaces are there simply to improve readability... they don t appear in the encoded version. Algorithms and Data Structures 8 Copyright D. Vernon 2014

Text, Codes, and Compression The following bit string is an ASCII encoded message: 10001001100101110001111011111100100110100111011 1011001110100000110100111100110100000110010111 0000111100111111001 Algorithms and Data Structures 9 Copyright D. Vernon 2014

Text, Codes, and Compression And we can decode it by chopping it into smaller strings each of 7 bits in length and by replacing the bit strings with their corresponding characters: 1000100(D)1100101(e)1100011(c)1101111(o)1100100(d) 1101001(i)1101110(n)1100111(g)0100000()1101001(i)111 0011(s)0100000()1100101(e)1100001(a)1110011(s)1111 001(y) Algorithms and Data Structures 10 Copyright D. Vernon 2014

Text, Codes, and Compression Every code can be thought of in terns of a finite alphabet of source symbols a finite alphabet of code symbols Each code maps every finite sequence or string of source symbols into a string of code symbols Algorithms and Data Structures 11 Copyright D. Vernon 2014

Text, Codes, and Compression Let A be the source alphabet Let B be the code alphabet A code f is an injective map f: S A S B where S A is the set of all strings of symbols from A where S B is the set of all strings of symbols from B Algorithms and Data Structures 12 Copyright D. Vernon 2014

Text, Codes, and Compression Injectivity ensures that each encoded string can be decoded uniquely (we do not want two source strings that are encoded as the same string) A B Injec,ve Mapping: each element in the range is related to at most one element in the domain Algorithms and Data Structures 13 Copyright D. Vernon 2014

Text, Codes, and Compression We are primarily interested in the code alphabet {0, 1} since we want to code source symbols strings as bit strings Algorithms and Data Structures 14 Copyright D. Vernon 2014

Text, Codes, and Compression There is a problem with block codes: n symbols produce nb bits with a block code of length b For example, if n = 100,000 (the number of characters in a typical 200-page book) b = 7 (e.g. 7-bit ASCII code) then the characters are encoded as 700,000 bits Algorithms and Data Structures 15 Copyright D. Vernon 2014

Text, Codes, and Compression While we cannot encode the ASCII characters with fewer than 7 bits We can encode the characters with a different number of bits, depending on their frequency of occurrence Use fewer bits for the more frequent characters Use more bits for the less frequent characters Such a code is called a variable-length code Algorithms and Data Structures 16 Copyright D. Vernon 2014

Text, Codes, and Compression First problem with variable length codes: when scanning an encoded text from left to right (decoding it) How do we know when one codeword finishes and another starts? We require each codeword not be a prefix of any other codeword So, for the binary code alphabet, we should base the codes on binary code trees Algorithms and Data Structures 17 Copyright D. Vernon 2014

Text, Codes, and Compression Binary code trees: binary tree whose external nodes are labelled uniquely with the source alphabet symbols Left branches are labelled 0 Right branches are labelled 1 Algorithms and Data Structures 18 Copyright D. Vernon 2014

Text, Codes, and Compression A binary code tree and its prefix code a 0 1 0 1 a 0 b 11 c 10 c b Algorithms and Data Structures 19 Copyright D. Vernon 2014

Text, Codes, and Compression The codeword corresponding to a symbol is the bit string given by the path from the root to the external node labeled with the symbol Note that, as required, no codeword is a prefix for any other codeword This follows directly from the fact that source symbols are only on external nodes and there is only one (unique) path to that symbol Algorithms and Data Structures 20 Copyright D. Vernon 2014

Text, Codes, and Compression Codes that satisfy the prefix property are called prefix codes Prefix codes are important because we can uniquely decode an encoded text with a left-to-right scan of the encoded text by considering only the current bit in the encoded text decoder uses the code tree for this purpose Algorithms and Data Structures 21 Copyright D. Vernon 2014

Text, Codes, and Compression Read the encoded message bit by bit Start at the root if the bit is a 0, move left if the bit is a 1, move right if the node is external, output the corresponding symbol and begin again at the root Algorithms and Data Structures 22 Copyright D. Vernon 2014

Text, Codes, and Compression Encoded message: 0 0 1 1 1 0 0 Decoded message: 0 1 A A B C A a 0 1 c b Algorithms and Data Structures 23 Copyright D. Vernon 2014

Optimal Variable-Length Codes What makes a good variable length code? Let A = a 1,..., a n, n>=1, be the alphabet of source symbols Let P = p 1,..., p n, n>=1, be their probability of occurrence We obtain these probabilities by analysing are representative sample of the type of text we wish to encode Algorithms and Data Structures 24 Copyright D. Vernon 2014

Optimal Variable-Length Codes Any binary tree with n external nodes labelled with the n symbols defines a prefix code Any prefix code for the n symbols defines a binary tree with at least n external nodes Such a binary tree with exactly n external nodes is a reduced prefix code (tree) Good prefix codes are always reduced (and we can always transform an non-reduced prefix code into a reduced one) Algorithms and Data Structures 25 Copyright D. Vernon 2014

Non-Reduced Prefix Code (Tree) a 000 b 111 c 110 0 1 0 1 0 1 a 0 1 c b Algorithms and Data Structures 26 Copyright D. Vernon 2014

Optimal Variable-Length Codes Comparison of prefix codes - compare the number of bits in the encoded text Let A = a 1,..., a n, n>=1, be the alphabet of source symbols Let P = p 1,..., p n be their probability of occurrence Let W = w 1,..., w n be a prefix code for A = a 1,..., a n Let L = l 1,..., l n be the lengths of W = w 1,..., w n Algorithms and Data Structures 27 Copyright D. Vernon 2014

Optimal Variable-Length Codes Given a source text T with f 1,..., f n occurrences of a 1,..., a n respectively The total number of bits when T is encoded is Σ n i = 1 f i l i The total number of source symbols is Σ n i = 1 f i The average length of the W-encoding is Alength(T, W) = Σ n i = 1 f i l i / Σ n i = 1 f i Algorithms and Data Structures 28 Copyright D. Vernon 2014

Optimal Variable-Length Codes For long enough texts, the probability p i of a given symbol occurring is approximately p i = f i / Σ n i = 1 f i So the expected length of the W-encoding is Elength(W, P) = Σ n i = 1 p i l i Algorithms and Data Structures 29 Copyright D. Vernon 2014

Optimal Variable-Length Codes To compare two different codes W 1 and W 2 we can compare either Alength(T, W 1 ) and Alength(T, W 2 ) or Elength(W 1, P) and Elength(W 2, P) We say W 1 is no worse than W 2 if Elength(W 1, P) <= Elength(W 2, P) We say W 1 is optimal if Elength(W 1, P) <= Elength(W 2, P) for all possible prefix codes W 2 of A Algorithms and Data Structures 30 Copyright D. Vernon 2014

Optimal Variable-Length Codes Huffman s Algorithm We wish to solve the following problem: Given n symbols A = a 1,..., a n, n>=1 and the probability of their occurrence P = p 1,..., p n, respectively, construct an optimal prefix code for A and P Algorithms and Data Structures 31 Copyright D. Vernon 2014

Optimal Variable-Length Codes This problem is an example of a global optimization problem Brute force (or exhaustive search) techniques are too expensive to compute: Given A and P Compute the set of all reduced prefix codes Choose the minimal expected length prefix code Algorithms and Data Structures 32 Copyright D. Vernon 2014

Optimal Variable-Length Codes This algorithm takes O(n n ) time, where n is the size of the alphabet Why? because any binary tree of size n-1 (i.e. with n external nodes) is a valid reduced prefix tree and there are n! ways of labelling the external nodes Since n! is approximately n n we see that there are approximately O(n n ) steps to go through when constructing all the trees to check Algorithms and Data Structures 33 Copyright D. Vernon 2014

Optimal Variable-Length Codes Huffman s Algorithm is only O(n 2 ) This is significant: if n = 128 (number of symbols in a 7-bit ASCII code) O(n n ) = 128 128 = 5.28 x 10 269 O(n 2 ) = 128 2 = 1.6384 x 10 4 There are 31536000 seconds in a year and if we could compute 1000 000 000 steps a second then the brute force technique would still take 1.67 x 10 253 years Algorithms and Data Structures 34 Copyright D. Vernon 2014

Optimal Variable-Length Codes The age of the universe is estimated to be 13 billion years, i.e., 1.3x1010 years A long way off 1.67 x 10 253 years! Algorithms and Data Structures 35 Copyright D. Vernon 2014

Optimal Variable-Length Codes Huffman s Algorithm uses a technique called Greedy It uses local optimization to achieve a globally optimum solution Build the code incrementally Reduce the code by one symbol at each step Merge the two symbols that have the smallest probabilities into one new symbol Algorithms and Data Structures 36 Copyright D. Vernon 2014

Optimal Variable-Length Codes Before we begin, note that we d like a tree with the symbols which have the lowest probability to be on the longest path Why? Because the length of the codeword is equal to the path length and we want short codewords for high-probability symbols longer codewords for low-probability symbols Algorithms and Data Structures 37 Copyright D. Vernon 2014

Text, Codes, and Compression A binary code tree and its prefix code a 0 1 0 1 a 0 b 11 c 10 c b Algorithms and Data Structures 38 Copyright D. Vernon 2014

Huffman s Algorithm We will treat Huffman s Algorithm for just six letters, i.e, n = 6, and there are six symbols in the source alphabet These are, with their probabilities, E - 0.1250 T - 0.0925 A - 0.0805 O - 0.0760 I - 0.0729 N - 0.0710 Algorithms and Data Structures 39 Copyright D. Vernon 2014

Huffman s Algorithm Step 1: Create a forest of code trees, one for each symbol Each tree comprises a single external node (empty tree) labelled with its symbol and weight (probability) 0.1250 0.0925 0.0805 0.0760 0.0729 0.0710 E T A O I N Algorithms and Data Structures 40 Copyright D. Vernon 2014

Huffman s Algorithm Step 2: Choose the two binary trees, B1 and B2, that have the smallest weights Create a new root node with B1 and B2 as its children and with weight equal to the sum of these two weights 0.1439 0.1250 0.0925 0.0805 0.0760 E T A O I N Algorithms and Data Structures 41 Copyright D. Vernon 2014

Huffman s Algorithm Step 3: Repeat step 2! Algorithms and Data Structures 42 Copyright D. Vernon 2014

Huffman s Algorithm 0.1565 0.1439 0.1250 E 0.0925 T A O I N Algorithms and Data Structures 43 Copyright D. Vernon 2014

Huffman s Algorithm 0.2175 0.1565 0.1439 E T A O I N Algorithms and Data Structures 44 Copyright D. Vernon 2014

Huffman s Algorithm 0.3004 0.2175 E T A O I N Algorithms and Data Structures 45 Copyright D. Vernon 2014

Huffman s Algorithm 0.5179 E T A O I N Algorithms and Data Structures 46 Copyright D. Vernon 2014

Huffman s Algorithm The final prefix code is: A 100 E 00 I 110 N 111 O 101 T 01 Algorithms and Data Structures 47 Copyright D. Vernon 2014

Huffman s Algorithm Three phases in the algorithm 1. Initialize the forest of code trees 2. Construct an optimal code tree 3. Compute the encoding map Algorithms and Data Structures 48 Copyright D. Vernon 2014

Huffman s Algorithm Phase 1: Initialize the forest of code trees How will we represent the forest of trees? Better question: how will we represent our tree... have to store both alphanumeric characters and probabilities? Need some kind of composite node Opt to represent this composite node as an INTERNAL node Algorithms and Data Structures 49 Copyright D. Vernon 2014

Huffman s Algorithm Consequently, the initial tree is simply one internal node That is, it is a root (with two external nodes) Char 0.nnn Algorithms and Data Structures 50 Copyright D. Vernon 2014

Huffman s Algorithm So, to create such a tree we simply invoke the following operations: Initialize the tree tree() Add a node addnode(char, weight, T) Algorithms and Data Structures 51 Copyright D. Vernon 2014

Huffman s Algorithm We must also keep track of our forest Could represent it as a linked list of pointers to Binary trees... Algorithms and Data Structures 52 Copyright D. Vernon 2014

Huffman s Algorithm Represented as: Algorithms and Data Structures 53 Copyright D. Vernon 2014

Huffman s Algorithm Is there an alternative? Question: why do we use dynamic data structures? Answer: When we don t know in advance how many elements are in our data set When the number of elements varies significantly Is this the case here? No! Algorithms and Data Structures 54 Copyright D. Vernon 2014

Huffman s Algorithm So, our alternatives are?... An array, indexed by number, of type... binary_tree, i.e., each element in the array can point to a binary code tree Algorithms and Data Structures 55 Copyright D. Vernon 2014

Huffman s Algorithm Algorithms and Data Structures 56 Copyright D. Vernon 2014

Huffman s Algorithm What will be the dimension of this array? n, the number of symbols in our source alphabet since this is the number of trees we start out with in our forest initially Algorithms and Data Structures 57 Copyright D. Vernon 2014

Huffman s Algorithm Phase 2: construct the optimal code tree Algorithms and Data Structures 58 Copyright D. Vernon 2014

Huffman s Algorithm Pseudo-code algorithm Find the tree with the smallest weight - A, at element i Find the tree with the next smallest weight - B, at element j Construct a tree, with right sub-tree A, left sub-tree B, with root having weight = sum of the roots of A and B Let array element i point to the new tree Delete tree at element j Algorithms and Data Structures 59 Copyright D. Vernon 2014

Huffman s Algorithm let n be the number of trees ini,ally Repeat Find the tree with the smallest weight - A, at element i Find the tree with the next smallest weight - B, at element j Construct a tree, with right sub-tree A, left subtree B, with root having weight = sum of the roots of A and B Un,l only one tree lep in the array Let array element i point to the new tree Delete tree at element j Algorithms and Data Structures 60 Copyright D. Vernon 2014

Huffman s Algorithm Phase 3: Compute the encoding map We need to write out a list of source symbols together with their prefix code We need to write out the contents of each external node (or each frontier internal node) together with the path to that node We need to traverse the binary code tree in some manner Algorithms and Data Structures 61 Copyright D. Vernon 2014

But. we want to print out the symbol and the prefix code: i.e. the symbol at the leafnode and the path by which we got to that node Algorithms and Data Structures 62 Copyright D. Vernon 2014

How will we represent the path? As an array of binary values (representing the left and right links on the path) Algorithms and Data Structures 63 Copyright D. Vernon 2014

Huffman s Algorithm // new tree definition struct node { char symbol; float probability; node *pleft, *pright; }; Algorithms and Data Structures 64 Copyright D. Vernon 2014

Huffman s Algorithm class tree { public: tree(); ~tree(); void add(char s, float p) {addnode(s,p,root);} void print() {pr(root,0);} node* &search(int n); int delnode(int x); private node *root; void deltree(node* &p); // NB void addnode(char s, float p, node* &p); void pr(const node *p, int nspace) const; }; Algorithms and Data Structures 65 Copyright D. Vernon 2014

Huffman s Algorithm void tree::deltree(node* &p) {! // parameter is reference parameter! } if (p!= NULL) {! deltree(p->pleft);! }! deltree(p->pright);! delete p;! p = NULL; // return null pointer! Algorithms and Data Structures 66 Copyright D. Vernon 2014

Huffman s Algorithm class forest { public:! forest(int size);! ~forest();! void initialize_forest();! void add_to_tree(int tree_number,! char symbol, float probability);! void print_forest() const;! void print_tree(int tree_number);! void join_trees(int tree_1, int tree_2);! int empty_tree(int tree_number);! float root_probability(int tree_number);! private:! tree tree_array[maximum_number_of_trees];! int forest_size;! };! Algorithms and Data Structures 67 Copyright D. Vernon 2014

Huffman s Algorithm // inorder traversal // // recursive function to print the contents of the // binary search tree void tree::prorder(const node *p) const { if (p!=null) { prorder(p->pleft); cout << p->data << ; prorder(p->pright); } } Algorithms and Data Structures 68 Copyright D. Vernon 2014

Huffman s Algorithm // inorder traversal to print only leaf nodes void tree::leafnode_traversal(const node *p) const { if (p!= NULL) { if (at_leafnode) { // PSEUDO CODE visit this node } else { leafnode_traversal(p->pleft); leafnode_traversal(p->pright); } } } Algorithms and Data Structures 69 Copyright D. Vernon 2014

Huffman s Algorithm // inorder traversal to print only leaf nodes void tree::leafnode_traversal(const node *p) const { if (p!= NULL) { if ((p->pleft == NULL) && (p->right == NULL)) { // leafnode cout << p->symbol << p->probability <<endl; } else { leafnode_traversal(p->pleft); leafnode_traversal(p->pright); } } } Algorithms and Data Structures 70 Copyright D. Vernon 2014

Huffman s Algorithm // pseudocode version of compute_map // to traverse tree and print leaf node and path // to leaf node void tree::traverse_leaf_nodes(const node *p, path) { if (at leaf node) { print out symbol and path } else { add_to_path(path, 0); // left traverse_leaf_nodes(p->pleft, path); remove_element_from_path(path); Algorithms and Data Structures 71 Copyright D. Vernon 2014

Huffman s Algorithm add_to_path(path, 1); // right traverse_leaf_nodes(p->pright, path); remove_element_from_path(path); Algorithms and Data Structures 72 Copyright D. Vernon 2014

// Definition of path Huffman s Algorithm #define MAX_PATH_LENGTH 20 class path { public: path(); ~path(); add_to_path(int direction); remove_from_path(); print_path(); private: int path_components[max_path_length]; int path_length; } Algorithms and Data Structures 73 Copyright D. Vernon 2014

Huffman s Algorithm // Definition of path path::path() { int i; for (i=0; i<max_path_length; i++) { path_components[i] = 0; } path_length = 0; } Algorithms and Data Structures 74 Copyright D. Vernon 2014

Huffman s Algorithm // Definition of path path::~path() { } Algorithms and Data Structures 75 Copyright D. Vernon 2014

Huffman s Algorithm // Definition of path path::add_to_path(int direction) { } if (path_length < MAX_PATH_LENGTH) { } path_components[path_length] = direction; path_length++; else { cout << Error maximum path length reached ; } Algorithms and Data Structures 76 Copyright D. Vernon 2014

Huffman s Algorithm // Definition of path path::remove_from_path() { if (path_length > 0) { path_length--; } else { cout << Error: no path exists ; } } Algorithms and Data Structures 77 Copyright D. Vernon 2014

Huffman s Algorithm // Definition of path path::print_path() { for (i=0; i<path_length; i++) { cout << path_components[i]; } cout << ; } Algorithms and Data Structures 78 Copyright D. Vernon 2014

Huffman s Algorithm // Definition of traverse_leaf_nodes // to traverse tree and print leaf node and path // to leaf node void tree::traverse_leaf_nodes(const node *p, path &code)! {! if (p!= NULL) {! if ( (p->pleft == NULL) &&!! (p->pright == NULL)) { // leaf node!! cout << p->symbol << " ";!! code.print_path();!! cout << endl;! }! else {! Algorithms and Data Structures 79 Copyright D. Vernon 2014

Huffman s Algorithm code.add_to_path(0); // left! traverse_leaf_nodes(p->pleft, code);!! code.remove_from_path();!! code.add_to_path(1); // right!! traverse_leaf_nodes(p->pright, code);!! code.remove_from_path();! }! }! }! Algorithms and Data Structures 80 Copyright D. Vernon 2014

Huffman s Algorithm void tree::compute_map() {! // new function to print leaf nodes! path code; // and the path to leaf nodes! traverse_leaf_nodes(root, code);! } Algorithms and Data Structures 81 Copyright D. Vernon 2014

Huffman s Algorithm void forest::compute_map() {! int i;! } for (i=0; i<maximum_number_of_trees; i++) {! if (tree_array[i].empty_tree() == FALSE) {!! tree_array[i].compute_map();! }! }! Algorithms and Data Structures 82 Copyright D. Vernon 2014