SlideShare a Scribd company logo
Data Structures and Algorithms
Trees - II
Outline
• Binary Tree Revision
• Traversing a Binary Tree
– Depth-first
• Pre-order
• In-order
• Post-order
– Breadth-first
Tree Traversal
• A traversal iterates through a collection, one item at a time, in
order to access or visit each item.
• With a linear structure such as a linked list, the traversal is
rather easy since we can start with the first node and iterate
through the nodes, one at a time, by following the links
between the nodes.
• But how do we visit every node in a binary tree?
Linked Implementation
Pre-order Traversal
Since every node is the
root of its own subtree,
we can repeat the same
process on each node,
resulting in a
recursive solution.
(Also called NLR traversal)
In-order Traversal
(Also called LNR traversal)
Post-Order Traversal
(Also called LRN traversal)
Depth-First and Breadth-First Traversal
• The preorder, inorder, and postorder traversals are
all examples of a depth-first traversal.
– That is, the nodes are traversed deeper in the tree
before returning to higher-level nodes.
• Another type of traversal that can be performed on
a binary tree is the level order traversal which is
known as breadth-first traversal.
– That is, the nodes are visited by level, from left to right.
Level-Order Traversal
Expression Trees
• Arithmetic expressions such as (9+3)*(8 4) can be
represented using an expression tree.
• An expression tree is a binary tree in which the
operators are stored in the interior nodes and the
operands (the variables or constant values) are
stored in the leaves.
• Once constructed, an expression tree can be used
to evaluate the expression or for converting an
infix expression to either pre-fix or post-fix
notation.
Expression Tree (Cont.)
• The structure of the expression tree is based on the
order in which the operators are evaluated.
• The operator in each internal node is evaluated after
both its left and right subtrees have been evaluated.
• The root node contains the operator to be evaluated.
+*ab/cd
Exercise
• Write the expression obtained after traversing
the following tree via Pre-order Traversal
a+b+c+d
Exercise
• Write the expression obtained after traversing
the following tree via in-order Traversal
ab+cd*/
Exercise
• Write the expression obtained after traversing
the following tree via post-order Traversal
/
+
d
c
b
a
*
*
+
9
/
6
5
Practice Question
Write the Preorder, Inorder and
Postorder traversals of the given
binary tree.
Also write the level order
traversal.
-
2
4
References
• Data Structures and Algorithms Using Python, Rance D.
Necaise, John Wiley & Sons, Inc. 2010
• John R. Hubbard, Data Structures with Java (Schaum's Outline
Series), McGraw-Hill Education; 2nd edition (June 16, 2009)

More Related Content

Similar to Lecture 22_Trees - II.pptx

Faster and smaller inverted indices with Treaps Research Paper
Faster and smaller inverted indices with Treaps Research PaperFaster and smaller inverted indices with Treaps Research Paper
Faster and smaller inverted indices with Treaps Research Paper
sameiralk
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
LavanyaJ28
 
Editors l21 l24
Editors l21 l24Editors l21 l24
Editors l21 l24
Neha Pachauri
 
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
 
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
 
chapter 6.1.pptx
chapter 6.1.pptxchapter 6.1.pptx
chapter 6.1.pptx
Tekle12
 
Data Handling and Function
Data Handling and FunctionData Handling and Function
Data Handling and Function
RatnaJava
 
First Class Variables as AST Annotations
 First Class Variables as AST Annotations First Class Variables as AST Annotations
First Class Variables as AST Annotations
ESUG
 
First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST Annotations
Marcus Denker
 
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
 
Top tree
Top treeTop tree
Top tree
ParidVaroshi1
 
Wrapper induction construct wrappers automatically to extract information f...
Wrapper induction   construct wrappers automatically to extract information f...Wrapper induction   construct wrappers automatically to extract information f...
Wrapper induction construct wrappers automatically to extract information f...
George Ang
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
sherrilsiddhardh
 
linked list in c++
linked list in c++linked list in c++
linked list in c++
YaminiLakshmi Meduri
 
Trees
TreesTrees
Standard template library
Standard template libraryStandard template library
Standard template library
Sukriti Singh
 
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queuesFallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
SnehilKeshari
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
Muhazzab Chouhadry
 
