SlideShare a Scribd company logo
Trees
• A tree is a connected simple undirected graph
with no simple circuits.
• Properties:
o There is a unique simple path between any 2
of its vertices.
o No loops.
o No multiple edges.
Example 1
e ●
c ●
● f
●b
●a
● d
G1 : This graph is a Tree because it is a connected
graph with no simple circuits
Poll Question: Is below graph a tree?
a b
c
d
f
e
A. Yes
B. No
Poll Question: Is below graph a tree?
A. Yes
B. No
a
b
d
f
c
e
In this case it’s called forest in which
each connected component is a tree.
Component 1: a, f
Component 2: c, e, b, d
• An undirected graph without simple circuits is
called a forest.
– You can think of it as a set of trees having
disjoint sets of nodes.
Forest
Rooted (Directed) Trees
• A rooted tree is a tree in which one node has been
designated the root and every edge is directed
away from the root.
• You should know the following terms about
rooted trees:
Root, Parent, Child, Siblings, Ancestors,
Descendents, Leaf, Internal node, Subtree.
Definitions
• Root: Vertex with in-degree 0
[Node a is the root]
a
dc
b
f g
e
y
• Parent: Vertex u is a parent, such that there is directed edge
from u to v.
[b is parent of g and f ]
• Child: If u is parent of v, then v is child of u.
[g and f are children of b ]
• Siblings: Vertices with the same parents.
[f and g ]
• Ancestors: Vertices in path from the root to vertex v,
excluding v itself, including the root.
[Ancestors of g : b, a ]
Definitions
• Descendents: All vertices that have v as ancestors.
[Descendents of b : f, g, y ]
• Leaf: Vertex with no children.
[y, g, e, d ]
• Internal vertices: Vertices that have children.
[a, b, c, f ]
• Subtree: Subgraphs consisting of v and its descendents
and their incident edges.
Subtree rooted at b :
b
gf
y
Definitions
• Level (of v ) is length of unique path from root to
v.
[level of root = 0, level of b = 1, level of g = 2]
• Height is maximum of vertices levels.
[ Height = 3]
Definitions
m-ary Trees
• A rooted tree is called m-ary if every internal
vertex has no more than m children.
• It is called full m-array if every internal vertex
has exactly m children.
• A 2-ary tree is called a binary tree.
Example
b c
a
fed g
Full binary tree
Full 3-ary tree
Ordered Rooted Tree
• A rooted tree where the children of each internal
node are ordered.
• In ordered binary trees, we can define:
– left child, right child
– left subtree, right subtree
• For m-ary trees with m > 2, we can use terms like
“leftmost”, “rightmost,” etc.
Examples
a
b g
c e h j
d f k n m
Left child of c is d , Right child of c is f
h
nk
j
m
Left subtree of g Right subtree of g
Properties of Trees
1- A tree with n vertices has
n - 1 edges.
e.g. The tree in the figure has
14 vertices and 13 edges
2- A full m-ary tree with I internal vertices and L
leaves contains:
n = m × I + 1 vertices
n = I + L vertices
e.g. The full binary tree in
the figure has:
Internal vertices I = 6
Leaves L = 7
Vertices 13 = (2)(6) + 1
Properties of Trees
3- The level of a vertex in a rooted tree is the length
of the path from the root to the vertex (level of
the root is 0)
4- The height of the rooted tree is the maximum of
the levels of vertices (length of the longest path
from the root to any vertex)
Properties of Trees
Balanced Trees
• Balanced Tree
A rooted m-ary tree of height h is balanced if all
leaves are at levels h or h - 1.
Balanced Balanced Not Balanced
Representing Organizations The structure of a large organization
can be modeled using a rooted tree. Each vertex in this tree
represents a position in the organization. An edge from one
vertex to another indicates that the person represented by the
initial vertex is the (direct) boss of the person represented by the
terminal vertex.
Computer File Systems Files in computer memory can be
organized into directories. A directory can contain both files
and subdirectories. The root directory contains the entire file
system. Thus, a file system may be represented by a rooted
tree, where the root represents the root directory, internal
vertices represent subdirectories, and leaves represent ordinary
files or empty directories.
For a full m-ary tree:
(i) Given n vertices, I = (n – 1 ) / m internal vertices and
L = n – I = [(m – 1) × n + 1] / m leaves.
(ii) Given I internal vertices, n = m × I + 1 vertices and
L = n – I = (m – 1) × I + 1 leaves.
(iii) Given L leaves, n = (m × L – 1) / (m – 1) vertices and
I = n – L = (L – 1) / (m – 1) internal vertices.
In the previous example:
m = 2, n = 13 , I = 6 and L = 7
(i) I = (13 – 1) / 2 = 6 and L = 13 – 6 = 7
(ii) n = 2×6 + 1 = 13 and L = 13 – 6 = 7
(iii) n = (2×7 – 1 )/ (2 – 1) = 13 and I = 13 – 7 = 6
Summary
Applications of Trees
• Binary Search Trees
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.
A binary search tree 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,
and each vertex is labeled with a key, which is one
of the items. Furthermore, 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.
Binary Search Trees
Example 1
Form a binary search tree for the words mathematics,
physics, geography, zoology, meteorology, geology,
psychology, and chemistry (using alphabetical
order).
Mathematics, physics, geography, zoology, meteorology,
geology, psychology, and chemistry
Example 2
Set up a binary tree for the following list of numbers,
in an increasing order: 45, 13, 55, 50, 20, 60, 10, 15,
40, 32.
– What is the height of the shortest binary search
tree that can hold all 10 numbers?
– Is it a full binary tree?
– Is it a balanced tree?
45, 13, 55, 50, 20, 60, 10, 15, 40, 32
 The height of the binary search tree is 4
 It is not a full binary tree
 It is not a balance tree
