SlideShare a Scribd company logo
1 of 31
P. Raveena Lakshmi, M.Sc.,M.Phil.,
Assistant Professor, V.V.Vanniaperumal College For Women
Tree Definition
 Tree: a set of elements that either
 it is empty
 or, it has a distinguished element called
the root and zero or more trees (called
subtrees of the root)
 What kind of definition is this?
 What is the base case?
 What is the recursive part?
Tree Definition
Subtrees of
the root
Root
Tree Terminology
 Nodes: the elements in the tree
 Edges: connections between nodes
 Root: the distinguished element that is the
origin of the tree
 There is only one root node in a tree
 Empty tree has no nodes and no edges
Tree Terminology
node or
vertex
Rootedge
arc, or
link
 Parent or predecessor: the node directly above
another node in the hierarchy
 A node can have only one parent
 Child or successor: a node directly below
another node in the hierarchy
 Siblings: nodes that have the same parent
 Ancestors of a node: its parent, the parent of its
parent, etc.
 Descendants of a node: its children, the children
of its children, etc.
Tree Terminology
Tree Terminology
 Leaf node: a node without children
 Internal node: a node that is not a leaf node
Tree Terminology
Leaf
nodes or
external
nodes
Root
Interior or
internal
nodes
Discussion
 Does a leaf node have any children?
 Does the root node have a parent?
 How many parents does every node
other than the root node have?
Height of a Tree
 A path is a sequence of edges leading
from one node to another
 Length of a path: number of edges on
the path
 Height of a (non-empty) tree : length of
the longest path from the root to a leaf
 What is the height of a tree that has only a
root node?
 By convention, the height of an empty tree
is -1
Tree Terminology
Root
A
B
F H
E
J
C D
I
L M N
G
K
Height = 3
Level of a Node
 Level of a node: number of edges
between root and the node
 It can be defined recursively:
 Level of root node is 0
 Level of a node that is not the root node is
level of its parent + 1
 Question: What is the level of a node in
terms of path length?
 Question: What is the height of a tree in
terms of levels?
Level of a Node
Level 0
Level 1
Level 2
Level 3
Subtrees
 Subtree of a node: consists of a child
node and all its descendants
 A subtree is itself a tree
 A node may have many subtrees
Subtrees
Subtrees of the
node labeled E
E
More Tree Terminology
 Degree or arity of a node: the number of
children it has
 Degree or arity of a tree: the maximum of
the degrees of the tree’s nodes
Degree
A
B
F H
E
J
C D
I
L M
G
K
4
2 1 1 3
1
0
0 1 0 1
0 0
Degree of a tree: the maximum degree of its nodes
Binary Trees
 General tree: a tree each of whose nodes
may have any number of children
 n-ary tree: a tree each of whose nodes
may have no more than n children
 Binary tree: a tree each of whose nodes
may have no more than 2 children
 i.e. a binary tree is a tree with degree (arity) 2
 The children (if present) are called the
left child and right child
 Recursive definition of a binary tree:
it is
 The empty tree
 Or, a tree which has a root whose left and right
subtrees are binary trees
 A binary tree is a positional tree, i.e. it
matters whether the subtree is left or right
Binary Trees
Binary Tree
A
IH
D E
B
F
C
G
Tree Traversals
 A traversal of a tree requires that each
node of the tree be visited once
 Example: a typical reason to traverse a tree is
to display the data stored at each node of the
tree
 Standard traversal orderings:
 preorder
 inorder
 postorder
 level-order
Traversals
We’ll trace the different traversals using this tree; recursive calls,
returns, and “visits” will be numbered in the order they occur
A
IH
D E
B
F
C
G
Preorder Traversal
 Start at the root
 Visit each node, followed by its children; we will
choose to visit left child before right
 Recursive algorithm for preorder traversal:
 If tree is not empty,
○ Visit root node of tree
○ Perform preorder traversal of its left subtree
○ Perform preorder traversal of its right subtree
 What is the base case?
 What is the recursive part?
Preorder Traversal
public void preorder (BinaryTreeNode<T> r) {
if (r != null) {
visit(r);
preorder (r.getLeftChild());
preorder (r.getRightChild());
}
}
Preorder Traversal
1: visit A
29: visit I9: visit H
5: visit D 17: visit E
3: visit B
27: visit F
25: visit C
39:
visit G
Nodes are visited in the order ABDHECFIG
. .
.
.. .
. ..
6
4
2
11
10
8
7
16
15
14
13
12
21
20
19
18 28
26
24
23
22
34
33
3231
30
38
37
45
44
43
42
41
40
35
36
Inorder Traversal
 Start at the root
 Visit the left child of each node, then the node,
