10 - Trees
10 - Trees
10 - Trees
Trees
Objectives
An organizational Tree
Introduction to Trees
Definition
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
Theorem
Example
Properties of Trees
Theorem
Theorem
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
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
Corollary
Example
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
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
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
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
11111011100 : s
11111011100 : a
11111011100 : n
11111011100 : e
Compression factor: 32/11 ~ 3
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
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