SlideShare a Scribd company logo
1.Tree
2.Grap
h
Why Tree is considered a non-linear data structure?
In computer science, a tree is a widely-used
data structure that emulates a hierarchical tree
structure with a set of linked nodes.
Nature View of a Tree
branch
e
s
leave
s
root
Computer Scientist’s View
leave
s
roo
t
branches
node
s
What is a Tree
⚫ A tree is a finite nonempty
set of elements.
⚫ It is an abstract model
of a hierarchical
structure.
⚫ consists of nodes with a
parent-child relation.
⚫ Applications:
⚫ Organization charts
⚫ File systems
⚫ Programming
environments
Computers”R”Us
Sale
s
R&
D
Manufacturin
g
Laptop
s
Desktop
s
US International
Europ
e
Asi
a
Canad
a
Tree Representation
Cont…
 Root-The top most
Node.
 Children
 Parents
 Siblings- Have same
parents.
 Leaf- Has no Child.
RELATION OF TREE
1
3
2
4
6
5
8
7
11
10
9
8
Trees Data Structures
⚫Tree
⚫Nodes
⚫Each node can have 0 or more
children
⚫A node can have at most one
parent
⚫Binary tree
⚫Tree with 0–2 children per node
Tre Binary Tree 9
Tree
s
⚫Terminology
⚫Root  no parent
⚫Leaf  no child
⚫Interior  non-leaf
⚫Height  distance from root to
leaf
Root node
Leaf nodes
Interior nodes Height
1
Example Tree
roo
t
Activitie
s
Teachin
g
Researc
h
Sami’s Home
Page
Paper
s
Presentatio
ns
CS21
1
CS10
1
Tree Terminology
⚫Path: Traversal from node to node along the edges
results
in a sequence called path.
⚫Root: Node at the top of the tree.
⚫Parent: Any node, except root has exactly one edge
running upward to another node. The node above it
is called parent.
⚫Child: Any node may have one or more lines
running downward to other nodes. Nodes
below are children.
⚫Leaf: A node that has no children.
⚫Subtree: Any node can be considered to be the
root of a subtree, which consists of its children
subtre
e
Tree Terminology
⚫ Root: node without parent (A)
⚫ Siblings: nodes share the same parent
⚫ Internal node: node with at least
one child (A, B, C, F)
⚫ External node (leaf ): node
without children (E, I, J, K, G, H,
D)
⚫ Ancestors of a node: parent,
grandparent, grand-grandparent, etc.
⚫ Descendant of a node: child,
grandchild,
grand-grandchild, etc.
⚫ Depth of a node: number of ancestors
⚫ Height of a tree: maximum depth of
any node (3)
⚫ Degree of a node: the number of
its children
⚫ Degree of a tree: the maximum
number of
its node.
A
B D
C
G H
E F
I J K
Subtree: tree consisting of a
node and its descendants
General Trees
⚫Trees store data in a hierarchical
manner
⚫ Root node is A
⚫ A's children are B, C, and D
⚫ E, F, and D are leaves
⚫ Length of path from A to E is 2
edges
D
A
C
F
B
E
Trees have layers of nodes,
where some are higher,
others are lower.
Logic of Tr
Used to represent hierarchical
data.

BS(CS) 1ST tree e.g.
BS(CS)1st
Section (A)
Section (B)
CR 1
CR2
group
1
group
2
group 3 group
4
group
1
group
2
group
3
group
4
1
Logic of Tr
Used to represent hierarchical
data.

BS(CS) 1ST tree e.g.
BS(CS)1st
Section (A)
Section (B)
CR 1
CR2
group
1
group
2
group 3 group
4
group
1
group
2
group
3
group
4
1
Cont…
 A Collection of entities called
Nodes.
 Tree is a Non-Linear Data
Structure.
 It’s a hierarchica Structure.