45
13 55
10 20 50 60
32
15 40
Decision Trees
Rooted trees can be used to model problems in which a series
of decisions leads to a solution. For instance, a binary search
tree can be used to locate items based on a series of
comparisons, where each comparison tells us whether we
have located the item, or whether we should go right or left in
a subtree. A 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. The possible solutions of the problem correspond to the
paths to the leaves of this rooted tree.
Prefix Codes
Consider using bit strings of different lengths to encode
letters. When letters are encoded using varying numbers of
bits, some method must be used to determine where the bits
for each character start and end. One way to ensure that no
bit string corresponds to more than one sequence of letters is
to encode letters so that the bit string for a letter never occurs
as the first part of the bit string for another letter. Codes with
this property are called prefix codes.
The tree in the figure represents the encoding of
e by 0,
a by 10,
t by 110,
n by 1110,
s by 1111.
Example
Huffman Codes
• On the left tree the word rate is encoded
001 000 011 100
• On the right tree, the same word rate is encoded
11 000 001 10
Tree Traversal
• Traversal algorithms
o Pre-order traversal
o In-order traversal
o Post-order traversal
• Prefix / Infix / Postfix notation
Traversal Algorithms
Is visiting every vertex of ordered rooted tree.
• Pre-order: Root, Left, Right.
• In-order: Left, Root, Right.
• Post-order: Left, Right, Root.
Tree Traversals
• Pre-order traversal
• In-order traversal
• Post-order traversal
Pre-order: a b e j k n o p f c d g l m h i
In-order: j e n k o p b f a c l g m d h i
Post-order: j n o p k e f b c l m g h i d a
Pre-order: Root, Left, Right.
In-order: Left, Root, Right.
Post-order: Left, Right, Root.
Tree Traversals
4/20/2020
Infix, Prefix, and Postfix Notation
A tree can be used to represents mathematical
expressions.
Example: (( x + y )^2) + (( x – 4) / 3)
Prefix: + ^ + x y 2 / – x 4 3
Postfix: x y + 2 ^ x 4 – 3 / +
Infix: In-order traversal of tree must be fully parenthesized to
remove ambiguity.
Example: x + 5 / 3 : (x + 5) / 3 , x + (5 / 3)
Prefix (polish): Pre-order traversal of tree (no parenthesis
needed)
Example: From the above tree  + * + x y 2 / – x 4 3
Postfix: Post-order traversal (no parenthesis needed)
Example: From the above tree  x y + 2 * x 4 – 3 / +
Prefix Codes
1. Evaluating a Prefix Expression: (Pre-order: Right to left)
2. Evaluating a Postfix Expression: (Post-order: Left to right)

