SlideShare una empresa de Scribd logo
1 de 13
Descargar para leer sin conexión
Trees
Tree traversal
By : Dana Dia’a
Data structure and algorithms
Tree (ADT)
 What is a tree ?
 A set of nodes.
 Each node can have one or more children .
 Each node can have at most one parent.
Root
( no parent)
Leaves
( no child)
Node level
1
2
3
4
Binary tree
 In binary tree , each node has at most two children .
 In other word , it can have zero children ( empty ) , one or
two children but not more than two children .
 Examples of a binary trees :
binary search tree ( BSTree)
 Every node entry has a unique key ( keys are not the
same in the tree ) .
 All the keys in the left of a node are less than the key of
a node
 All the keys in the right of a node are greater than the
key of a node .
 Example of a BStree :
Left
Less than 8
Right
Greater than 8
A binary tree but NOT binary search
tree
Search a node
 A binary search tree provide an excellent structure for
searching a node .
 For example : find the node with value 6 .
 We compare: 6 is <or > 8( the root) ?
 6 is less than 8 so we go to the left of node .
 Then we compare : 6 is <or > 3 ?
 6 is greater than 3 so we go right to
The right of node .
 6 is found .
Tree traversal
 The process of visiting all nodes in a tree only
one time .
 Two classes :
 1 . Breadth – first traversal .
 2 . Depth –first traversal .
Breadth – first traversal
 We can go with two methods :
1. Visiting each node starting from the lowest level (
root ) and moving down level by level , visiting
each node from left to right . ( top – down – from
left – to right ) .
Output : 8 , 3 , 10 ,1 ,6 ,14 ,4 ,7
,13
Starts
Breadth – first traversal (cont.)
 The other method is :
1. Visiting each node starting from the highest level (
leaves ) and moving up level by level , visiting each
node from right to left . ( bottom – up – from right –
to left ) .
Starts
Output : 13 , 7 ,4 , 14 ,6 ,1 ,10 ,3 ,8
Depth – first traversal
 We have to consider three tasks in this type .
 V – visiting a node .
 L – traversing the left sub-tree if any .
 R – traversing the right sub-tree if any .
 There are six types of depth –first traversal , but
we will consider three of them :
 VLR ( Preorder )
 LVR ( Inorder )
 LRV ( Postorder)
VLR ( Preorder )
VLR
VLR VLR
VLRVLR VLR
VLR
VLR
VLR
Output : 8 ,3 ,1 ,6 ,4 ,7 ,10 ,14 ,13
Assuming that the
visit(v) is to output
the value
LVR ( Ineorder )
LVR
Output : 1 , 3 , 4 , 6 , 7 , 8 ,10 ,13 ,14
Assuming that the
visit() is to output
the valueLVR
LVR
LVR
LVR
LVR
LVR
LVRLVR
LRV ( Posteorder )
LRV
Output : 1 , 4 , 7 ,6 ,3 , 13 , 14 , 10 , 8
Assuming that the
visit() is to output
the value
LRV
LRV LRV
LRV
LRV
LRVLRVLRV

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Expression trees
Expression treesExpression trees
Expression trees
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Binary tree
Binary tree Binary tree
Binary tree
 
Trees
TreesTrees
Trees
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Trees
TreesTrees
Trees
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Red black tree
Red black treeRed black tree
Red black tree
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
 

Destacado

computer notes - Traversal of a binary tree
computer notes - Traversal of a binary treecomputer notes - Traversal of a binary tree
computer notes - Traversal of a binary treeecomputernotes
 
Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)raj upadhyay
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method Shantanu Mishra
 
Deloitte uk-cpo-survey-2014
Deloitte uk-cpo-survey-2014Deloitte uk-cpo-survey-2014
Deloitte uk-cpo-survey-2014Clive Gallery
 
Phrasal verbs vaughan
Phrasal verbs  vaughanPhrasal verbs  vaughan
Phrasal verbs vaughanlola macias
 
Selectivitat writing guide2
Selectivitat writing guide2Selectivitat writing guide2
Selectivitat writing guide2lola macias
 
