You are on page 1of 72

Chapter 10

Trees
Objectives
 10.1 Introduction to Trees
 10.2 Applications of Trees
 10.3 Tree Traversal
 10.4 Spanning Trees
 10.5 Minimum Spanning Trees
10.1 Introduction to Trees
10.1 Introduction to Trees
10.1 Introduction to Trees

Ex: Tree Tree circuit disconnected


10.1 Introduction to Trees

THEOREM 1:
An undirected graph is a tree if and only if there is a unique simple
path between any two of its vertices.
DEFINITION 2:
A rooted tree is a tree in which one vertex has been designated
as the root and every edge is directed away from the root.
Some terminologies
Parent, Child, Siblings, Ancestors, Descendants, Leaf (Vertices that
have no children), internal vertices (Vertices that have children),
Subtree.
Ex:
the parent of c: b
the children of g: h, i, j
the siblings of h: i, j
all ancestors of e: c, b, a
all descendants of b: c, d, e
all internal vertices: a, b, g, c, h, j
all leaves: d, e, f, i, k, l, m
the subtree rooted at g:
10.1 Introduction to Trees

DEFINITION 3:
A rooted tree is called an m-ary tree if every internal vertex has no more
than m children. The tree is called a full m-ary tree if every internal vertex
has exactly m children. An m-ary tree with m = 2 is called a binary tree.

Full Full Full


3-ary
Binary Ternary 5-ary
10.1 Introduction to Trees

An ordered rooted tree is a rooted tree where the children of each


internal vertex are ordered (from left to right).
Ex1: Ex2:

Ordered rooted tree

Some terminologies on binary tree:


Left child, right child, left subtree, right subtree
Some Tree Models
Some Tree Models
Some Tree Models

How do the processors in Figure 12 can be used to add eight


numbers, using three steps?
Properties of Trees
THEOREM 2:
A tree with n vertices has n − 1 edges.

Using Mathematic Induction.


Let nE be number of edges.
P(n): If the tree T having n vertices then nE=n-1
Basic step:
P(1): n=1, tree has the root node only  nE= 0 = n-1  P(1) true
Induction step:
Suppose that P(k) is true for all k>=1, ie nE=k-1
Add a leaf vertex v to the tree T so that T having k+1 verteices still is a tree.
Let w be the parent of v.
Because T is connected and has no simple circuit  there is only one new edge
between u and v  nE= (k-1)+1 = k .
 P(k+1) true
Proved.
Properties of Trees
THEOREM 3:
A full m-ary tree with i internal vertices contains n  m.i  1 vertices.

Ex:
m=3
i=4
n = 3.4+1 = 13 vertices.
n - 1 = 12 edges

m=5
i=3
n = 3.5+1 = 16 vertices.
n - 1 = 15 edges
Properties of Trees
THEOREM 4: A full m-ary tree with
(i) n vertices has i   n  1 / m internal vertices and l   m  1 n  1 / m leaves,
(ii) i internal vertices has n  m.i  1 vertices and l   m  1 i  1 leaves,
(iii) l leaves has n   ml  1 /  m  1 vertices and i  l  1 /  m  1 internal vertices.

Ex: m=3
n = 13
i = 12/3= 4
L = [(3-1)13+1]/3=9

m=5
n = 16
i = 15/5 =3
L = (4.16+1)/5 = 13
10.1 Introduction to Trees

 Level of a vertex: The length of the path from the root to this vertex.
 Height of Tree: The maximum of levels of vertices = The length of the
longest path from the root to any vertex.

Ex: Level
0

4 (height)
10.1 Introduction to Trees

A m-ary tree of height h is called balanced if all leaves are at


levels h or h-1.

h=4 h=4 h=3


All leaves are at All leaves are at All leaves are at
levels 3, 4 levels 2,3, 4 levels 3
 Balanced  Not Balanced  Balanced
10.1 Introduction to Trees

THEOREM 5:
There are at most
h mh leaves in an m-ary tree of height h.
There are at most m leaves in an m-ary tree of height h.
Proof: page 754

