0% found this document useful (0 votes)
23 views68 pages

Chapter 11

Uploaded by

soovamsg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views68 pages

Chapter 11

Uploaded by

soovamsg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

Trees

Chapter 11
11.1 Introduction to TREE
and its Terminologies
Graph VS TREE

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Trees
Definition: A tree is a connected undirected graph
with no simple circuits.

Definition: An undirected graph is a tree if and only if


there is a unique simple path between any two of its
vertices. A tree cannot contain multiple edges or
loops.

Definition: An undirected graph is a tree if and only if


there is a unique simple path between any two of its
vertices.
Trees
Example: Which of these graphs are trees?

Solution: G1 and G2 are trees - both are connected and have no


simple circuits. G3 is not a tree because e, b, a, d, e is a simple
circuit,. G4 is not a tree because it is not connected.
Forest
Definition: A forest is a graph that has no simple circuit, but is not
connected. Each of the connected components in a forest is a tree.
General form of TREEs
Applications
of Trees
Section 11.2

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Arthur Cayley
Trees as Models (1821-1895)

• Trees are used as models in computer science, chemistry, geology, botany,


psychology, and many other areas.

• Trees were introduced by the mathematician Cayley in 1857 in his work counting
the number of isomers of saturated hydrocarbons. The two isomers of butane are: .

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Trees as Models
• The organization of a computer file system into directories, subdirectories,
and files is naturally represented as a tree.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Trees as Models
• Trees are used to represent the structure of organizations.
Applications of Trees
• Game Trees
Trees can be used to analyze certain types of games such as
tic-tac-toe, nim, checkers, and chess.
Universal Address Systems

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Prefix
Definition: code
A code that has the property that the code of
a character is never a prefix of the code of another
character.

• A prefix code can be represented using a binary tree,


where the characters are the labels of the leaves in the
tree.
• The edges of the tree are labeled so that an edge
leading to a left child is assigned a 0 and an edge leading
to a right child is assigned a 1.
• The bit string used to encode a character is the
sequence of labels of the edges in the unique path from
the root to the leaf that has this character as its label.
• For instance, the tree in Figure 5 represents the
encoding of e by 0, a by 10, t by 110, n by 1110, and s
by 1111.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Huffman Coding
Huffman code is a way to encode information using variable-length
strings to represent symbols depending on how frequently they
appear. The idea is that symbols that are used more frequently
should be shorter while symbols that appear more rarely can be
longer.
Decision Trees
Definition: A rooted tree where each vertex represents a possible outcome of a
decision and the leaves represent the possible solutions of a problem.
• Rooted trees can be used to model problems in which a series of decisions leads
to a solution.
• The possible solutions of the problem correspond to the paths to the leaves of
this rooted tree.

Example : A decision tree that orders the


elements of the list a, b, c.
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.
• An unrooted tree is converted into different rooted trees when
different vertices are chosen as the root.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Rooted Tree Terminology
• Terminology for rooted trees is a mix from botany and genealogy (such as
this family tree of the Bernoulli family of mathematicians).
Rooted Tree Terminology
• If v is a vertex of a rooted tree 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 a parent of v, v
is called a child of u. Vertices with the same parent are called siblings.
Rooted Tree Terminology
• The ancestors of a vertex 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. The
subtree rooted at u includes all the descendants of u, and all edges that connect
between them.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Rooted Tree Terminology
• A vertex of a rooted tree with no children is called a leaf.
Vertices that have children are called internal vertices.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Terminology for Rooted Trees
Example: In the rooted tree T (with
root a):
(i) Find the parent of c, the children
of g, the siblings of h, the
ancestors of e, and the
descendants of b.

Solution:
(ii) 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.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Terminology for Rooted Trees
Example: In the rooted tree T (with
root a):
(i) Find all internal vertices and
all leaves.
Solution:
(ii) The internal vertices are a, b,
c, g, h, and j. The leaves are d,
e, f, i, k, l, and m.
Terminology for Rooted Trees

(i) What is the subtree rooted


at g?
Solution:
(ii) We display the subtree
rooted at g.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Level of vertices and
height
• When working with ofto have
trees, we often want treesrooted trees where the
sub trees at each vertex contain paths of approximately the same length.
• To make this idea precise we need some definitions:
• The level of a vertex v in a rooted tree is the length of the unique path from the
root to this vertex.
• The height of a rooted tree is the maximum of the levels of the vertices.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Level of vertices and height of trees
Example:
(i) Find the level of each vertex in
the tree to the right.
(ii) What is the height of the tree?

Solution:

(i) 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.

(ii) The height is 4, since 4 is the largest level of any vertex.


FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
m-ary Rooted Trees
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-

tree with m = 2 is called a binary tree.


ary tree if every internal vertex has exactly m children. An m-ary

Example: Are the following rooted trees full m-ary trees for some
positive integer m?

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Solution:
• T1 is a full binary tree because each of its internal vertices has

T2 is a full 3-ary tree because each of its internal vertices has


two children.

three children.

5-ary tree.
• In T3 each internal vertex has five children, so T3 is a full

• 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.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Balanced m-Ary Trees
levels h or h − 1.
Definition: A rooted m-ary tree of height h is balanced if all leaves are at

Example: Which of the rooted trees shown below is balanced?

levels 2, 3, and 4.
Solution: T1 and T3 are balanced, but T2 is not because it has leaves at

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Ordered Rooted Trees
Definition: An ordered rooted tree is a rooted tree where the children of
each internal vertex are ordered.
• We draw ordered rooted trees so that the children of each internal
vertex are shown in order from left to right.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Binary Trees
Definition: A binary tree is an ordered rooted where each internal
vertex has at most two children. If an internal vertex of a binary
tree has two children, the first is called the left child and the
second the right child. The tree rooted at the left child of a vertex
is called the left subtree of this vertex, and the tree rooted at the
right child of a vertex is called the right subtree of this vertex.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Example:
Consider the binary tree T.
(i) What are the left and right children of d?
(ii) What are the left and right sub trees of c?
Solution:

(i) The left child of d is f and the right child is g.


(ii) The left and right subtrees of c are displayed in (b) and (c).

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Properties of Trees
A tree with n vertices has n − 1 edges.
A full m-ary tree with i internal vertices has n = mi + 1


vertices.
• A full m-ary tree with:

• n vertices has i = (n − 1)/m internal vertices and


l = [(m − 1)n + 1]/m leaves,

• i internal vertices has n = mi + 1 vertices and


l = (m − 1)i + 1 leaves,

• l leaves has n = (ml − 1)/(m − 1) vertices and


i = (l − 1)/ (m − 1) internal vertices.

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


FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Binary Search Tree
Definition: A binary tree in which the vertices are labeled with items so that a label
of a vertex is greater than the labels of all vertices in the left subtree of this vertex
and is less than the labels of all vertices in the right subtree of this vertex.
• Searching for items in a list is one of the most important tasks that arises in
computer science.
• Our primary goal is to implement a searching algorithm that finds items
efficiently when the items are totally ordered. This can be accomplished through
the use of a binary search tree.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Example : Form a binary search tree for the words
mathematics, physics, geography, zoology,
meteorology, geology, psychology, and chemistry
(using alphabetical order).

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Tree
Traversal
Section 11.3

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
11.3 Tree Traversal
Traversal Algorithms Procedures for systematically visiting
every vertex of an ordered rooted tree are called traversal
algorithms. We will describe three of the most commonly
used such algorithms, preorder traversal, inorder traversal,
and postorder traversal.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Tree Traversal Shortcut
Preorder:
Inorder:
Postorder:

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Tree Traversal
Definition: Let T be an ordered rooted tree with root r. If T consists
only of r, then r is the preorder traversal of T. Otherwise, suppose that
T1, T2, …, Tn are the subtrees of r from left to right in T. The preorder
traversal begins by visiting r, and continues by traversing T 1 in
preorder, then T2 in preorder, and so on, until Tn is traversed in
preorder.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Inorder traversal: Visit
leftmost subtree, visit
root, visit other subtrees
left to right
Inorder Traversal
Definition: Let T be an ordered rooted tree with root r. If T consists
only of r, then r is the inorder traversal of T. Otherwise, suppose
that T1, T2, …, Tn are the subtrees of r from left to right in T. The
inorder traversal begins by traversing T1 in inorder, then visiting r,
and continues by traversing T2 in inorder, and so on, until Tn is
traversed in inorder.
Inorder traversal:
Visit leftmost subtree,
visit root, visit other
subtrees left to right
Postorder Traversal
Definition: Let T be an ordered rooted tree with root r. If T consists
only of r, then r is the postorder traversal of T. Otherwise, suppose
that T1, T2, …, Tn are the subtrees of r from left to right in T. The
postorder traversal begins by traversing T1 in postorder, then T2 in
postorder, and so on, after Tn is traversed in postorder, r is visited.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Postorder traversal:
Visit subtrees left to right;
visit root
Infix, Prefix, and Postfix
Notation
Expression
• Complex expressions Trees
can be represented using ordered rooted