More Related Content

What's hot

Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
Himanshu Choudhary
 
Tree
TreeTree
Tree
TreeTree
Tree
bhumish
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
Rai University
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
Äshïsh Jäïn
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
Dabbal Singh Mahara
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
Tribhuvan University
 
Tree data structure
Tree data structureTree data structure
Tree data structureDana dia
 
Tree_Definition.pptx
Tree_Definition.pptxTree_Definition.pptx
Tree_Definition.pptx
sandeep54552
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of treeSiddhi Viradiya
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
Sumit Gupta
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structures
ASairamSairam1
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
maamir farooq
 
Trees
TreesTrees
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
Danish Aakash
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
 

What's hot (17)

Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
 
Tree
TreeTree
Tree
 
Tree
TreeTree
Tree
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
Tree data structure
Tree data structureTree data structure
Tree data structure
 
Tree_Definition.pptx
Tree_Definition.pptxTree_Definition.pptx
Tree_Definition.pptx
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structures
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Trees
TreesTrees
Trees
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Tree
TreeTree
Tree
 

Similar to Farhana shaikh webinar_treesindiscretestructure

Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
worldchannel
 
Tree
TreeTree
trees in data structure
trees in data structure trees in data structure
trees in data structure
shameen khan
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
SeethaDinesh
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
AnuradhaJadiya1
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptx
RahulAI
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
SurajSharma266169
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
Revathiparamanathan
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
LavanyaJ28
 
AD3251-Data Structures Design-Notes-Tree.pdf
AD3251-Data Structures  Design-Notes-Tree.pdfAD3251-Data Structures  Design-Notes-Tree.pdf
AD3251-Data Structures Design-Notes-Tree.pdf
Ramco Institute of Technology, Rajapalayam, Tamilnadu, India
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
Rai University
 
Binary tree
Binary treeBinary tree
Binary tree
Rajendran
 
Introduction to tree ds
Introduction to tree dsIntroduction to tree ds
Introduction to tree ds
Viji B
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
Rai University
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
ssuser5c874e
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
SSE_AndyLi
 

Similar to Farhana shaikh webinar_treesindiscretestructure (20)

Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Tree
TreeTree
Tree
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 trees
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptx
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
AD3251-Data Structures Design-Notes-Tree.pdf
AD3251-Data Structures  Design-Notes-Tree.pdfAD3251-Data Structures  Design-Notes-Tree.pdf
AD3251-Data Structures Design-Notes-Tree.pdf
 
Data structures 3
Data structures 3Data structures 3
Data structures 3
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
 
Binary tree
Binary treeBinary tree
Binary tree
 
Introduction to tree ds
Introduction to tree dsIntroduction to tree ds
Introduction to tree ds
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 

Recently uploaded

English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 