KEEP IT SAFE FINAL IT Security A6 FINAL 2 (1)
KEEP IT SAFE FINAL IT Security A6 FINAL 2 (1)KEEP IT SAFE FINAL IT Security A6 FINAL 2 (1)
KEEP IT SAFE FINAL IT Security A6 FINAL 2 (1)Jenine Sanchez
 
ми всі майбутні таланти
ми всі майбутні талантими всі майбутні таланти
ми всі майбутні талантиJohn_Fikus
 
Fact Sheet_FederalAgency-Aug2016-FINAL
Fact Sheet_FederalAgency-Aug2016-FINALFact Sheet_FederalAgency-Aug2016-FINAL
Fact Sheet_FederalAgency-Aug2016-FINALAnn Warner
 
Khodiyar ceramics (i) pvt ltd corporate presentation
Khodiyar ceramics (i) pvt ltd corporate presentationKhodiyar ceramics (i) pvt ltd corporate presentation
Khodiyar ceramics (i) pvt ltd corporate presentationNakul Bhut
 
Question 1 x2
Question 1 x2Question 1 x2
Question 1 x2Kirsty98
 

Destacado (19)

Unit8 C
Unit8 CUnit8 C
Unit8 C
 
Tree
TreeTree
Tree
 
computer notes - Traversal of a binary tree
computer notes - Traversal of a binary treecomputer notes - Traversal of a binary tree
computer notes - Traversal of a binary tree
 
Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Binary Search Algorithm
Binary Search Algorithm Binary Search Algorithm
Binary Search Algorithm
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Deloitte uk-cpo-survey-2014
Deloitte uk-cpo-survey-2014Deloitte uk-cpo-survey-2014
Deloitte uk-cpo-survey-2014
 
Phrasal verbs vaughan
Phrasal verbs  vaughanPhrasal verbs  vaughan
Phrasal verbs vaughan
 
Selectivitat writing guide2
Selectivitat writing guide2Selectivitat writing guide2
Selectivitat writing guide2
 
KEEP IT SAFE FINAL IT Security A6 FINAL 2 (1)
KEEP IT SAFE FINAL IT Security A6 FINAL 2 (1)KEEP IT SAFE FINAL IT Security A6 FINAL 2 (1)
KEEP IT SAFE FINAL IT Security A6 FINAL 2 (1)
 
Proposal
ProposalProposal
Proposal
 
ми всі майбутні таланти
ми всі майбутні талантими всі майбутні таланти
ми всі майбутні таланти
 
Fact Sheet_FederalAgency-Aug2016-FINAL
Fact Sheet_FederalAgency-Aug2016-FINALFact Sheet_FederalAgency-Aug2016-FINAL
Fact Sheet_FederalAgency-Aug2016-FINAL
 
Khodiyar ceramics (i) pvt ltd corporate presentation
Khodiyar ceramics (i) pvt ltd corporate presentationKhodiyar ceramics (i) pvt ltd corporate presentation
Khodiyar ceramics (i) pvt ltd corporate presentation
 
Question 1 x2
Question 1 x2Question 1 x2
Question 1 x2
 

Similar a Tree data structure

Similar a Tree data structure (20)

Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Tree data structure.pptx
Tree data structure.pptxTree data structure.pptx
Tree data structure.pptx
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
4a searching-more
4a searching-more4a searching-more
4a searching-more
 
Binary trees
Binary treesBinary trees
Binary trees
 
Tree data structure
Tree data structureTree data structure
Tree data structure
 
Trees ayaz
Trees ayazTrees ayaz
Trees ayaz
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
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
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Trees
TreesTrees
Trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Tree
TreeTree
Tree
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
 

Más de Dana dia

types of computer cases
types of computer cases types of computer cases
types of computer cases Dana dia
 
summarize system analysis and design ch2
summarize system analysis and design ch2 summarize system analysis and design ch2
summarize system analysis and design ch2 Dana dia
 
system analysis and design chapter 1 Kendall & Kendall
system analysis and design chapter 1 Kendall & Kendallsystem analysis and design chapter 1 Kendall & Kendall
system analysis and design chapter 1 Kendall & KendallDana dia
 
Os examples scheduling
Os examples schedulingOs examples scheduling
Os examples schedulingDana dia
 