COROLLARY 1:
 hash.l leaves, then h  log ml  .
If an m-ary tree of heightofhheight
 If the m-ary tree is full and balanced, then h  log ml  .

Proof: page 754


10.2 Applications of Trees
 Binary Search Trees
 Decision Trees
 Prefix Codes
Binary Search Trees
Binary search tree (BST) is a binary tree in which the
assigned key of a vertex is:
larger than the keys of all vertices in its left subtree, and
smaller than the keys of all vertices in its right subtree.

Ex:
6

2 7

9
1 4

5 8
3
Constructing a Binary Search Tree
Ex1: Construct a binary search tree for numbers: 23, 16, 43, 5, 9,
1, 6, 2, 33, 27.
23

16 43

5 33

1 9 27

2 6
Constructing a Binary Search Tree
Ex2: Form a binary search tree for the words mathematics, physics, geography,
zoology, meteorology, geology, psychology, and chemistry (using alphabetical
order).
Algorithm for inserting an element to BST

Complexity: O(logn)
Proof: page 759
Decision Trees
Ex: Suppose there are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are necessary
using a balance scale to determine which of the eight coins is the counterfeit
one? Give an algorithm for finding this counterfeit coin.

The Counterfeit Coin Problem


Decision Trees:
Sorting based on Binary Comparisons

THEOREM 1: A sorting algorithm based on binary comparisons


height h.
log
at leastrequires
 n ! comparisons.
COROLLARY 1 : The number of comparisons used by a sorting algorithm
height h.
to sort n elements based on binary comparisons is   n log n  .
THEOREM 2: The average number of comparisons used by a sorting algorithm
height h.
to sort n elements based on binary comparisons is   n log n  .
Prefix Codes
 Introduction to Prefix Codes
 Huffman Coding Algorithm
Prefix Codes: Introduction
 English word “sane” is stores 1 byte/character
 4-byte memory block is needed (32 bits).
 There are 26 characters  We can use 5 bit only to store a
character ( 25=32)
 The word “sane” can be stored in 20 bits
 May we can store this word in fewer bit?
Prefix Codes: Introduction
 Construct a binary tree with a prefix
code.
 “sane” will be store as
11111011100  11 bits
11111011100 : s
11111011100 : a
11111011100 : n
11111011100 : e
 Compression factor: 32/11 ~ 3
Prefix Codes:
Huffman Coding Algorithm
 Counting occurrences of characters in a text.
 frequencies (probabilities) of each character.
 Constructing a binary tree representing prefix codes of
characters.
 The set of binary codes representing each character.
 Coding source text.
Prefix Codes:
Huffman
Coding
Algorithm
Prefix Codes:
Huffman Coding Algorithm
Game Trees: The Game Nim

There are some piles of stones (ex: 2,2,1).


Two players will take turns picking stones from one pile.
The player picks the last stones is loser.
Game Trees: Tic-tac-toe
Game Trees…
Game Trees…
Traversal Algorithms
 At a time, a vertex is visited
 Operations are done:
– Process the visited vertex, e.g. list it’s information
– Traversing recursively subtree.
 Bases on orders of tasks, traversals are
classified into:
– Preorder traversal. N L R
– Inorder traversal. L N R
– Postorder traversal. L R N
10.3 Tree Traversal
 Traversal a tree: A way to visit all vertices of the rooted
tree.
– Universal Address Systems
– Traversal Algorithms
– Infix, Prefix, and Postfix Notation
Universal Address Systems
1. Label the root with the integer 0.

2. Then label its k children


(at level 1) from left to right
with 1, 2, 3, . . . , k.

3. For each vertex v at level n


with label A, label its kv children,
as they are drawn from left to
right, with A.1, A.2, . . . , A.kv
Preorder Traversal

a, b, e, f, c, d, g, h, i

b, e, j, k, n, o, p, f, m ?
Inorder Traversal

e, b, f, a, c, g, d, h, i

j, e, n, k, o, p, b, f, m
?
Postorder Traversal

e, f, b, c, g, h, i, d, a

