Title: ICS 241
1ICS 241
- Discrete Mathematics II
- William Albritton, Information and Computer
Sciences Department at University of Hawaii at
Manoa - For use with Kenneth H. Rosens Discrete
Mathematics Its Applications (5th Edition) - Based on slides originally created by
- Dr. Michael P. Frank, Department of Computer
Information Science Engineering at University
of Florida
2Section 9.2 Applications of Trees
- Binary search trees
- A simple data structure for sorted lists
- Decision trees
- Minimum comparisons in sorting algorithms
- Prefix codes
- Huffman coding
- Game trees
3Binary Search Trees
- A representation for sorted sets of items.
- Supports the following operations in T(log n)
average-case time - Searching for an existing item.
- Inserting a new item, if not already present.
- Supports printing out all items in T(n) time.
- Note that inserting into a plain sequence ai
would instead take T(n) worst-case time.
4Binary Search Tree Format
- Items are stored at individual tree nodes.
- We arrange for the tree to always obey this
invariant - For every item x,
- Every node in xs left subtree is less than x.
- Every node in xs right subtree is greater than x.
Example
7
3
12
1
5
9
15
0
2
8
11
5Recursive Binary Tree Insert
- procedure insert(T binary tree, x item)v
rootTif v null then begin rootT x
return Done endelse if v x return Already
presentelse if x lt v then return
insert(leftSubtreeT, x)else must be x gt
v return insert(rightSubtreeT, x)
6Class Exercise
- Exercise 1., 3. (p. 656)
- Each pair of students should use only one sheet
of paper while solving the class exercises
7Decision Trees
- A decision tree represents a decision-making
process. - Each possible decision point or situation is
represented by a node. - Each possible choice that could be made at that
decision point is represented by an edge to a
child node. - In the extended decision trees used in decision
analysis, we also include nodes that represent
random events and their outcomes.
8Coin-Weighing Problem
- Imagine you have 8 coins, oneof which is a
lighter counterfeit, and a free-beam balance. - No scale of weight markings is required for this
problem! - How many weighings are needed to guarantee that
the counterfeit coin will be found?
?
9As a Decision-Tree Problem
- In each situation, we pick two disjoint and
equal-size subsets of coins to put on the scale.
A given sequence ofweighings thus yieldsa
decision tree withbranching factor 3.
The balance thendecides whether to tip left,
tip right, or stay balanced.
10Applying the Tree Height Theorem
- The decision tree must have at least 8 leaf
nodes, since there are 8 possible outcomes. - In terms of which coin is the counterfeit one.
- Recall the tree-height theorem, h?logm??.
- Thus the decision tree must have heighth
?log38? ?1.893? 2. - Lets see if we solve the problem with only 2
weighings
11General Solution Strategy
- The problem is an example of searching for 1
unique particular item, from among a list of n
otherwise identical items. - Somewhat analogous to the adage of searching for
a needle in haystack. - Armed with our balance, we can attack the problem
using a divide-and-conquer strategy, like whats
done in binary search. - We want to narrow down the set of possible
locations where the desired item (coin) could be
found down from n to just 1, in a logarithmic
fashion. - Each weighing has 3 possible outcomes.
- Thus, we should use it to partition the search
space into 3 pieces that are as close to
equal-sized as possible. - This strategy will lead to the minimum possible
worst-case number of weighings required.
12General Balance Strategy
- On each step, put ?n/3? of the n coins to be
searched on each side of the scale. - If the scale tips to the left, then
- The lightweight fake is in the right set of ?n/3?
n/3 coins. - If the scale tips to the right, then
- The lightweight fake is in the left set of ?n/3?
n/3 coins. - If the scale stays balanced, then
- The fake is in the remaining set of n - 2?n/3?
n/3 coins that were not weighed! - Except if n mod 3 1 then we can do a little
better by weighing ?n/3? of the coins on each
side.
You can prove that this strategy always leads to
a balanced 3-ary tree.
13Coin Balancing Decision Tree
- Heres what the tree looks like in our case
123 vs 456
left 123
balanced78
right 456
4 vs. 5
1 vs. 2
7 vs. 8
L1
L4
L7
R2
B3
R5
B6
R8
14Prefix Codes
- A way to encode letters (or any data) using bit
strings, so that the bit string for a letter
never occurs as the first part of a bit string
for another letter - Can be represented as a binary tree, where the
characters are the leaves, and the bits
correspond to the left and right child - Example e (0), a(10), t(11), so that 101110
ate - If use 8-bit ASCII code, then 3824 bits
15Class Exercise
- Exercise 21. (p. 657)
- Each pair of students should use only one sheet
of paper while solving the class exercises
16Huffman Coding
- A way to compress data, using the frequency of
symbols in a string to produce a prefix code that
encodes the string using the fewest possible bits - Tree is built from the bottom-up, connecting the
symbols and nodes with the lowest frequencies
first - Parent frequency of left child freq. of right
child - Left child frequency gt right child frequency
17Huffman Coding Example
- Letters respective frequencies
- A0.08, B0.10, C0.12, D0.15, E0.20, F0.35
- Huffman coding gives following encodings
- A(111), B(110), C(011), D(010),E(10), F(00)
- Average number of bits used to encode a letter
- 30.08 3 0.10 30.12 30.15 20.20
20.35 2.45
18Class Exercise
- Exercise 23. (p. 657)
- Each pair of students should use only one sheet
of paper while solving the class exercises
19Game Trees
- Game of Nim
- Two players remove 1 or more stones from one of
the piles of stones - Lose by removing the last stone
- Represent game with a tree that shows all
possible moves in the game - Root is start
- Even level nodes are boxes (1st players move)
- Odd level nodes are circles(2nd players move)
20Game of Nim
- Determine who will win by minmax strategy
- Leaves are given numbers that correspond to who
wins the game - 1 if 1st player wins
- -1 if 2nd player wins
- For each node, take the maximum (for 1st player)
or minimum value of children (for 2nd player) - 1st player tries to maximize payoffs 2nd player
tries to minimize payoffs
21Game of Nim Example
- Start with 3 piles of stones 2, 2, 1 stones
- Start with square node (1st players move)
- 3 children are circular nodes (2nd players move)
- Leaves are 1/-1 for 1st/2nd player wins
- Take min or max values of children to determine
who will win the game
22Class Exercise
- Exercise 33. (p. 659)
- Each pair of students should use only one sheet
of paper while solving the class exercises