TREE
Nodes
1
Tree Traversal
⚫Unlike linear data structures (Array, Linked
List, Queues, Stacks, etc) which have only one
logical way to traverse them, trees can be
traversed in different ways.
⚫Traversal is a process to visit all the nodes of a
tree and may print their values too. Because, all
nodes are connected via edges (links) we always
start from the root (head) node.
Cont…
⚫ That is, we cannot randomly access a node in a tree.
There are
three ways which we use to traverse a tree
⚫ (a) Inorder (Left, Root, Right) : 4 2 5 1
3
(b) Preorder (Root, Left, Right) : 1 2 4
5 3
(c) Postorder (Left, Right, Root) : 4 5 2
3 1
⚫
In-order Traversal
⚫In this traversal method, the left subtree is
visited first, then the root and later the right sub-
tree. We should always remember that every
node may represent a subtree itself.
Inorder (Left, Root, Right)
Inorder (Left, Root, Right)
⚫We start from A, and following in-order
traversal, we move to its left subtree B. B is also
traversed in-order. The process goes on until all
the nodes are visited. The output of inorder
traversal of this tree will be −
⚫D → B → E → A → F → C → G
Inorder Algorithm
⚫Until all nodes are traversed −
⚫ Step 1 − Recursively traverse left
subtree.
⚫ Step 2 − Visit root node.
⚫Step 3 − Recursively traverse right
subtree.
Pre-order Traversal
⚫In this traversal method, the root node is visited
first, then the left subtree and finally the right
subtree.
Preorder (Root, Left, Right)
Preorder (Root, Left, Right)
⚫ We start from A, and following pre-order traversal, we first visit A
itself and then move to its left subtree B. B is also traversed pre-order.
The process goes on until all the nodes are visited. The output of pre-
order traversal of this tree will be.
⚫ A → B → D → E → C → F →
G
Algorithm
⚫Until all nodes are traversed −
⚫Step 1 − Visit root node.
⚫Step 2 − Recursively traverse left
subtree.
⚫ Step 3 − Recursively traverse right
subtree.
Post-order Traversal
⚫In this traversal method, the root node is visited
last, hence the name. First we traverse the left
subtree, then the right subtree and finally the
root node.
Postorder (Left, Right, Root)
Postorder (Left, Right, Root)
⚫ We start from A, and following pre-order traversal, we first visit
the left subtree B. B is also traversed post-order. The process
goes on until all the nodes are visited. The output of post-order
traversal of this tree will be.
⚫ D → E → B → F → G → C →
A
Algorithm
⚫Until all nodes are traversed
⚫ Step 1 − Recursively traverse left
subtree.
⚫ Step 2 − Recursively traverse right
subtree.
⚫ Step 3 − Visit root node.
1.Insertio
n
2.Deletion
3.Searchin
g
• If root is NULL
• then create root node
• return
• If root exists then
• compare the data with node.data
• while until insertion position is
located
• If data is greater than node.data
• goto right subtree
• else
• goto left subtree
• endwhile
• insert data
If root.data is equal to
search.data return root
else
while data not found
If data is greater than
node.data goto right subtree
else
goto left
subtree If
data found
return node
endwhile
return data not
found end if
Deletion
algorithm ????
Why Tree is considered a non-linear data structure?
There are a large number of tree types, however three
types specifically are used the majority of the time (both
in real world development and in Computer Science
courses). The types are:
 Binary trees
 B-Trees
 Heaps