Multicore system
Multicore systemMulticore system
Multicore systemDana dia
 
Multimedia
MultimediaMultimedia
MultimediaDana dia
 

Más de Dana dia (6)

types of computer cases
types of computer cases types of computer cases
types of computer cases
 
summarize system analysis and design ch2
summarize system analysis and design ch2 summarize system analysis and design ch2
summarize system analysis and design ch2
 
system analysis and design chapter 1 Kendall & Kendall
system analysis and design chapter 1 Kendall & Kendallsystem analysis and design chapter 1 Kendall & Kendall
system analysis and design chapter 1 Kendall & Kendall
 
Os examples scheduling
Os examples schedulingOs examples scheduling
Os examples scheduling
 
Multicore system
Multicore systemMulticore system
Multicore system
 
Multimedia
MultimediaMultimedia
Multimedia
 

Último

ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Osopher
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...HetalPathak10
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxryandux83rd
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 

Último (20)

ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
CARNAVAL COM MAGIA E EUFORIA _
CARNAVAL COM MAGIA E EUFORIA            _CARNAVAL COM MAGIA E EUFORIA            _
CARNAVAL COM MAGIA E EUFORIA _
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptx
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 

Tree data structure

  • 1. Trees Tree traversal By : Dana Dia’a Data structure and algorithms
  • 2. Tree (ADT)  What is a tree ?  A set of nodes.  Each node can have one or more children .  Each node can have at most one parent. Root ( no parent) Leaves ( no child) Node level 1 2 3 4
  • 3. Binary tree  In binary tree , each node has at most two children .  In other word , it can have zero children ( empty ) , one or two children but not more than two children .  Examples of a binary trees :
  • 4. binary search tree ( BSTree)  Every node entry has a unique key ( keys are not the same in the tree ) .  All the keys in the left of a node are less than the key of a node  All the keys in the right of a node are greater than the key of a node .  Example of a BStree : Left Less than 8 Right Greater than 8
  • 5. A binary tree but NOT binary search tree
  • 6. Search a node  A binary search tree provide an excellent structure for searching a node .  For example : find the node with value 6 .  We compare: 6 is <or > 8( the root) ?  6 is less than 8 so we go to the left of node .  Then we compare : 6 is <or > 3 ?  6 is greater than 3 so we go right to The right of node .  6 is found .
  • 7. Tree traversal  The process of visiting all nodes in a tree only one time .  Two classes :  1 . Breadth – first traversal .  2 . Depth –first traversal .
  • 8. Breadth – first traversal  We can go with two methods : 1. Visiting each node starting from the lowest level ( root ) and moving down level by level , visiting each node from left to right . ( top – down – from left – to right ) . Output : 8 , 3 , 10 ,1 ,6 ,14 ,4 ,7 ,13 Starts
  • 9. Breadth – first traversal (cont.)  The other method is : 1. Visiting each node starting from the highest level ( leaves ) and moving up level by level , visiting each node from right to left . ( bottom – up – from right – to left ) . Starts Output : 13 , 7 ,4 , 14 ,6 ,1 ,10 ,3 ,8
  • 10. Depth – first traversal  We have to consider three tasks in this type .  V – visiting a node .  L – traversing the left sub-tree if any .  R – traversing the right sub-tree if any .  There are six types of depth –first traversal , but we will consider three of them :  VLR ( Preorder )  LVR ( Inorder )  LRV ( Postorder)
  • 11. VLR ( Preorder ) VLR VLR VLR VLRVLR VLR VLR VLR VLR Output : 8 ,3 ,1 ,6 ,4 ,7 ,10 ,14 ,13 Assuming that the visit(v) is to output the value
  • 12. LVR ( Ineorder ) LVR Output : 1 , 3 , 4 , 6 , 7 , 8 ,10 ,13 ,14 Assuming that the visit() is to output the valueLVR LVR LVR LVR LVR LVR LVRLVR
  • 13. LRV ( Posteorder ) LRV Output : 1 , 4 , 7 ,6 ,3 , 13 , 14 , 10 , 8 Assuming that the visit() is to output the value LRV LRV LRV LRV LRV LRVLRVLRV