j, n, o, p, k, e, m, f, b ?
Traverse
Algorithms
Example
In which order are the vertices of the ordered rooted
tree visited using:

a) A preorder traversal?
a, b, e, j , k , n, o, p, f , c, d , g , l , m, h, i

b) An inorder traversal?
j , e, n, k , o, p, b, f , a, c, l , g , m, d , h, i

c) A postorder traversal?
j , n, o, p, k , e, f , b, c, l , m, g , h, i, d , a
Infix, Prefix and Postfix Notation
Expression Trees
Infix, Prefix and Postfix Notation
Expression Trees
Infix, Prefix and Postfix Notation
 Infix form:
operand_1 operator operand_2 x+y
 Prefix form:
operator(operand_1,operand_2) +xy
 Postfix form:
(operand_1,operand_2)operator xy+

 How to find prefix and postfix form from infix form?


(1) Draw expression tree.
(2) Using Preorder traverse  Prefix form
Using Postorder traverse  Postfix form
Infix, Prefix and Postfix Notation
Infix form

Prefix form

Postfix form
Ex1: Find the value of
the prefix expression:
+-* 2 35/ 23 4
Prefix form:
operator(operand_1,operand_2)
Ex2: Find the value of
the postfix expression:
723*-49 3/+
Postfix form:
(operand_1,operand_2)operator
Example
Find the ordered rooted tree representing the compound proposition
(¬(p ∧ q)) ↔ (¬p ∨ ¬q).
Then use this rooted tree to find the prefix, postfix, and infix forms of this
expression.
Example
Find the ordered rooted tree representing the compound proposition
(¬(p ∧ q)) ↔ (¬p ∨ ¬q).
Then use this rooted tree to find the prefix, postfix, and infix forms of this
expression.

The prefix form is:

The postfix form is:

The infix form is:


10.4 Spanning trees
10.4 Spanning trees
Definition:
Let G be a simple graph. A spanning tree of G is a
subgraph of G that is a tree containing every vertex of G.
10.4 Spanning trees

Remove Edges That Form Simple Circuits.


10.4 Spanning trees
THEOREM 1
A simple graph is connected if and only if it has a spanning tree.
10.4 Spanning trees
Depth-First Search
EX: Use depth-first search to find a spanning tree for the graph G
shown in Figure 6.

f
g d

h e
k i c
j b a
Depth-First Search
Breadth-First Search
EX: Use breadth-first search to find a spanning tree for the graph
shown in Figure 9.
e

b d f i

a c g j k
h
l m
Breadth-First Search
Backtracking Applications
 Graph Colorings
 Then-Queens Problem
 Sums of Subsets
Graph Colorings - example
The n-Queens Problem
10.5 Minimum Spanning Trees

Which links should be made to ensure that there is a path between any
two computer centers so that the total cost of the network is minimized?
 minimum spanning tree: a spanning tree that has the smallest
possible sum of weights of its edges.
Algorithms for Minimum Spanning Trees
 Prim’s algorithm (1957 by Robert Prim) (originally discovered by
Vojtech Jarník in1930).
 Choosing any edge with smallest weight, putting it into the
spanning tree.
 Add to the tree edges of minimum weight that are incident to a
vertex already in the tree, never forming a simple circuit with
those edges already in the tree.
 Stop when n−1 edges have been added.
Use Prim’s algorithm to design a minimum-cost communications
network connecting all the computers represented by the graph in
Figure 1.
Use Prim’s algorithm to find a minimum spanning tree in the graph
shown in Figure 3.
Kruskal’s algorithm
(by Joseph Kruskal in 1956)
 Choose an edge in the graph with minimum weight.
 Add edges with minimum weight that do not form a simple
circuit with those edges already chosen.
 Stop after n−1 edges have been selected.
Use Kruskal’s algorithm to find a minimum spanning tree in the
weighted graph shown in Figure 3.
Summary
 Introduction to Trees
 Applications of Trees
 Tree Traversal
 Spanning Trees
 Minimum Spanning Trees
Thanks

You might also like