Binary trees
⚫Binary tree follows the rule that each parent
node has no more than 2 child nodes. This
means that a binary tree can look like this:
⚫ OR
Strict Binary Tree
⚫Strict Binary Tree is a Binary tree in which
root can have exactly two children or no
children at all. That means, there can be 0 or
2 children of any node.
Complete Binary Tree
⚫Complete Binary Tree is a Strict Binary tree in
which every leaf node is at same level. That
means, there are equal number of children in
right and left subtree for every node.
Extended Binary Tree
⚫An extended binary tree is a transformation
of any binary tree into a complete binary
tree.
⚫This transformation consists of replacing
every null subtree of the original tree with
“special nodes.”
⚫The nodes from the original tree are then called
as internal nodes, while the “special nodes” are
called as external nodes.
Extended Binary Tree
A binary tree with special nodes replacing every null subtree. Every
regular node
has two children, and every special node has no children.
Binary Search Tree
⚫ The value at any node, • Greater than every value in left subtree.
Smaller than every value in right subtree – Example
⚫Y > X
⚫Y < Z
⚫ Values in left sub tree less than parent
⚫ Values in right sub tree greater than parent
⚫ Fast searches in a Binary Search tree, maximum of log n
comparisons.
Binary Search Trees
⚫Example
s
⚫Binary search
tree
B - Trees
⚫B – Tree is another type of search tree called B-
Tree in which a node can store more than one
value (key) and it can have more than two
children.
⚫B-Tree can be defined as follows…
⚫ B-Tree is a self-balanced search tree with multiple keys in every node and more
than two
children for every node.
Operations on a B-Tree
⚫The following operations are performed on a B-
Tree...
⚫Search
⚫Insertion
⚫Deletion
Heap Tree
⚫Heap is a special case of balanced binary tree
data structure where the root-node key is
compared with its children and arranged
accordingly. If α has child
node β then
⚫key(α) ≥ key(β)
⚫A heap can be of two types
⚫ Min-Heap
⚫ Max-Heap
Max-Heap
⚫Max-Heap − Where the value of the root
node is greater than or equal to either of its
children.
Max-Heap insertion
Max-Heap deletion
Max Heap Construction Algorithm
⚫Step 1 − Create a new node.
⚫ Step 2 − Assign new value to the node.
⚫Step 3 − Compare the value of this child node
with its parent.
⚫Step 4 If
− value of parent is less than child,
then swap them.
⚫Step 5 − Repeat step 3 & 4 until Heap property
holds.
Max Heap Deletion
Algorithm
⚫Step 1 − Remove root node.
⚫Step 2 − Move the last element of last level to
root.
⚫Step 3 − Compare the value of this child node
with its parent.
⚫Step 4 If
− value of parent is less than child,
then swap them.
⚫Step 5 − Repeat step 3 & 4 until Heap property
holds.
Min-Heap
⚫Min-Heap − Where the value of the root node
is less than or equal to either of its children.
Min-Heap insertion
Min-Heap deletion
Ad

More Related Content

Similar to Why Tree is considered a non-linear data structure? (20)

Data Structure And Algorithms for Computer Science
Data Structure And Algorithms for Computer ScienceData Structure And Algorithms for Computer Science
Data Structure And Algorithms for Computer Science
ManishShukla712917
 
9. TREE Data Structure Non Linear Data Structure
9. TREE Data Structure Non Linear Data Structure9. TREE Data Structure Non Linear Data Structure
9. TREE Data Structure Non Linear Data Structure
kejika1215
 
Data Structure in Tree form. .pptx
Data Structure in Tree form.       .pptxData Structure in Tree form.       .pptx
Data Structure in Tree form. .pptx
mhumza1976
 
lecture 17,18. .pptx
lecture 17,18.                      .pptxlecture 17,18.                      .pptx
lecture 17,18. .pptx
mhumza1976
 
lecture 17,18. .pptx
lecture 17,18.                      .pptxlecture 17,18.                      .pptx
lecture 17,18. .pptx
mhumza1976
 
unit 4 for trees data structure notes it is
unit 4 for trees data structure notes it isunit 4 for trees data structure notes it is
unit 4 for trees data structure notes it is
logesswarisrinivasan
 
Trees
TreesTrees
Trees
Shankar Bishnoi
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
maamir farooq
 
Team-hawks_DSA_binary_tree[1] [Read-Only].pptx
Team-hawks_DSA_binary_tree[1] [Read-Only].pptxTeam-hawks_DSA_binary_tree[1] [Read-Only].pptx
Team-hawks_DSA_binary_tree[1] [Read-Only].pptx
XEON14
 
Introduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptxIntroduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
Trees.pptx
Trees.pptxTrees.pptx
Trees.pptx
REMEGIUSPRAVEENSAHAY
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
worldchannel
 
Tree
TreeTree
Tree
Timothy Pastoral
 