Supporting Arrays and Allocatables in LFortran
Supporting Arrays and Allocatables in LFortranSupporting Arrays and Allocatables in LFortran
Supporting Arrays and Allocatables in LFortran
Gagandeep Singh
 
presentation.pdf bnksjqwjkwlsjoilhsnwososh
presentation.pdf bnksjqwjkwlsjoilhsnwososhpresentation.pdf bnksjqwjkwlsjoilhsnwososh
presentation.pdf bnksjqwjkwlsjoilhsnwososh
ShivaCharan54
 

Similar to Lecture 22_Trees - II.pptx (20)

Faster and smaller inverted indices with Treaps Research Paper
Faster and smaller inverted indices with Treaps Research PaperFaster and smaller inverted indices with Treaps Research Paper
Faster and smaller inverted indices with Treaps Research Paper
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
Editors l21 l24
Editors l21 l24Editors l21 l24
Editors l21 l24
 
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
 
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
 
chapter 6.1.pptx
chapter 6.1.pptxchapter 6.1.pptx
chapter 6.1.pptx
 
Data Handling and Function
Data Handling and FunctionData Handling and Function
Data Handling and Function
 
First Class Variables as AST Annotations
 First Class Variables as AST Annotations First Class Variables as AST Annotations
First Class Variables as AST Annotations
 
First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST Annotations
 
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
 
Top tree
Top treeTop tree
Top tree
 
Wrapper induction construct wrappers automatically to extract information f...
Wrapper induction   construct wrappers automatically to extract information f...Wrapper induction   construct wrappers automatically to extract information f...
Wrapper induction construct wrappers automatically to extract information f...
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
linked list in c++
linked list in c++linked list in c++
linked list in c++
 
Trees
TreesTrees
Trees
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queuesFallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Supporting Arrays and Allocatables in LFortran
Supporting Arrays and Allocatables in LFortranSupporting Arrays and Allocatables in LFortran
Supporting Arrays and Allocatables in LFortran
 
presentation.pdf bnksjqwjkwlsjoilhsnwososh
presentation.pdf bnksjqwjkwlsjoilhsnwososhpresentation.pdf bnksjqwjkwlsjoilhsnwososh
presentation.pdf bnksjqwjkwlsjoilhsnwososh
 

Recently uploaded

一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
nuttdpt
 
一比一原版(Sheffield毕业证书)谢菲尔德大学毕业证如何办理
一比一原版(Sheffield毕业证书)谢菲尔德大学毕业证如何办理一比一原版(Sheffield毕业证书)谢菲尔德大学毕业证如何办理
一比一原版(Sheffield毕业证书)谢菲尔德大学毕业证如何办理
1tyxnjpia
 
社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .
NABLAS株式会社
 
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
taqyea
 
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
z6osjkqvd
 
Jio cinema Retention & Engagement Strategy.pdf
Jio cinema Retention & Engagement Strategy.pdfJio cinema Retention & Engagement Strategy.pdf
Jio cinema Retention & Engagement Strategy.pdf
inaya7568
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
nyfuhyz
 
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
lzdvtmy8
 
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
aguty
 
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
mkkikqvo
 
Sample Devops SRE Product Companies .pdf
Sample Devops SRE  Product Companies .pdfSample Devops SRE  Product Companies .pdf
Sample Devops SRE Product Companies .pdf
Vineet
 
Cell The Unit of Life for NEET Multiple Choice Questions.docx
Cell The Unit of Life for NEET Multiple Choice Questions.docxCell The Unit of Life for NEET Multiple Choice Questions.docx
Cell The Unit of Life for NEET Multiple Choice Questions.docx
vasanthatpuram
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Aggregage
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
VyNguyen709676
 
University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
soxrziqu
 
一比一原版(lbs毕业证书)伦敦商学院毕业证如何办理
一比一原版(lbs毕业证书)伦敦商学院毕业证如何办理一比一原版(lbs毕业证书)伦敦商学院毕业证如何办理
一比一原版(lbs毕业证书)伦敦商学院毕业证如何办理
ywqeos
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
Sachin Paul
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
Timothy Spann
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
AlessioFois2
 
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
nyvan3
 

Recently uploaded (20)

一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
 
