You are on page 1of 64

Chapter 10

Trees
Objectives

 10.1- Introduction to Trees


 10.2- Applications of Trees
 10.3- Tree Traversal
10.1- Introduction to Trees
Some Tree Models

A Computer File System


Some Tree Models

An organizational Tree
Introduction to Trees

Definition

A tree (cây) is a connected undirected graph with no simple circuits.


Consequently, a tree must be a simple graph.
circuit disconnected
Introduction to Trees

THEOREM 1

A tree A tree
Introduction to Trees
Introduction to Trees

Definition 2
DEFINITION
A rooted tree (cây có gốc) is a tree in which:
oOne vertex has been designated as the root and
oEvery edge is directed away from the root
Introduction to Trees

Terminology

Definition
o Parent (cha) of v is the unique u such that there is a directed edge from u to v.
o When u is the parent of v, v is called a child (con) of u.
o Vertices with the same parent are called siblings (anh em)
o The ancestors (tổ tiên) of a vertex are the vertices in the path from the root to
this vertex (excluding the vertex itself)
o Descendants (con cháu) of a vertex v are those vertices that have v as an
ancestor
o A vertex of a tree is called a leaf (lá) if it has no children
o Vertices that have children are called internal vertices (đỉnh trong)
Introduction to Trees
Terminology
siblings

Subtree with b
as root

Parent of u:
Child of g:
Ancestors of f:
Descendants b:
Leaves (lá):
Internal vertices (đỉnh trong):
Introduction to Trees

Full Full Full


Binary Ternary 5-ary 3-ary

Some terminologies on binary tree:


Left child, right child, left subtree, right subtree
Introduction to Trees

Ordered rooted tree


Properties of Trees
Theorem

A tree with n vertices has n – 1 edges.

Theorem

For a full m-ary tree:


•n = mi + 1 vertices. with i: number of internal vertices
•n = i + l l: number of leaves

Example
Properties of Trees
Theorem

A tree with n vertices has n – 1 edges.

Theorem

For a full m-ary tree:


•n = mi + 1 vertices. with i: number of internal vertices
•n = i + l l: number of leaves
Example
1/ How many edges does a full binary tree with 100 internal vertices
have?
Properties of Trees

Example

2/ How many leaves are there in a full 5-ary tree with 56 node?

3/ How many vertices are there in a full ternary tree with 55 leaves?
Level and Height

 Level of a vertex: The length of the path from the root to this vertex.
 The level of the root is defined to be zero.
 Height of tree: The maximum of levels of vertices = The length of the
longest path from the root to any vertex.
Level and Height

A m-ary tree is called balanced if all leaves are at levels h or h-1.

Example

h=4 h=4
All leafs are at levels 3, 4 All leafs are at levels 2,3, 4
 Balanced  Not Balanced
Properties of Trees
Theorem

There are at most mh leaves in an m-ary tree of height h.

Corollary

• If an m-ary tree of height h has l leaves, then h   log m l 


• If an m-ary tree is full and balanced, then h   log m l  .
Example

Which of the following statements are true?

(i)If T is a binary tree with 101 vertices, its minimum height is 5.


(ii)If T is a binary tree with 101 vertices, its minimum height is 7.
(iii)Every full binary tree with 61 vertices has 30 leaves.
10.2- Applications of Trees

 Binary Search Trees


 Decision Trees
 Prefix Codes
Constructing a Binary Search Tree

• A binary tree where each vertex is labeled with a key.


• The key of a vertex is both larger than the keys of all vertices in its left
subtree and smaller than the keys of all vertices in its right subtree.
Constructing a Binary Search Tree

Add a new vertex

Example How to add 2 to the binary search tree?


(Same question for 9, 15, 5)
Constructing a Binary Search Tree

Example

Construct a binary search tree for numbers: 23, 16, 43, 5, 9, 1, 6, 2,


33, 27.

How many comparisons are required to locate number 27 in that


binary search tree?
Constructing a Binary Search Tree

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 698
Decision Trees
Decision Trees
Decision Trees

The Counterfeit Coin Problem

Suppose there are 7 coins (same weight), and 1 counterfeit coin (lighter).
How many weightings are necessary to determine the counterfeit coin?
(Using a balance scale).
Decision Trees

The Counterfeit Coin Problem

Suppose there are 7 coins (same weight), and 1 counterfeit coin (lighter).
How many weightings are necessary to determine the counterfeit coin?
(Using a balance scale).
Decision Trees

The Counterfeit Coin Problem

Suppose there are 7 coins (same weight), and 1 counterfeit coin (lighter).
How many weightings are necessary to determine the counterfeit coin?
(Using a balance scale).
Decision Trees
Sorting based on Binary Comparisons
Prefix Codes

 Introduction to Prefix Codes


 Huffman Coding Algorithm
Prefix Codes: Introduction
 English word “blue” 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 “blue” 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
Prefix Codes: Introduction
Example:
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

 Construct a binary tree with a prefix code to encode the word: “sane”.
Prefix Codes: Huffman Coding Algorithm
 Construct a binary tree with a prefix code to encode the word: “success”.
Prefix Codes: Huffman Coding Algorithm
 Construct a binary tree with a prefix code to encode the word: “football”.
Prefix Codes: Huffman Coding Algorithm
Traversal Algorithms

o At a time, a vertex is visited


o Operations are done:
– Process the visited vertex, e.g. list it’s information
– Traversing recursively subtree.
o Bases on orders of tasks, traversals are classified into:
– Preorder traversal. Node- Left subtree- Right subtree
– 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.


– Traversal Algorithms
– Infix, Prefix, and Postfix Notation
Preorder Traversal

a, b, e, f, c, d, g, h, i
Preorder Traversal
Inorder Traversal

e, b, f, a, c, g, d, h, i
Inorder Traversal
Postorder Traversal

e, f, b, c, g, h, i, d, a
Traverse Algorithms
Example:
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
Infix, Prefix, and Postfix Notation

Example:
Infix form
Infix, Prefix, and Postfix Notation
Infix, Prefix, and Postfix Notation

Example:
Infix, Prefix, and Postfix Notation

Example:
Infix, Prefix, and Postfix Notation

Example:
Infix, Prefix, and Postfix Notation

Example:
Summary

o 10.1- Introduction to Trees


o 10.2- Applications of Trees
o 10.3- Tree Traversal
Thanks

You might also like