then any remaining nodes
 Recursive algorithm for inorder traversal
 If tree is not empty,
○ Perform inorder traversal of left subtree of root
○ Visit root node of tree
○ Perform inorder traversal of its right subtree
Inorder Traversal
public void inorder (BinaryTreeNode<T> r) {
if (r != null) {
inorder (r.getLeftChild());
visit(r);
inorder (r.getRightChild());
}
}
Inorder Traversal
23: visit A
29: visit I9: visit H
5: visit D 18: visit E
14: visit B
33: visit F
37: visit C
41:
visit G
Nodes are visited in the order DHBEAIFCG
. .
.
.. .
. ..
3
2
1
8
7
6
4
15
13
12
11
10
20
19
17
16 26
25
24
22
21
32
31
3028
27
38
36
45
44
43
42
40
39
34
35
Postorder Traversal
 Start at the root
 Visit the children of each node, then the node
 Recursive algorithm for postorder traversal
 If tree is not empty,
○ Perform postorder traversal of left subtree of root
○ Perform postorder traversal of right subtree of root
○ Visit root node of tree
Postorder Traversal
public void postorder (BinaryTreeNode<T> r) {
if (r != null) {
postorder (r.getLeftChild());
postorder (r.getRightChild());
visit(r);
}
}
Postorder Traversal
45: visit A
30: visit I10: visit H
12: visit D 19: visit E
21: visit B
34: visit F
43: visit C
41:
visit G
Nodes are visited in the order HDEBIFGCA
. .
.
.. .
. ..
3
2
1
7
6
5
4
14
13
11
9
8
18
17
16
15 25
24
23
22
20
31
29
2827
26
36
35
44
42
40
39
38
37
32
33

More Related Content

What's hot (20)

Stacks
StacksStacks
Stacks
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
 
Unit 3 Tree chapter 5
Unit 3  Tree chapter 5Unit 3  Tree chapter 5
Unit 3 Tree chapter 5
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Heap tree
Heap treeHeap tree
Heap tree
 
Tree in data structure
Tree in data structureTree in data structure
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
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Unit 2 linked list
Unit 2   linked listUnit 2   linked list
Unit 2 linked list
 
single linked list
single linked listsingle linked list
single linked list
 

Similar to Binary tree traversal ppt - 02.03.2020

Similar to Binary tree traversal ppt - 02.03.2020 (20)

Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Tree of Data Structure
Tree of Data StructureTree of Data Structure
Tree of Data Structure
 
tree traversals.pdf
tree traversals.pdftree traversals.pdf
tree traversals.pdf
 
Describe the tree data structure- What is a root node- What is a child.docx
Describe the tree data structure- What is a root node- What is a child.docxDescribe the tree data structure- What is a root node- What is a child.docx
Describe the tree data structure- What is a root node- What is a child.docx
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptxLecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
Trees
TreesTrees
Trees
 
Trees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsTrees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and Algorithms
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
Data structure using c module 2
Data structure using c module 2Data structure using c module 2
Data structure using c module 2
 
Binary tree in data structures
Binary tree in  data structuresBinary tree in  data structures
Binary tree in data structures
 

Recently uploaded

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 