Data structure using c module 2
Data structure using c module 2Data structure using c module 2
Data structure using c module 2
smruti sarangi
 
7.tree
7.tree7.tree
7.tree
Chandan Singh
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
Dabbal Singh Mahara
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
LavanyaJ28
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
Waheed Khalid
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
Data Structure And Algorithms for Computer Science
Data Structure And Algorithms for Computer ScienceData Structure And Algorithms for Computer Science
Data Structure And Algorithms for Computer Science
ManishShukla712917
 
9. TREE Data Structure Non Linear Data Structure
9. TREE Data Structure Non Linear Data Structure9. TREE Data Structure Non Linear Data Structure
9. TREE Data Structure Non Linear Data Structure
kejika1215
 
Data Structure in Tree form. .pptx
Data Structure in Tree form.       .pptxData Structure in Tree form.       .pptx
Data Structure in Tree form. .pptx
mhumza1976
 
lecture 17,18. .pptx
lecture 17,18.                      .pptxlecture 17,18.                      .pptx
lecture 17,18. .pptx
mhumza1976
 
lecture 17,18. .pptx
lecture 17,18.                      .pptxlecture 17,18.                      .pptx
lecture 17,18. .pptx
mhumza1976
 
unit 4 for trees data structure notes it is
unit 4 for trees data structure notes it isunit 4 for trees data structure notes it is
unit 4 for trees data structure notes it is
logesswarisrinivasan
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
maamir farooq
 
Team-hawks_DSA_binary_tree[1] [Read-Only].pptx
Team-hawks_DSA_binary_tree[1] [Read-Only].pptxTeam-hawks_DSA_binary_tree[1] [Read-Only].pptx
Team-hawks_DSA_binary_tree[1] [Read-Only].pptx
XEON14
 
Introduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptxIntroduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
Data structure using c module 2
Data structure using c module 2Data structure using c module 2
Data structure using c module 2
smruti sarangi
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
Waheed Khalid
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 

Recently uploaded (20)

Introduction to MedDRA hgjuyh mnhvnj mbv hvj jhgjgjgjg
Introduction to MedDRA hgjuyh mnhvnj mbv hvj jhgjgjgjgIntroduction to MedDRA hgjuyh mnhvnj mbv hvj jhgjgjgjg
Introduction to MedDRA hgjuyh mnhvnj mbv hvj jhgjgjgjg
MichaelTuffourAmirik
 
Peeling the onion: How to move through multiple discovery and analysis cycles
Peeling the onion: How to move through multiple discovery and analysis cyclesPeeling the onion: How to move through multiple discovery and analysis cycles
Peeling the onion: How to move through multiple discovery and analysis cycles
Process mining Evangelist
 
From Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
From Data to Insight: How News Aggregator APIs Deliver Contextual IntelligenceFrom Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
From Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
Contify
 
End to End Process Analysis - Cox Communications
End to End Process Analysis - Cox CommunicationsEnd to End Process Analysis - Cox Communications
End to End Process Analysis - Cox Communications
Process mining Evangelist
 
Feature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record SystemsFeature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record Systems
Process mining Evangelist
 
MLOps_with_SageMaker_Template_EN idioma inglés
MLOps_with_SageMaker_Template_EN idioma inglésMLOps_with_SageMaker_Template_EN idioma inglés
MLOps_with_SageMaker_Template_EN idioma inglés
FabianPierrePeaJacob
 
PN_Junction_Diode_Typdbhghfned_Notes.pdf
PN_Junction_Diode_Typdbhghfned_Notes.pdfPN_Junction_Diode_Typdbhghfned_Notes.pdf
PN_Junction_Diode_Typdbhghfned_Notes.pdf
AryanGohil1
 
Unit 2 - Unified Modeling Language (UML).pdf
Unit 2 - Unified Modeling Language (UML).pdfUnit 2 - Unified Modeling Language (UML).pdf
Unit 2 - Unified Modeling Language (UML).pdf
sixokak391
 
Mixed Methods Research.pptx education 201
Mixed Methods Research.pptx education 201Mixed Methods Research.pptx education 201
Mixed Methods Research.pptx education 201
GraceSolaa1
 
