Lecture 7
Lecture 7
Lecture 7
قومية كلية
ل معتمدة من الهيئة ال عتماد
ضمان جودة التعليم واال
• Example:T1 is a full binary tree because each of its internal vertices has two children.
• T2 is a full 3-ary tree because each of its internal vertices has three children.
• In T3 each internal vertex has five children, so T3 is a full 5-ary tree.
• T4 is not a full m-ary tree for any m because some of its internal vertices have two children and others
have three children.
Trees as Models
• An Organizational Tree for a Computer Company
Trees as Models (Cont.)
• A Computer File System
Properties of Trees
• THEOREM: A tree with n vertices has n − 1 edges.
• Proof: We will use mathematical induction to prove this theorem.
• BASIS STEP: When n = 1, a tree with n = 1 vertex has no edges. It follows that the theorem is true for n = 1.
• INDUCTIVE STEP: The inductive hypothesis states that every tree with k vertices has k − 1 edges, where k is
a positive integer. Suppose that a tree T has k + 1 vertices and that v is a leaf of T (which must exist because
the tree is finite), and let w be the parent of v. Removing from T the vertex v and the edge connecting w to v
produces a tree T with k vertices, because the resulting graph is still connected and has no simple circuits. By
the inductive hypothesis, T has k − 1 edges. It follows that T has k edges because it has one more edge than T,
the edge connecting v and w. This completes the inductive step.
Properties of Trees (Cont.)
• The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex. The level
of the root is defined to be zero.
• The height of a rooted tree is the maximum of the levels of vertices.
• In other words, the height of a rooted tree is the length of the longest path from the root to any vertex.
• EXAMPLE: Find the level of each vertex in the following rooted tree. What is the height of this tree?
• Solution: The root a is at level 0.
• Vertices b, j , and k are at level 1.
• Vertices c, e, f , and l are at level 2.
• Vertices d, g, i, m, and n are at level 3.
• Vertex h is at level 4.
• Because the largest level of any vertex is 4, this tree has height 4.
Properties of Trees (Cont.)
• A rooted m-ary tree of height h is balanced if all leaves are at levels h or h − 1.
• EXAMPLE: Which of the following rooted trees are balanced?
• Solution: T1 is balanced, because all its leaves are at levels 3 and 4.
• T2 is not balanced, because it has leaves at levels 2, 3, and 4.
• T3 is balanced, because all its leaves are at level 3.
Properties of Trees (Cont.)
• THEOREM: There are at most mh leaves in an m-ary tree of height h.
Binary Search Trees
• A binary search tree, which is a binary tree in which each child of a vertex is designated as a right or left
child, no vertex has more than one right child or left child,
• each vertex is labeled with a key.
• Vertices are assigned keys so that 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.
• EXAMPLE: Form a binary search tree for the words mathematics, physics, geography, zoology, meteorology,
geology, psychology, and chemistry (using alphabetical order).
ALGORITHM: Locating an Item in or Adding an Item to a
Binary Search Tree
• THEOREM:A sorting algorithm based on binary comparisons requires at least log 𝑛! comparisons.
• log 𝑛! is θ(n log n),
HUFFMAN CODING
• Huffman coding is a fundamental algorithm in data compression, the subject devoted to reducing the number
of bits required to represent information.
• Huffman coding is extensively used to compress bit strings representing text and it also plays an important
role in compressing audio and image files.
• The algorithm begins with a forest of trees each consisting of one vertex, where each vertex has a symbol as
its label and where the weight of this vertex equals the frequency of the symbol that is its label.
• At each step, we combine two trees having the least total weight into a single tree by introducing a new root
and placing the tree with larger weight as its left subtree and the tree with smaller weight as its right subtree.
• Furthermore, we assign the sum of the weights of the two subtrees of this tree as the total weight of the tree.
• Example: Use Huffman coding to encode the following symbols with the frequencies listed: A: 0.08, B: 0.10,
C: 0.12, D: 0.15, E: 0.20, F: 0.35. What is the average number of bits used to encode a character?
HUFFMAN CODING (Cont.)
• Solution: The encoding produced encodes A by 111, B by 110, C by 011, D by 010, E by 10, and F by 00. The
average number of bits used to encode a symbol using this encoding is
• 3 · 0.08 + 3 · 0.10 + 3 · 0.12 + 3 · 0.15 + 2 · 0.20 + 2 · 0.35 = 2.45.
Tree Traversal
• Ordered rooted trees are often used to store information.We need procedures for visiting each vertex of an
ordered rooted tree to access data.
The Universal Address System of an Ordered Rooted Tree
Traversal Algorithms
• Preorder traversal
• Inorder traversal
• Postorder traversal
Preorder traversal
ALGORITHM:Preorder Traversal.
procedure preorder(T : ordered rooted tree)
r := root of T
list r
for each child c of r from left to right
T (c) := subtree with c as its root
preorder(T (c))
Inorder traversal
ALGORITHM:Postorder Traversal.
procedure postorder(T : ordered rooted tree)
r := root of T
for each child c of r from left to right
T (c) := subtree with c as its root
postorder(T (c))
list r