Introduction to Trees Applications of Trees - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Introduction to Trees Applications of Trees

Description:

Note that inserting into a plain sequence ai would instead take T(n) worst-case time. ... Huffman coding for the symbol ai is the concatenation of the labels ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 36
Provided by: isabellebi
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Trees Applications of Trees


1
Introduction to TreesApplications of Trees
2
Learning Objectives
  • Understand what is a tree.
  • Understand the terminology related to trees.
  • Understand tree height theorem.
  • Understand some applications of trees.
  • Understand tree modeling for coding symbols and
    for games.
  • Understand algorithms for tree traversal.

3
Introduction to Trees
  • A tree is a connected undirected graph that
    contains no circuits.
  • Theorem There is a unique simple path between
    any two of its nodes.
  • A (not-necessarily-connected) undirected graph
    without simple circuits is called a forest.
  • You can think of it as a set of trees having
    disjoint sets of nodes.
  • A leaf node in a tree or forest is any pendant or
    isolated vertex. An internal node is any
    non-leaf vertex (thus it has degree ___ ).

4
Tree and Forest Examples
Leaves in green, internal nodes in brown.
  • A Tree
  • A Forest

5
Rooted Trees
  • A rooted tree is a tree in which one node has
    been designated the root.
  • Every edge is (implicitly or explicitly) directed
    away from the root.
  • Concepts related to rooted trees
  • Parent, child, siblings, ancestors, descendents,
    leaf, internal node, subtree.

6
Rooted Tree Examples
  • Note that a given unrooted tree with n nodes
    yields n different rooted trees.

Same tree exceptfor choiceof root
root
root
7
Rooted-Tree Terminology Exercise
  • Find the parent,children, siblings,ancestors,
    descendants of node f.

o
n
h
r
d
m
b
root
a
c
g
e
q
i
f
l
j
k
p
8
n-ary Trees
  • A rooted tree is called n-ary if every vertex has
    no more than n children.
  • It is called full if every internal (non-leaf)
    vertex has exactly n children.
  • A 2-ary tree is called a binary tree.
  • These are handy for describing sequences of
    yes-no decisions.
  • Example Comparisons in binary search algorithm.

9
Which Tree is Binary?
  • Theorem A given rooted tree is a binary tree iff
    every node other than the root has degree ___,
    and the root has degree ___.

10
Ordered Rooted Tree
  • This is just a rooted tree in which the children
    of each internal node are ordered.
  • In ordered binary trees, we can define
  • left child, right child
  • left subtree, right subtree
  • For n-ary trees with ngt2, can use terms like
    leftmost, rightmost, etc.

11
Trees as Models
  • Can use trees to model the following
  • Saturated hydrocarbons
  • Organizational structures
  • Computer file systems
  • In each case, would you use a rooted or a
    non-rooted tree?

12
Some Tree Theorems
  • Any tree with n nodes has e n-1 edges.
  • Proof Consider removing leaves.
  • A full m-ary tree with i internal nodes has
    nmi1 nodes, and ?(m-1)i1 leaves.
  • Proof There are mi children of internal nodes,
    plus the root. And, ? n-i (m-1)i1. ?
  • Thus, when m is known and the tree is full, we
    can compute all four of the values e, i, n, and
    ?, given any one of them.

13
Some More Tree Theorems
  • Definition The level of a node is the length of
    the simple path from the root to the node.
  • The height of a tree is maximum node level.
  • A rooted m-ary tree with height h is called
    balanced if all leaves are at levels h or h-1.
  • Theorem There are at most mh leaves in an m-ary
    tree of height h.
  • Corollary An m-ary tree with ? leaves has
    height h?logm?? . If m is full and balanced
    then h?logm??.

14
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

15
Binary 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.

16
Binary 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
17
Recursive 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)

18
Decision 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.

19
Coin-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?

?
20
As 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.
21
Applying 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

22
General 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.

23
General 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.
24
Coin 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
25
Character Coding Tree
  • Trees are also used to code characters because
    coding of characters with fixed length codes
    takes a lot of space.Example to code alphabet
    (26 letters), it takes 5 bits per letter, which
    multiplies the length of the text by 5.
  • Trees permit to derive variable length codes
    called prefix codes.

26
Prefix Codes
  • A prefix code is a code such that the bit string
    for a letter never occurs as the first part of
    the bit string of another letter.
  • Example decode 11111011100

11111011100 sane
27
Huffman Coding Tree
  • Huffman coding tree takes as input the
    frequencies of symbols in a string such that it
    builds a prefix code that encodes the string with
    the fewest possible bits.
  • Developed by David Huffman 1951, MIT.
  • Used in particular in data compression.

28
Huffman Coding Tree
  • Algorithmprocedure Huffman(C symbols ai with
    frequencies wi, i1, , n)
  • F forest of n rooted trees with root ai /
    wi
  • while F is not a treebegin Replace the
    rooted trees T and T of least weights from F
    with w(T) w(T) with a tree having a new
    root that has T as its left subtree and T
    as its right subtree. Label the new edge to T
    with 0 and the new edge of T with 1.
    Assign w(T) w(T) as the weight of the new
    tree.end
  • the Huffman coding for the symbol ai is the
    concatenation of the labels of the edges in the
    unique path from the root to the vertex ai

29
Huffman Coding Tree
30
Game Trees
  • Games like checkers, tic-tac-toe, nim, and chess
    can be modeled with trees.
  • Leaves represent the final positions of the game
    and are assigned the playoff of the first player.
  • Win/lose games are assigned 1/0/-1 for
    win/draw/lose at the leaves..

31
Game Trees
32
Game Trees
  • A strategy is a set of rules that tells the
    player how to select moves to win the game.
  • MINMAX strategyThe value of a vertex in a game
    tree is defined recursively as
  • The value of a leaf is a payoff to the first
    player when the game terminates in the position
    represented by this leaf.
  • The value of an internal vertex of an even level
    is the maximum of the values of its children, and
    the value of an internal vertex at an odd level
    is the minimum of the values of its children.

33
Game Trees
  • The MINMAX strategy is the optimal strategy for
    both players.
  • Theoremthe value of a vertex of a game tree
    tells us the payoff to the first player if both
    players follow the MINMAX strategy and play
    starts from the position represented by this
    vertex.

34
Game Trees
35
Game Trees
Write a Comment
User Comments (0)
About PowerShow.com