Ann Naser Nabil- Data Scientist Portfolio.pdf
Ann Naser Nabil- Data Scientist Portfolio.pdfAnn Naser Nabil- Data Scientist Portfolio.pdf
Ann Naser Nabil- Data Scientist Portfolio.pdf
আন্ নাসের নাবিল
 
The-Future-is-Now-Information-Technology-Trends.pptx.pdf
The-Future-is-Now-Information-Technology-Trends.pptx.pdfThe-Future-is-Now-Information-Technology-Trends.pptx.pdf
The-Future-is-Now-Information-Technology-Trends.pptx.pdf
winnt04
 
Language Learning App Data Research by Globibo [2025]
Language Learning App Data Research by Globibo [2025]Language Learning App Data Research by Globibo [2025]
Language Learning App Data Research by Globibo [2025]
globibo
 
Storage Devices and the Mechanism of Data Storage in Audio and Visual Form
Storage Devices and the Mechanism of Data Storage in Audio and Visual FormStorage Devices and the Mechanism of Data Storage in Audio and Visual Form
Storage Devices and the Mechanism of Data Storage in Audio and Visual Form
Professional Content Writing's
 
Bringing data to life - Crime webinar Accessible.pptx
Bringing data to life - Crime webinar Accessible.pptxBringing data to life - Crime webinar Accessible.pptx
Bringing data to life - Crime webinar Accessible.pptx
Office for National Statistics
 
2-Cholera-Outbreaks-and-Waterborne-Pathogens-Typhoid-fever (1).pdf
2-Cholera-Outbreaks-and-Waterborne-Pathogens-Typhoid-fever (1).pdf2-Cholera-Outbreaks-and-Waterborne-Pathogens-Typhoid-fever (1).pdf
2-Cholera-Outbreaks-and-Waterborne-Pathogens-Typhoid-fever (1).pdf
AngelitaVergara1
 
web-roadmap developer file information..
web-roadmap developer file information..web-roadmap developer file information..
web-roadmap developer file information..
pandeyarush01
 
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
Taqyea
 
Digital Disruption Use Case_Music Industry_for students.pdf
Digital Disruption Use Case_Music Industry_for students.pdfDigital Disruption Use Case_Music Industry_for students.pdf
Digital Disruption Use Case_Music Industry_for students.pdf
ProsenjitMitra9
 
presentacion.slideshare.informáticaJuridica..pptx
presentacion.slideshare.informáticaJuridica..pptxpresentacion.slideshare.informáticaJuridica..pptx
presentacion.slideshare.informáticaJuridica..pptx
GersonVillatoro4
 
Dynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics DynamicsDynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics Dynamics
heyoubro69
 
Introduction to MedDRA hgjuyh mnhvnj mbv hvj jhgjgjgjg
Introduction to MedDRA hgjuyh mnhvnj mbv hvj jhgjgjgjgIntroduction to MedDRA hgjuyh mnhvnj mbv hvj jhgjgjgjg
Introduction to MedDRA hgjuyh mnhvnj mbv hvj jhgjgjgjg
MichaelTuffourAmirik
 
Peeling the onion: How to move through multiple discovery and analysis cycles
Peeling the onion: How to move through multiple discovery and analysis cyclesPeeling the onion: How to move through multiple discovery and analysis cycles
Peeling the onion: How to move through multiple discovery and analysis cycles
Process mining Evangelist
 
From Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
From Data to Insight: How News Aggregator APIs Deliver Contextual IntelligenceFrom Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
From Data to Insight: How News Aggregator APIs Deliver Contextual Intelligence
Contify
 
End to End Process Analysis - Cox Communications
End to End Process Analysis - Cox CommunicationsEnd to End Process Analysis - Cox Communications
End to End Process Analysis - Cox Communications
Process mining Evangelist
 
Feature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record SystemsFeature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record Systems
Process mining Evangelist
 
