Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Assiut University

‫قومية‬ ‫كلية‬
‫ل معتمدة من الهيئة ال عتماد‬
‫ضمان جودة التعليم واال‬

Course Title: Discrete Structures


Course Code: CS201

Prof. Dr. Khaled F. Hussain


Introduction to Trees
• DEFINITION:A tree is a connected undirected graph with no simple circuits.
• Because a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops. Therefore, any
tree must be a simple graph.
• Example: Which of the graphs shown in the following figure are trees?
• Solution: G1 and G2 are trees, because both are connected graphs with no simple circuits.
• G3 is not a tree because e, b, a, d, e is a simple circuit in this graph.
• G4 is not a tree because it is not connected.
Rooted Trees
• DEFINITION: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.
• Suppose that T is a rooted tree. If v is a vertex in T other than the root, the parent of v is the unique vertex u
such that there is a directed edge from u to v. When u is the parent of v, v is called a child of u.
• Vertices with the same parent are called siblings.
• The ancestors of a vertex other than the root are the vertices in the path from the root to this vertex, excluding
the vertex itself and including the root.
• The descendants of a vertex v are those vertices that have v as an ancestor.
• A vertex of a rooted tree is called a leaf if it has no children.
• Vertices that have children are called internal vertices.
Rooted Trees (Cont.)
• Example: In the following rooted tree (with root a), find the parent of c, the children of g, the siblings of h, all
ancestors of e, all descendants of b, all internal vertices, and all leaves. What is the subtree rooted at g?
• Solution: The parent of c is b. The children of g are h, i, and j . The siblings of h are i and j .
• The ancestors of e are c, b, and a. The descendants of b are c, d, and e.
• The internal vertices are a, b, c, g, h, and j . The leaves are d, e, f , i, k, l, and m. The subtree rooted at g is
shown in the Figure.
Rooted Trees (Cont.)
• DEFINITION: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.

• 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

procedure insertion(T : binary search tree, x: item)


v := root of T
{a vertex not present in T has the value null }
while v ≠ null and label(v) ≠ x
if x < label(v) then
if left child of v ≠ null then v := left child of v
else add new vertex as a left child of v and set v := null
else
if right child of v ≠ null then v := right child of v
else add new vertex as a right child of v and set v := null
if root of T = null then add a vertex v to the tree and label it with x
else if v is null or label(v) ≠ x then label new vertex with x and let v be this new vertex
return v {v = location of x}
Decision Trees
• Rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each
possible outcome of the decision, is called a decision tree.
• Example: Suppose there are seven coins, all with the same weight, and a counterfeit coin that weighs less
than the others. How many weightings 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.
• Solution: It is possible to determine the counterfeit coin using two weighings.
A Decision Tree for Sorting Three Distinct
Elements

• 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: Inorder Traversal.


procedure inorder(T : ordered rooted tree)
r := root of T
if r is a leaf then list r
else
l := first child of r from left to right
T (l) := subtree with l as its root
inorder(T (l))
list r
for each child c of r except for l from left to right
T (c) := subtree with c as its root
inorder(T (c))
Postorder 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

You might also like