Farhana shaikh webinar_treesindiscretestructure

  • 1. Trees • A tree is a connected simple undirected graph with no simple circuits. • Properties: o There is a unique simple path between any 2 of its vertices. o No loops. o No multiple edges.
  • 2. Example 1 e ● c ● ● f ●b ●a ● d G1 : This graph is a Tree because it is a connected graph with no simple circuits
  • 3. Poll Question: Is below graph a tree? a b c d f e A. Yes B. No
  • 4. Poll Question: Is below graph a tree? A. Yes B. No a b d f c e In this case it’s called forest in which each connected component is a tree. Component 1: a, f Component 2: c, e, b, d
  • 5. • An undirected graph without simple circuits is called a forest. – You can think of it as a set of trees having disjoint sets of nodes. Forest
  • 6.
  • 7. Rooted (Directed) Trees • A rooted tree is a tree in which one node has been designated the root and every edge is directed away from the root. • You should know the following terms about rooted trees: Root, Parent, Child, Siblings, Ancestors, Descendents, Leaf, Internal node, Subtree.
  • 8. Definitions • Root: Vertex with in-degree 0 [Node a is the root] a dc b f g e y
  • 9. • Parent: Vertex u is a parent, such that there is directed edge from u to v. [b is parent of g and f ] • Child: If u is parent of v, then v is child of u. [g and f are children of b ] • Siblings: Vertices with the same parents. [f and g ] • Ancestors: Vertices in path from the root to vertex v, excluding v itself, including the root. [Ancestors of g : b, a ] Definitions
  • 10. • Descendents: All vertices that have v as ancestors. [Descendents of b : f, g, y ] • Leaf: Vertex with no children. [y, g, e, d ] • Internal vertices: Vertices that have children. [a, b, c, f ] • Subtree: Subgraphs consisting of v and its descendents and their incident edges. Subtree rooted at b : b gf y Definitions
  • 11. • Level (of v ) is length of unique path from root to v. [level of root = 0, level of b = 1, level of g = 2] • Height is maximum of vertices levels. [ Height = 3] Definitions
  • 12. m-ary Trees • A rooted tree is called m-ary if every internal vertex has no more than m children. • It is called full m-array if every internal vertex has exactly m children. • A 2-ary tree is called a binary tree.
  • 13. Example b c a fed g Full binary tree Full 3-ary tree
  • 14. Ordered Rooted Tree • A rooted tree where the children of each internal node are ordered. • In ordered binary trees, we can define: – left child, right child – left subtree, right subtree • For m-ary trees with m > 2, we can use terms like “leftmost”, “rightmost,” etc.
  • 15. Examples a b g c e h j d f k n m Left child of c is d , Right child of c is f h nk j m Left subtree of g Right subtree of g
  • 16. Properties of Trees 1- A tree with n vertices has n - 1 edges. e.g. The tree in the figure has 14 vertices and 13 edges
  • 17. 2- A full m-ary tree with I internal vertices and L leaves contains: n = m × I + 1 vertices n = I + L vertices e.g. The full binary tree in the figure has: Internal vertices I = 6 Leaves L = 7 Vertices 13 = (2)(6) + 1 Properties of Trees
  • 18. 3- The level of a vertex in a rooted tree is the length of the path from the root to the vertex (level of the root is 0) 4- The height of the rooted tree is the maximum of the levels of vertices (length of the longest path from the root to any vertex) Properties of Trees
  • 19. Balanced Trees • Balanced Tree A rooted m-ary tree of height h is balanced if all leaves are at levels h or h - 1. Balanced Balanced Not Balanced
  • 20. Representing Organizations The structure of a large organization can be modeled using a rooted tree. Each vertex in this tree represents a position in the organization. An edge from one vertex to another indicates that the person represented by the initial vertex is the (direct) boss of the person represented by the terminal vertex.
  • 21. Computer File Systems Files in computer memory can be organized into directories. A directory can contain both files and subdirectories. The root directory contains the entire file system. Thus, a file system may be represented by a rooted tree, where the root represents the root directory, internal vertices represent subdirectories, and leaves represent ordinary files or empty directories.
  • 22. For a full m-ary tree: (i) Given n vertices, I = (n – 1 ) / m internal vertices and L = n – I = [(m – 1) × n + 1] / m leaves. (ii) Given I internal vertices, n = m × I + 1 vertices and L = n – I = (m – 1) × I + 1 leaves. (iii) Given L leaves, n = (m × L – 1) / (m – 1) vertices and I = n – L = (L – 1) / (m – 1) internal vertices. In the previous example: m = 2, n = 13 , I = 6 and L = 7 (i) I = (13 – 1) / 2 = 6 and L = 13 – 6 = 7 (ii) n = 2×6 + 1 = 13 and L = 13 – 6 = 7 (iii) n = (2×7 – 1 )/ (2 – 1) = 13 and I = 13 – 7 = 6 Summary
  • 23. Applications of Trees • Binary Search Trees 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.
  • 24. A binary search tree 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, and each vertex is labeled with a key, which is one of the items. Furthermore, 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. Binary Search Trees
  • 25. Example 1 Form a binary search tree for the words mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry (using alphabetical order).
  • 26. Mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry
  • 27. Example 2 Set up a binary tree for the following list of numbers, in an increasing order: 45, 13, 55, 50, 20, 60, 10, 15, 40, 32. – What is the height of the shortest binary search tree that can hold all 10 numbers? – Is it a full binary tree? – Is it a balanced tree?
  • 28. 45, 13, 55, 50, 20, 60, 10, 15, 40, 32  The height of the binary search tree is 4  It is not a full binary tree  It is not a balance tree 45 13 55 10 20 50 60 32 15 40
  • 29. Decision Trees Rooted trees can be used to model problems in which a series of decisions leads to a solution. For instance, a binary search tree can be used to locate items based on a series of comparisons, where each comparison tells us whether we have located the item, or whether we should go right or left in a subtree. A 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. The possible solutions of the problem correspond to the paths to the leaves of this rooted tree.
  • 30.
  • 31.
  • 32. Prefix Codes Consider using bit strings of different lengths to encode letters. When letters are encoded using varying numbers of bits, some method must be used to determine where the bits for each character start and end. One way to ensure that no bit string corresponds to more than one sequence of letters is to encode letters so that the bit string for a letter never occurs as the first part of the bit string for another letter. Codes with this property are called prefix codes.
  • 33. The tree in the figure represents the encoding of e by 0, a by 10, t by 110, n by 1110, s by 1111. Example
  • 34. Huffman Codes • On the left tree the word rate is encoded 001 000 011 100 • On the right tree, the same word rate is encoded 11 000 001 10
  • 35. Tree Traversal • Traversal algorithms o Pre-order traversal o In-order traversal o Post-order traversal • Prefix / Infix / Postfix notation
  • 36. Traversal Algorithms Is visiting every vertex of ordered rooted tree. • Pre-order: Root, Left, Right. • In-order: Left, Root, Right. • Post-order: Left, Right, Root.
  • 37. Tree Traversals • Pre-order traversal • In-order traversal • Post-order traversal
  • 38. Pre-order: a b e j k n o p f c d g l m h i In-order: j e n k o p b f a c l g m d h i Post-order: j n o p k e f b c l m g h i d a Pre-order: Root, Left, Right. In-order: Left, Root, Right. Post-order: Left, Right, Root. Tree Traversals
  • 39. 4/20/2020 Infix, Prefix, and Postfix Notation A tree can be used to represents mathematical expressions. Example: (( x + y )^2) + (( x – 4) / 3) Prefix: + ^ + x y 2 / – x 4 3 Postfix: x y + 2 ^ x 4 – 3 / +
  • 40. Infix: In-order traversal of tree must be fully parenthesized to remove ambiguity. Example: x + 5 / 3 : (x + 5) / 3 , x + (5 / 3) Prefix (polish): Pre-order traversal of tree (no parenthesis needed) Example: From the above tree  + * + x y 2 / – x 4 3 Postfix: Post-order traversal (no parenthesis needed) Example: From the above tree  x y + 2 * x 4 – 3 / + Prefix Codes
  • 41. 1. Evaluating a Prefix Expression: (Pre-order: Right to left)
  • 42. 2. Evaluating a Postfix Expression: (Post-order: Left to right)