• Consider the expression ((x + y) ↑ 2 ) + ((x − 4)/3).


trees.

• A binary tree for the expression can be built from the bottom
up, as is illustrated here.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Infix Notation
• An inorder traversal of the tree representing an expression
produces the original expression when parentheses are included
except for unary operations, which now immediately follow their
operands.
• We illustrate why parentheses are needed with an example that
displays three trees all yield the same infix representation.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Tree Traversal Shortcut
Preorder:
Inorder:
Postorder:

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Deep Understanding
(x + y) / (x + 3)

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Jan Łukasiewicz

Prefix Notation
(1878-1956)

• When we traverse the rooted tree representation of an expression in


preorder, we obtain the prefix form of the expression. Expressions

Polish logician Jan Łukasiewicz.


in prefix form are said to be in Polish notation, named after the

• Operators precede their operands in the prefix form of an


expression. Parentheses are not needed as the representation is

• The prefix form of ((x + y) ↑ 2 ) + ((x − 4)/3)


unambiguous.

is + ↑ + x y 2 / − x 4 3.
• Prefix expressions are evaluated by working from right to left. When
we encounter an operator, we perform the corresponding operation
with the two operations to the right.
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Prefix Notation
• Example: We show the steps used to evaluate a particular prefix
expression:

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Postfix Notation
• We obtain the postfix form of an expression by traversing its binary
trees in postorder. Expressions written in postfix form are said to be
in reverse Polish notation.

• x y + 2 ↑ x 4 − 3 / + is the postfix form of ((x + y) ↑ 2 ) + ((x −


• Parentheses are not needed as the postfix form is unambiguous.

4)/3).
• A binary operator follows its two operands. So, to evaluate an
expression one works from left to right, carrying out an operation
represented by an operator on its preceding operands.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Postfix Notation
• Example: We show the steps used to evaluate a particular postfix
expression.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Spanning
Trees
Section 11.4

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Spanning Tree
A spanning tree is a subset of Graph G, which has all
the vertices covered with minimum possible number
of edges. Hence, a spanning tree does not have cycles
and it cannot be disconnected.
By this definition, we can draw a conclusion that every
connected and undirected Graph G has at least one
spanning tree. A disconnected graph does not have any
spanning tree, as it cannot be spanned to all its
vertices.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists
For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Kirchhoff's Matrix tree theorem
Understanding Minimum
spanning tree
A minimum spanning tree is a spanning tree in
which the sum of the weight of the edges is as
minimum as possible.

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Minimum spanning tree

A minimum spanning tree in a connected


weighted graph is a spanning tree that has the
smallest possible sum of weights of its edges.

• Prim’s algorithm
• Kruskal’s Algorithm
PRIM’S ALGORITHM

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Minimal spanning tree (MST)
Example: Use Prims algorithm
to find a minimal spanning tree
for the graph below. Indicate
the order in which edges are
added to form the tree.
Minimal spanning tree (MST)
Example: Use Prims algorithm
to find a minimal spanning tree
for the graph below. Indicate
the order in which edges are
added to form the tree.

Order of adding the edges:


{a , b}, {b , c}, {c , d}, {d , e}, {e , f}, {f , g}

MST COST = 23
KRUSKAL’S ALGORITHM

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
Minimal spanning tree (MST)
Example: Use Kruskal’s
algorithm to find a minimal
spanning tree for the graph
below. Indicate the order in
which edges are added to
form the tree.
Minimal spanning tree (MST)
Example: Use Kruskal’s
algorithm to find a minimal
spanning tree for the graph
below. Indicate the order in
which edges are added to
form the tree.

MST COST = 23
11.4 Spanning Trees
Depth-First Search | Breadth-First Search |
Backtracking Applications | Depth-First
Search in Directed Graphs

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
11.4 Spanning Trees
Depth-First Search | Breadth-First Search |
Backtracking Applications | Depth-First
Search in Directed Graphs

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
11.4 Spanning Trees
Depth-First Search | Breadth-First Search |
Backtracking Applications | Depth-First
Search in Directed Graphs

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
11.4 Spanning Trees
Depth-First Search | Breadth-First Search |
Backtracking Applications | Depth-First
Search in Directed Graphs

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/
11.4 Spanning Trees
Depth-First Search | Breadth-First Search |
Backtracking Application| Depth-First
Search in Directed Graphs

FREE: For Complete Playlist: https://www.youtube.com/c/FahadHussaintutorial/playlists


For Code, Slide and Books: https://fahadhussaincs.blogspot.com/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy