SlideShare a Scribd company logo
Binary tree traversal ppt
Binary Tree Traversal
submitted by :-
K. Priyadarsini II-M.Sc., (CS & IT)
V. Priyanka II-M.Sc., (CS & IT)
(Nadar saraswathi college of arts & science, Theni,
TamilNadu)
• A tree is finite set of one or more
nodes.
• A binary tree is an important type of
tree structure which occurs very often.
• A binary tree traversal is perform
many operations that we often want to
perform on trees.
• A full traversal produces a linear
order for the information in a tree.
• This linear order may be familiar and
useful.
• If we let L,D,R stand for moving left,
printing the data, and moving right when
at a node then there are six possible
combinations of traversal:
 LDR
 LRD
 DLR
 DRL
 RDL
 RLD
• If we adopt the convention that we
traverse left before right then only three
traversals remain: LDR, LRD, DLR
• To these we assign the names inorder ,
postorder , preorder because there is a
natural correspondence between these
traversals and producing the infix ,
postfix , prefix forms of an expression.
This tree contains an arithmetic
expression with binary operators : add(+),
Multiply(*),divide(/),exponentiation(**) and
variables A,B,C,D, and E.
+
+
+
E*
D/
**A
CB
Inorder Traversal:
 Informally this calls for moving down
the tree towards the left until you can go
to farther.
 Then you “visit” the node, move one
node to the right and continue again.
 If you cannot move to the right, go
back one more node.
 A precise way of describing this
traversal is to write it as a recursive
procedure.
Procedure INORDER(T)
//T is a binary tree where each node has
three fields L-CHILD,DATA,R-CHILD//
If T≠0 then[call INORDER(LCHILD(T))
Print (DATA(T))
call(INORDER(RCHILD(T))]
End INORDER
Procedure PREORDER(T)
//T is a binary tree where each node has
three fields L-CHILD,DATA,R-CHILD//
If T≠0 then[Print (DATA(T))
call PREORDER(LCHILD(T))
call(PREORDER(RCHILD(T))]
End PREORDER
Procedure POSTORDER(T)
//T is a binary tree where each node has
three fields L-CHILD,DATA,R-CHILD//
If T≠0 then[call POSTORDER(LCHILD(T))
call(POSTORDER(RCHILD(T))
Print (DATA(T))]
End POSTORDER
Non Recursive Procedure:
 The above three algorithm using
recursion. It is very easy to produce
an equivalent non recursive
procedure.
INORDER1(T):
 A Nonrecursive version using a stack of size n
i0 //initialize the stack//
L1:if T!=0 then [i<-i+2;if i > n then call STACK_FULL
STACK(i-1)  T; STACK(i) ‘L2’
T LCHILD(T);go to L1; //traverse left subtree//
L2: Print (DATA(T))
ii+2; if i>n then call STACK__FULL
STACK(i-1)  T; STACK(i) ‘L3’
T RCHILD(T);go to L1; //traverse right subtree//
L3: if i !=0 then [//stack not empty, simulate a return//
T STACK(i-10); X STACK(i-1); x STACK(i)
i i-2; go to X]
End INORDER1
INORDER2(T):
 A simplified, nonrecursive version using a stack of size n
i0 //initialize stack//
L1: if T !=0 then [i i+1; if i>n then call STACK__FULL
stack( i )T; T LCHILD(T); go to L1
L2; print(DATA(T))
TRCHILD(T); go to L1]
if i !=0 then [//stack not empty//
T STACK(i): ii-1; go to L2]
End INORDER2
INORDER3(T):
 A nonrecursive, no go to version using a stack of size n
i0 //initialize stack//
loop
while T=0 do //move down LCHILD FIELDS//
ii+1; if i>n then call STACK__FULL
STACK( i )T; T LCHILD(T)
end
if i=0 then return
TSTACK(i); i i-1
print(DATA(T)); TRCHILD(T)
forever
End INORDER3
 Let n be the number of nodes in T. If we consider the
action of the above algorithm, we note that every node of the
tree is placed on the stack once.
 Thus, the statements STACK ( i ) ← T and T  STACK( i )
are executed n times. Moreover , T will equal zero once
for every zero link in the tree which is exactly
2n0+n1=n0+n1+n2+1=n+1
 So, every step will be executed no more than some
small constant times n or O (n).
Binary tree traversal ppt

More Related Content

PPTX
Threaded Binary Tree.pptx
PPTX
Binary Tree in Data Structure
PPTX
Binary search tree
PPT
Binary tree
PPT
Binary search tree(bst)
PPTX
Data structure - Graph
PPT
Spanning trees
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
Threaded Binary Tree.pptx
Binary Tree in Data Structure
Binary search tree
Binary tree
Binary search tree(bst)
Data structure - Graph
Spanning trees
Trees, Binary Search Tree, AVL Tree in Data Structures

What's hot (20)

PPTX
stack & queue
PPTX
THREADED BINARY TREE AND BINARY SEARCH TREE
PPT
B trees in Data Structure
PPTX
Tree in data structure
PPTX
Trees (data structure)
PDF
Linear search algorithm
PPSX
Data Structure (Queue)
PDF
Triggers in SQL | Edureka
PPTX
Depth-First Search
PPTX
Binary Tree Traversal
PPTX
Solving recurrences
PPT
Abstract data types
PPT
Data Structure and Algorithms Binary Search Tree
PDF
Searching and Sorting Techniques in Data Structure
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
Binary Search Tree
PPTX
Tree Traversal
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT
Lecture 1 data structures and algorithms
stack & queue
THREADED BINARY TREE AND BINARY SEARCH TREE
B trees in Data Structure
Tree in data structure
Trees (data structure)
Linear search algorithm
Data Structure (Queue)
Triggers in SQL | Edureka
Depth-First Search
Binary Tree Traversal
Solving recurrences
Abstract data types
Data Structure and Algorithms Binary Search Tree
Searching and Sorting Techniques in Data Structure
Binary Search - Design & Analysis of Algorithms
Binary Search Tree
Tree Traversal
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Lecture 1 data structures and algorithms
Ad

Similar to Binary tree traversal ppt (20)

PDF
Lecture notes data structures tree
PDF
7 chapter4 trees_binary
PPTX
BCS304 Module 3 Slide 29-61.pptx DSA notes 3rd sem
PDF
Unit II,III - Data Structures.pdf
PPTX
Zippers: Derivatives of Regular Types
PDF
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
PPT
Unit 4 tree
PPT
Data Structure and Algorithms Binary Tree
PPT
Review session2
PPTX
List,Stacks and Queues.pptx
PPT
ds 10-Binary Tree.ppt
PDF
481803111-Trees-Unit-Review-pdf - Copy.pdf
PPT
Data structures final lecture 1
PPTX
DM_Lecture 8_ tree travarsal_Khadija Islam..pptx
PPTX
Advanced data structure
PPT
data_structures_and_applications_-_module-4.ppt
PPTX
PPTX
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
Lecture notes data structures tree
7 chapter4 trees_binary
BCS304 Module 3 Slide 29-61.pptx DSA notes 3rd sem
Unit II,III - Data Structures.pdf
Zippers: Derivatives of Regular Types
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
Unit 4 tree
Data Structure and Algorithms Binary Tree
Review session2
List,Stacks and Queues.pptx
ds 10-Binary Tree.ppt
481803111-Trees-Unit-Review-pdf - Copy.pdf
Data structures final lecture 1
DM_Lecture 8_ tree travarsal_Khadija Islam..pptx
Advanced data structure
data_structures_and_applications_-_module-4.ppt
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
Ad

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Cell Types and Its function , kingdom of life
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
master seminar digital applications in india
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
01-Introduction-to-Information-Management.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Supply Chain Operations Speaking Notes -ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
Yogi Goddess Pres Conference Studio Updates
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Types and Its function , kingdom of life
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
human mycosis Human fungal infections are called human mycosis..pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Structure & Organelles in detailed.
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Microbial disease of the cardiovascular and lymphatic systems
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Abdominal Access Techniques with Prof. Dr. R K Mishra
A systematic review of self-coping strategies used by university students to ...
Chinmaya Tiranga quiz Grand Finale.pdf
master seminar digital applications in india
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
01-Introduction-to-Information-Management.pdf

Binary tree traversal ppt

  • 2. Binary Tree Traversal submitted by :- K. Priyadarsini II-M.Sc., (CS & IT) V. Priyanka II-M.Sc., (CS & IT) (Nadar saraswathi college of arts & science, Theni, TamilNadu)
  • 3. • A tree is finite set of one or more nodes. • A binary tree is an important type of tree structure which occurs very often. • A binary tree traversal is perform many operations that we often want to perform on trees. • A full traversal produces a linear order for the information in a tree.
  • 4. • This linear order may be familiar and useful. • If we let L,D,R stand for moving left, printing the data, and moving right when at a node then there are six possible combinations of traversal:  LDR  LRD  DLR  DRL  RDL  RLD
  • 5. • If we adopt the convention that we traverse left before right then only three traversals remain: LDR, LRD, DLR • To these we assign the names inorder , postorder , preorder because there is a natural correspondence between these traversals and producing the infix , postfix , prefix forms of an expression.
  • 6. This tree contains an arithmetic expression with binary operators : add(+), Multiply(*),divide(/),exponentiation(**) and variables A,B,C,D, and E. + + + E* D/ **A CB
  • 7. Inorder Traversal:  Informally this calls for moving down the tree towards the left until you can go to farther.  Then you “visit” the node, move one node to the right and continue again.  If you cannot move to the right, go back one more node.  A precise way of describing this traversal is to write it as a recursive procedure.
  • 8. Procedure INORDER(T) //T is a binary tree where each node has three fields L-CHILD,DATA,R-CHILD// If T≠0 then[call INORDER(LCHILD(T)) Print (DATA(T)) call(INORDER(RCHILD(T))] End INORDER Procedure PREORDER(T) //T is a binary tree where each node has three fields L-CHILD,DATA,R-CHILD// If T≠0 then[Print (DATA(T)) call PREORDER(LCHILD(T)) call(PREORDER(RCHILD(T))] End PREORDER
  • 9. Procedure POSTORDER(T) //T is a binary tree where each node has three fields L-CHILD,DATA,R-CHILD// If T≠0 then[call POSTORDER(LCHILD(T)) call(POSTORDER(RCHILD(T)) Print (DATA(T))] End POSTORDER
  • 10. Non Recursive Procedure:  The above three algorithm using recursion. It is very easy to produce an equivalent non recursive procedure.
  • 11. INORDER1(T):  A Nonrecursive version using a stack of size n i0 //initialize the stack// L1:if T!=0 then [i<-i+2;if i > n then call STACK_FULL STACK(i-1)  T; STACK(i) ‘L2’ T LCHILD(T);go to L1; //traverse left subtree// L2: Print (DATA(T)) ii+2; if i>n then call STACK__FULL STACK(i-1)  T; STACK(i) ‘L3’ T RCHILD(T);go to L1; //traverse right subtree// L3: if i !=0 then [//stack not empty, simulate a return// T STACK(i-10); X STACK(i-1); x STACK(i) i i-2; go to X] End INORDER1
  • 12. INORDER2(T):  A simplified, nonrecursive version using a stack of size n i0 //initialize stack// L1: if T !=0 then [i i+1; if i>n then call STACK__FULL stack( i )T; T LCHILD(T); go to L1 L2; print(DATA(T)) TRCHILD(T); go to L1] if i !=0 then [//stack not empty// T STACK(i): ii-1; go to L2] End INORDER2
  • 13. INORDER3(T):  A nonrecursive, no go to version using a stack of size n i0 //initialize stack// loop while T=0 do //move down LCHILD FIELDS// ii+1; if i>n then call STACK__FULL STACK( i )T; T LCHILD(T) end if i=0 then return TSTACK(i); i i-1 print(DATA(T)); TRCHILD(T) forever End INORDER3
  • 14.  Let n be the number of nodes in T. If we consider the action of the above algorithm, we note that every node of the tree is placed on the stack once.  Thus, the statements STACK ( i ) ← T and T  STACK( i ) are executed n times. Moreover , T will equal zero once for every zero link in the tree which is exactly 2n0+n1=n0+n1+n2+1=n+1  So, every step will be executed no more than some small constant times n or O (n).