Recently uploaded (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 

Binary tree traversal ppt - 02.03.2020

  • 1. P. Raveena Lakshmi, M.Sc.,M.Phil., Assistant Professor, V.V.Vanniaperumal College For Women
  • 2. Tree Definition  Tree: a set of elements that either  it is empty  or, it has a distinguished element called the root and zero or more trees (called subtrees of the root)  What kind of definition is this?  What is the base case?  What is the recursive part?
  • 4. Tree Terminology  Nodes: the elements in the tree  Edges: connections between nodes  Root: the distinguished element that is the origin of the tree  There is only one root node in a tree  Empty tree has no nodes and no edges
  • 6.  Parent or predecessor: the node directly above another node in the hierarchy  A node can have only one parent  Child or successor: a node directly below another node in the hierarchy  Siblings: nodes that have the same parent  Ancestors of a node: its parent, the parent of its parent, etc.  Descendants of a node: its children, the children of its children, etc. Tree Terminology
  • 7. Tree Terminology  Leaf node: a node without children  Internal node: a node that is not a leaf node
  • 9. Discussion  Does a leaf node have any children?  Does the root node have a parent?  How many parents does every node other than the root node have?
  • 10. Height of a Tree  A path is a sequence of edges leading from one node to another  Length of a path: number of edges on the path  Height of a (non-empty) tree : length of the longest path from the root to a leaf  What is the height of a tree that has only a root node?  By convention, the height of an empty tree is -1
  • 11. Tree Terminology Root A B F H E J C D I L M N G K Height = 3
  • 12. Level of a Node  Level of a node: number of edges between root and the node  It can be defined recursively:  Level of root node is 0  Level of a node that is not the root node is level of its parent + 1  Question: What is the level of a node in terms of path length?  Question: What is the height of a tree in terms of levels?
  • 13. Level of a Node Level 0 Level 1 Level 2 Level 3
  • 14. Subtrees  Subtree of a node: consists of a child node and all its descendants  A subtree is itself a tree  A node may have many subtrees
  • 16. More Tree Terminology  Degree or arity of a node: the number of children it has  Degree or arity of a tree: the maximum of the degrees of the tree’s nodes
  • 17. Degree A B F H E J C D I L M G K 4 2 1 1 3 1 0 0 1 0 1 0 0 Degree of a tree: the maximum degree of its nodes
  • 18. Binary Trees  General tree: a tree each of whose nodes may have any number of children  n-ary tree: a tree each of whose nodes may have no more than n children  Binary tree: a tree each of whose nodes may have no more than 2 children  i.e. a binary tree is a tree with degree (arity) 2  The children (if present) are called the left child and right child
  • 19.  Recursive definition of a binary tree: it is  The empty tree  Or, a tree which has a root whose left and right subtrees are binary trees  A binary tree is a positional tree, i.e. it matters whether the subtree is left or right Binary Trees
  • 21. Tree Traversals  A traversal of a tree requires that each node of the tree be visited once  Example: a typical reason to traverse a tree is to display the data stored at each node of the tree  Standard traversal orderings:  preorder  inorder  postorder  level-order
  • 22. Traversals We’ll trace the different traversals using this tree; recursive calls, returns, and “visits” will be numbered in the order they occur A IH D E B F C G
  • 23. Preorder Traversal  Start at the root  Visit each node, followed by its children; we will choose to visit left child before right  Recursive algorithm for preorder traversal:  If tree is not empty, ○ Visit root node of tree ○ Perform preorder traversal of its left subtree ○ Perform preorder traversal of its right subtree  What is the base case?  What is the recursive part?
  • 24. Preorder Traversal public void preorder (BinaryTreeNode<T> r) { if (r != null) { visit(r); preorder (r.getLeftChild()); preorder (r.getRightChild()); } }
  • 25. Preorder Traversal 1: visit A 29: visit I9: visit H 5: visit D 17: visit E 3: visit B 27: visit F 25: visit C 39: visit G Nodes are visited in the order ABDHECFIG . . . .. . . .. 6 4 2 11 10 8 7 16 15 14 13 12 21 20 19 18 28 26 24 23 22 34 33 3231 30 38 37 45 44 43 42 41 40 35 36
  • 26. Inorder Traversal  Start at the root  Visit the left child of each node, then the node, then any remaining nodes  Recursive algorithm for inorder traversal  If tree is not empty, ○ Perform inorder traversal of left subtree of root ○ Visit root node of tree ○ Perform inorder traversal of its right subtree
  • 27. Inorder Traversal public void inorder (BinaryTreeNode<T> r) { if (r != null) { inorder (r.getLeftChild()); visit(r); inorder (r.getRightChild()); } }
  • 28. Inorder Traversal 23: visit A 29: visit I9: visit H 5: visit D 18: visit E 14: visit B 33: visit F 37: visit C 41: visit G Nodes are visited in the order DHBEAIFCG . . . .. . . .. 3 2 1 8 7 6 4 15 13 12 11 10 20 19 17 16 26 25 24 22 21 32 31 3028 27 38 36 45 44 43 42 40 39 34 35
  • 29. Postorder Traversal  Start at the root  Visit the children of each node, then the node  Recursive algorithm for postorder traversal  If tree is not empty, ○ Perform postorder traversal of left subtree of root ○ Perform postorder traversal of right subtree of root ○ Visit root node of tree
  • 30. Postorder Traversal public void postorder (BinaryTreeNode<T> r) { if (r != null) { postorder (r.getLeftChild()); postorder (r.getRightChild()); visit(r); } }
  • 31. Postorder Traversal 45: visit A 30: visit I10: visit H 12: visit D 19: visit E 21: visit B 34: visit F 43: visit C 41: visit G Nodes are visited in the order HDEBIFGCA . . . .. . . .. 3 2 1 7 6 5 4 14 13 11 9 8 18 17 16 15 25 24 23 22 20 31 29 2827 26 36 35 44 42 40 39 38 37 32 33