{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS 310 Unit 16 Optimal Binary Search Trees, and Huffman Codes, and Greedy Programming

# CS 310 Unit 16 Optimal Binary Search Trees, and Huffman Codes, and Greedy Programming

This preview shows pages 1–7. Sign up to view the full content.

CS 310 Unit 16 Optimal Binary Search Trees, Huffman Codes, and Greedy Programming Furman Haddix Ph.D. Assistant Professor Minnesota State University, Mankato Spring 2008

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Unit 16 Objectives Optimal Binary Search Trees Elements of the Greedy Strategy Huffman Codes Variable Length Codes Prefix Codes Constructing a Huffman Code Justification of Optimality of Huffman Codes Dynamic Programming compared to Greedy Methods Knapsack Problems Text Chapters 15.5, 16.3, 16.2
Optimal Binary Search Tree If we have a binary search tree that looks up C++ reserved words, performance would vary depending on whether specific words were located near the root or near the bottom level, e.g., if union were near the root and int were near the bottom level. The binary search tree that we will work on has two kinds of nodes: key nodes holding a valid key dummy nodes representing all of the possible but invalid keys before, after, or in between valid keys. if there are n key nodes, there will be n+1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Optimality Metric for Binary Search Tree The cost of a search may be approximated as the number of nodes traversed (level of termination + 1). A search terminating at the root (level 0) would cost 1 A search terminating at level 3 would cost 4 A search terminating at level 4 would cost 5 Whether the search is successful (terminating at a key node) or unsuccessful (terminating at a dummy node does not affect the cost computation. Based on the above the following relationships exist: Σ i=1 n p i + Σ i=0 n q i = 1, where p i = probability of searching for key k i and q i = probability of searching for an invalid key and encountering dummy d i (in between k i and k i+1 ) E = Expected search cost = Σ i=1 n ((l i + 1)*p i ) + Σ
The Structure of an Optimal Binary Search Tree 1. Characterize the structure of an optimal solution There are n possible roots if n is the number of keys For each possible root, the tree is composed of the root and two optimal binary search subtrees Each optimal binary search subtree has j – i + 1 possible roots, where i is the index of the smallest key and j is the index of the largest key. Similarly, the optimal subtree must be composed of a root and two optimal subtrees. k-level optimal subtrees may be components of multiple optimal subtrees.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Recursive Formulation of Optimal Binary Search Tree 1. Recursively define the value of an optimal solution Define the expected search cost e(i, j) for the subtree which includes keys i through j. If we have a leaf node, then j = i-1 and e(i, j) = q j For a subtree, the sum of the probabilities is w(i, j) = Σ k=i j p k + Σ k=i-1 j q k This is the amount by which a subtree’s search cost increases when it becomes a subtree of a node.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 58

CS 310 Unit 16 Optimal Binary Search Trees, and Huffman Codes, and Greedy Programming

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online