一比一原版(Sheffield毕业证书)谢菲尔德大学毕业证如何办理
一比一原版(Sheffield毕业证书)谢菲尔德大学毕业证如何办理一比一原版(Sheffield毕业证书)谢菲尔德大学毕业证如何办理
一比一原版(Sheffield毕业证书)谢菲尔德大学毕业证如何办理
 
社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .
 
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
 
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
 
Jio cinema Retention & Engagement Strategy.pdf
Jio cinema Retention & Engagement Strategy.pdfJio cinema Retention & Engagement Strategy.pdf
Jio cinema Retention & Engagement Strategy.pdf
 
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
一比一原版(UMN文凭证书)明尼苏达大学毕业证如何办理
 
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
一比一原版格里菲斯大学毕业证(Griffith毕业证书)学历如何办理
 
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
一比一原版澳洲西澳大学毕业证(uwa毕业证书)如何办理
 
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
 
Sample Devops SRE Product Companies .pdf
Sample Devops SRE  Product Companies .pdfSample Devops SRE  Product Companies .pdf
Sample Devops SRE Product Companies .pdf
 
Cell The Unit of Life for NEET Multiple Choice Questions.docx
Cell The Unit of Life for NEET Multiple Choice Questions.docxCell The Unit of Life for NEET Multiple Choice Questions.docx
Cell The Unit of Life for NEET Multiple Choice Questions.docx
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
 
University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
 
一比一原版(lbs毕业证书)伦敦商学院毕业证如何办理
一比一原版(lbs毕业证书)伦敦商学院毕业证如何办理一比一原版(lbs毕业证书)伦敦商学院毕业证如何办理
一比一原版(lbs毕业证书)伦敦商学院毕业证如何办理
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
 
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
一比一原版英国赫特福德大学毕业证(hertfordshire毕业证书)如何办理
 

Lecture 22_Trees - II.pptx

  • 1. Data Structures and Algorithms Trees - II
  • 2. Outline • Binary Tree Revision • Traversing a Binary Tree – Depth-first • Pre-order • In-order • Post-order – Breadth-first
  • 3. Tree Traversal • A traversal iterates through a collection, one item at a time, in order to access or visit each item. • With a linear structure such as a linked list, the traversal is rather easy since we can start with the first node and iterate through the nodes, one at a time, by following the links between the nodes. • But how do we visit every node in a binary tree?
  • 5. Pre-order Traversal Since every node is the root of its own subtree, we can repeat the same process on each node, resulting in a recursive solution. (Also called NLR traversal)
  • 8. Depth-First and Breadth-First Traversal • The preorder, inorder, and postorder traversals are all examples of a depth-first traversal. – That is, the nodes are traversed deeper in the tree before returning to higher-level nodes. • Another type of traversal that can be performed on a binary tree is the level order traversal which is known as breadth-first traversal. – That is, the nodes are visited by level, from left to right.
  • 10. Expression Trees • Arithmetic expressions such as (9+3)*(8 4) can be represented using an expression tree. • An expression tree is a binary tree in which the operators are stored in the interior nodes and the operands (the variables or constant values) are stored in the leaves. • Once constructed, an expression tree can be used to evaluate the expression or for converting an infix expression to either pre-fix or post-fix notation.
  • 11. Expression Tree (Cont.) • The structure of the expression tree is based on the order in which the operators are evaluated. • The operator in each internal node is evaluated after both its left and right subtrees have been evaluated. • The root node contains the operator to be evaluated.
  • 12. +*ab/cd Exercise • Write the expression obtained after traversing the following tree via Pre-order Traversal
  • 13. a+b+c+d Exercise • Write the expression obtained after traversing the following tree via in-order Traversal
  • 14. ab+cd*/ Exercise • Write the expression obtained after traversing the following tree via post-order Traversal / + d c b a *
  • 15. * + 9 / 6 5 Practice Question Write the Preorder, Inorder and Postorder traversals of the given binary tree. Also write the level order traversal. - 2 4
  • 16. References • Data Structures and Algorithms Using Python, Rance D. Necaise, John Wiley & Sons, Inc. 2010 • John R. Hubbard, Data Structures with Java (Schaum's Outline Series), McGraw-Hill Education; 2nd edition (June 16, 2009)