MLOps_with_SageMaker_Template_EN idioma inglés
MLOps_with_SageMaker_Template_EN idioma inglésMLOps_with_SageMaker_Template_EN idioma inglés
MLOps_with_SageMaker_Template_EN idioma inglés
FabianPierrePeaJacob
 
PN_Junction_Diode_Typdbhghfned_Notes.pdf
PN_Junction_Diode_Typdbhghfned_Notes.pdfPN_Junction_Diode_Typdbhghfned_Notes.pdf
PN_Junction_Diode_Typdbhghfned_Notes.pdf
AryanGohil1
 
Unit 2 - Unified Modeling Language (UML).pdf
Unit 2 - Unified Modeling Language (UML).pdfUnit 2 - Unified Modeling Language (UML).pdf
Unit 2 - Unified Modeling Language (UML).pdf
sixokak391
 
Mixed Methods Research.pptx education 201
Mixed Methods Research.pptx education 201Mixed Methods Research.pptx education 201
Mixed Methods Research.pptx education 201
GraceSolaa1
 
The-Future-is-Now-Information-Technology-Trends.pptx.pdf
The-Future-is-Now-Information-Technology-Trends.pptx.pdfThe-Future-is-Now-Information-Technology-Trends.pptx.pdf
The-Future-is-Now-Information-Technology-Trends.pptx.pdf
winnt04
 
Language Learning App Data Research by Globibo [2025]
Language Learning App Data Research by Globibo [2025]Language Learning App Data Research by Globibo [2025]
Language Learning App Data Research by Globibo [2025]
globibo
 
Storage Devices and the Mechanism of Data Storage in Audio and Visual Form
Storage Devices and the Mechanism of Data Storage in Audio and Visual FormStorage Devices and the Mechanism of Data Storage in Audio and Visual Form
Storage Devices and the Mechanism of Data Storage in Audio and Visual Form
Professional Content Writing's
 
2-Cholera-Outbreaks-and-Waterborne-Pathogens-Typhoid-fever (1).pdf
2-Cholera-Outbreaks-and-Waterborne-Pathogens-Typhoid-fever (1).pdf2-Cholera-Outbreaks-and-Waterborne-Pathogens-Typhoid-fever (1).pdf
2-Cholera-Outbreaks-and-Waterborne-Pathogens-Typhoid-fever (1).pdf
AngelitaVergara1
 
web-roadmap developer file information..
web-roadmap developer file information..web-roadmap developer file information..
web-roadmap developer file information..
pandeyarush01
 
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
最新版澳洲西澳大利亚大学毕业证(UWA毕业证书)原版定制
Taqyea
 
Digital Disruption Use Case_Music Industry_for students.pdf
Digital Disruption Use Case_Music Industry_for students.pdfDigital Disruption Use Case_Music Industry_for students.pdf
Digital Disruption Use Case_Music Industry_for students.pdf
ProsenjitMitra9
 
presentacion.slideshare.informáticaJuridica..pptx
presentacion.slideshare.informáticaJuridica..pptxpresentacion.slideshare.informáticaJuridica..pptx
presentacion.slideshare.informáticaJuridica..pptx
GersonVillatoro4
 
Dynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics DynamicsDynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics Dynamics
heyoubro69
 
Ad

Why Tree is considered a non-linear data structure?

  • 3. In computer science, a tree is a widely-used data structure that emulates a hierarchical tree structure with a set of linked nodes.
  • 4. Nature View of a Tree branch e s leave s root
  • 6. What is a Tree ⚫ A tree is a finite nonempty set of elements. ⚫ It is an abstract model of a hierarchical structure. ⚫ consists of nodes with a parent-child relation. ⚫ Applications: ⚫ Organization charts ⚫ File systems ⚫ Programming environments Computers”R”Us Sale s R& D Manufacturin g Laptop s Desktop s US International Europ e Asi a Canad a
  • 8. Cont…  Root-The top most Node.  Children  Parents  Siblings- Have same parents.  Leaf- Has no Child. RELATION OF TREE 1 3 2 4 6 5 8 7 11 10 9 8
  • 9. Trees Data Structures ⚫Tree ⚫Nodes ⚫Each node can have 0 or more children ⚫A node can have at most one parent ⚫Binary tree ⚫Tree with 0–2 children per node Tre Binary Tree 9
  • 10. Tree s ⚫Terminology ⚫Root  no parent ⚫Leaf  no child ⚫Interior  non-leaf ⚫Height  distance from root to leaf Root node Leaf nodes Interior nodes Height 1
  • 12. Tree Terminology ⚫Path: Traversal from node to node along the edges results in a sequence called path. ⚫Root: Node at the top of the tree. ⚫Parent: Any node, except root has exactly one edge running upward to another node. The node above it is called parent. ⚫Child: Any node may have one or more lines running downward to other nodes. Nodes below are children. ⚫Leaf: A node that has no children. ⚫Subtree: Any node can be considered to be the root of a subtree, which consists of its children
  • 13. subtre e Tree Terminology ⚫ Root: node without parent (A) ⚫ Siblings: nodes share the same parent ⚫ Internal node: node with at least one child (A, B, C, F) ⚫ External node (leaf ): node without children (E, I, J, K, G, H, D) ⚫ Ancestors of a node: parent, grandparent, grand-grandparent, etc. ⚫ Descendant of a node: child, grandchild, grand-grandchild, etc. ⚫ Depth of a node: number of ancestors ⚫ Height of a tree: maximum depth of any node (3) ⚫ Degree of a node: the number of its children ⚫ Degree of a tree: the maximum number of its node. A B D C G H E F I J K Subtree: tree consisting of a node and its descendants
  • 14. General Trees ⚫Trees store data in a hierarchical manner ⚫ Root node is A ⚫ A's children are B, C, and D ⚫ E, F, and D are leaves ⚫ Length of path from A to E is 2 edges D A C F B E Trees have layers of nodes, where some are higher, others are lower.
  • 15. Logic of Tr Used to represent hierarchical data.  BS(CS) 1ST tree e.g. BS(CS)1st Section (A) Section (B) CR 1 CR2 group 1 group 2 group 3 group 4 group 1 group 2 group 3 group 4 1
  • 16. Logic of Tr Used to represent hierarchical data.  BS(CS) 1ST tree e.g. BS(CS)1st Section (A) Section (B) CR 1 CR2 group 1 group 2 group 3 group 4 group 1 group 2 group 3 group 4 1
  • 17. Cont…  A Collection of entities called Nodes.  Tree is a Non-Linear Data Structure.  It’s a hierarchica Structure. TREE Nodes 1
  • 18. Tree Traversal ⚫Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. ⚫Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes are connected via edges (links) we always start from the root (head) node.
  • 19. Cont… ⚫ That is, we cannot randomly access a node in a tree. There are three ways which we use to traverse a tree ⚫ (a) Inorder (Left, Root, Right) : 4 2 5 1 3 (b) Preorder (Root, Left, Right) : 1 2 4 5 3 (c) Postorder (Left, Right, Root) : 4 5 2 3 1 ⚫
  • 20. In-order Traversal ⚫In this traversal method, the left subtree is visited first, then the root and later the right sub- tree. We should always remember that every node may represent a subtree itself.
  • 22. Inorder (Left, Root, Right) ⚫We start from A, and following in-order traversal, we move to its left subtree B. B is also traversed in-order. The process goes on until all the nodes are visited. The output of inorder traversal of this tree will be − ⚫D → B → E → A → F → C → G
  • 23. Inorder Algorithm ⚫Until all nodes are traversed − ⚫ Step 1 − Recursively traverse left subtree. ⚫ Step 2 − Visit root node. ⚫Step 3 − Recursively traverse right subtree.
  • 24. Pre-order Traversal ⚫In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.
  • 26. Preorder (Root, Left, Right) ⚫ We start from A, and following pre-order traversal, we first visit A itself and then move to its left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The output of pre- order traversal of this tree will be. ⚫ A → B → D → E → C → F → G
  • 27. Algorithm ⚫Until all nodes are traversed − ⚫Step 1 − Visit root node. ⚫Step 2 − Recursively traverse left subtree. ⚫ Step 3 − Recursively traverse right subtree.
  • 28. Post-order Traversal ⚫In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node.
  • 30. Postorder (Left, Right, Root) ⚫ We start from A, and following pre-order traversal, we first visit the left subtree B. B is also traversed post-order. The process goes on until all the nodes are visited. The output of post-order traversal of this tree will be. ⚫ D → E → B → F → G → C → A
  • 31. Algorithm ⚫Until all nodes are traversed ⚫ Step 1 − Recursively traverse left subtree. ⚫ Step 2 − Recursively traverse right subtree. ⚫ Step 3 − Visit root node.
  • 33. • If root is NULL • then create root node • return • If root exists then • compare the data with node.data • while until insertion position is located • If data is greater than node.data • goto right subtree • else • goto left subtree • endwhile • insert data
  • 34. If root.data is equal to search.data return root else while data not found If data is greater than node.data goto right subtree else goto left subtree If data found return node endwhile return data not found end if
  • 37. There are a large number of tree types, however three types specifically are used the majority of the time (both in real world development and in Computer Science courses). The types are:  Binary trees  B-Trees  Heaps
  • 38. Binary trees ⚫Binary tree follows the rule that each parent node has no more than 2 child nodes. This means that a binary tree can look like this: ⚫ OR
  • 39. Strict Binary Tree ⚫Strict Binary Tree is a Binary tree in which root can have exactly two children or no children at all. That means, there can be 0 or 2 children of any node.
  • 40. Complete Binary Tree ⚫Complete Binary Tree is a Strict Binary tree in which every leaf node is at same level. That means, there are equal number of children in right and left subtree for every node.
  • 41. Extended Binary Tree ⚫An extended binary tree is a transformation of any binary tree into a complete binary tree. ⚫This transformation consists of replacing every null subtree of the original tree with “special nodes.” ⚫The nodes from the original tree are then called as internal nodes, while the “special nodes” are called as external nodes.
  • 42. Extended Binary Tree A binary tree with special nodes replacing every null subtree. Every regular node has two children, and every special node has no children.
  • 43. Binary Search Tree ⚫ The value at any node, • Greater than every value in left subtree. Smaller than every value in right subtree – Example ⚫Y > X ⚫Y < Z ⚫ Values in left sub tree less than parent ⚫ Values in right sub tree greater than parent ⚫ Fast searches in a Binary Search tree, maximum of log n comparisons.
  • 45. B - Trees ⚫B – Tree is another type of search tree called B- Tree in which a node can store more than one value (key) and it can have more than two children. ⚫B-Tree can be defined as follows… ⚫ B-Tree is a self-balanced search tree with multiple keys in every node and more than two children for every node.
  • 46. Operations on a B-Tree ⚫The following operations are performed on a B- Tree... ⚫Search ⚫Insertion ⚫Deletion
  • 47. Heap Tree ⚫Heap is a special case of balanced binary tree data structure where the root-node key is compared with its children and arranged accordingly. If α has child node β then ⚫key(α) ≥ key(β) ⚫A heap can be of two types ⚫ Min-Heap ⚫ Max-Heap
  • 48. Max-Heap ⚫Max-Heap − Where the value of the root node is greater than or equal to either of its children.
  • 51. Max Heap Construction Algorithm ⚫Step 1 − Create a new node. ⚫ Step 2 − Assign new value to the node. ⚫Step 3 − Compare the value of this child node with its parent. ⚫Step 4 If − value of parent is less than child, then swap them. ⚫Step 5 − Repeat step 3 & 4 until Heap property holds.
  • 52. Max Heap Deletion Algorithm ⚫Step 1 − Remove root node. ⚫Step 2 − Move the last element of last level to root. ⚫Step 3 − Compare the value of this child node with its parent. ⚫Step 4 If − value of parent is less than child, then swap them. ⚫Step 5 − Repeat step 3 & 4 until Heap property holds.
  • 53. Min-Heap ⚫Min-Heap − Where the value of the root node is less than or equal to either of its children.
  翻译: