SlideShare a Scribd company logo
1 of 12
BINARY TREE REPRESENTATION AND
TRAVERSAL
19IFTE063_K.S.Jothika
19IFTE073_T.G.Preethi
TREE
• A Tree is a finite set of one or more nodes
• (i). Root (ii). Subtree
BASIC TERMINOLOGY
• Root=M
• Subtree=G, W
• Leaves=B, H, J, N, S
• Terminal Nodes=B, H, N, S
• Non Terminal Nodes=M, G, W, D, P
• Level=4
• Height/Depth=ex(J):2
• Degree=G, W
• Parent=G, W, D, P, M
• Child=G, W, D, J, P, B, H, N, S
BINARY TREE
•A Binary Tree is set of nodes which is either empty or
consists of a root and to disjoint binary trees called LEFT
SUBTREE and RIGHT SUBTREE.
Example of binary tree:
SKEWED BINARY TREE COMPLETE BINARY TREE
REPRESENTION OF BINARY TREES
ARRAY REPRESENTATION
(a)The Root R Of T is stored in
TREE[1]
(b)If a node N occupies TREE[K]
then,
• Left child is stored in TREE[2*k]
• Right child is stored in
TREE[2*k+1]
(c)NULL is used to indicate an
empty subtree. TREE[1]=NULL
• INFO[K] contains the data at node N
• LEFT[K]=Left child of node N
• RIGHT[K]=Right child of node N
(a)The value ROOT=14 indicates that
harris is the root of the tree.
(b)LEFT[14]=9 indicates that cohen is
the left child of harris,
RIGHT[14]=7 indicates that lewis is the
right child of harris
(c)Repeating step (b) untill reach the
NULL
LINKED REPRESENTATION
TRAVERSING BINARY TREES
Tree Traversal is the process of visiting each node in
the tree exactly once
There are three standard ways of traversing a binary
tree T with Root R.
• Preorder
• Inorder
• postorder
PREORDER TRAVERSAL
ITeRATION
1. STACK:NULL set PTR=A
2. STACK:NULL, C Process B
3. STACK:NULL, C Process D
4. STACK: NULL, C, H Process G
5. STACK:NULL, C PTR=H
6. STACK:NULL, C, K No Left child
7. STACK:NULL, C Process K
8. STACK:NULL Process C
9. STACK:NULL, F Process E
10. STACK:NULL, popout F and Process F
A, B, D, G, H, K, C, E, F
PREORDER TRAVERSAL ALGORITHM
PREORD(INFO, LEFT, RIGHT, ROOT)
• [Initially push NULL onto STACK, andIinitialize PTR. ]
Set TOP:=1, STACK[1]:=NULL and PTR:=ROOT.
• Repeat Steps 3 to 5 while PTR!= NULL:
• Apply PROCESS to INFO[PTR].
• [Right child?]
If RIGHT[PTR]!=NULL, then:[Push on STACK. ]
Set TOP:=TOP+1, and STACK[TOP]:=RIGHT[PTR].
[End of If structure]
• [Left child? ]
If LEFT[PTR]!=NULL, then:
Set PTR:=LEFT[PTR].
Else:[Pop from STACK. ]
Set PTR:=STACK[TOP] and TOP:=TOP-1.
[End of if structure. ]
[End of step 2 loop. ]
• Exit.
INORDER TRAVERSAL
ITeRATION
1. STACK:NULL set PTR=A
2. STACK:NULL,A, B, D, G, K
3. STACK:NULL,A, B Set PTR:=H
4. PTR:H, STACK:NULL, A, B, H, L
5. STACK:NULL,A, B Set PTR:=M
6. STACK:NULL,A,B,M
7. STACK:NULL,PTR=C
8. PTR=C, STACK:NULL ,C, E
9. [BT]E, C
K, G, D, L, H, M, B, A, E, C
INORDER TRAVERSAL ALGORITHM
INORD(INFO, LEFT, RIGHT, ROOT)
• [Push NULL onto STACK and initialize PTR. ]
Set TOP:=1, STACK[1]:=NULL and
PTR:=ROOT.
• Repeat while PTR!=NULL:[Pushes left most
path onto stack. ]
(a) Set TOP:=TOP+1 and STACK[TOP]:=PTR.
[Saves node. ]
(b) Set PTR:=LEFT[PTR]. [Updates PTR. ]
[End of loop. ]
• Set PTR:=STACK[TOP] and TOP:=TOP-1.
[ Pops node from STACK. ]
• Repeat Steps 5 to 7 while
PTR!=NULL:[Backtracking. ]
• Apply PROCESS to INFO[PTR].
• [Right child?] If RIGHT[PTR]!=NULL, then:
(a) Set PTR:=RIGHT[PTR].
(b) Go to step 2.
[ End of if structure. ]
• Set PTR:=STACK[TOP] and TOP:=TOP-1.[Pops node]
[End of step 4 loop. ]
• Exit.
POSTORDER TRAVERSAL
ITeRATION
1. STACK:=NULL set PTR:=A
2. STACK:=NULL,A,-C, B,D,-H, G, K
3. POP K, G, -H STACK:NULL,A, -C, B, D PTR=-H
Reset PTR:=H
4. PTR:H, STACK:NULL, A,-C, B, D, H, -M, L
5. Pop L, -M
6. PTR=M STACK:NULL,A,-C, B, D, H, M Pop M, H,
D, B, -C PTR=C
7. PTR=C, STACK:NULL ,A, C, E
Pop and Process E, C, AK, G, L, M, H, D, B, E, C, A
POSTORDER TRAVERSAL ALGORITHM
• Set PTR:=SRACK[TOP] and TOP:=TOP-
1.
[Pops node from STACK. ]
• Repeat while PTR>0;
(a) Apply PROCESS to INFO[PTR].
(b) Set PTR:=STACK[TOP] and
TOP:=TOP-1.
[ Pops node from STACK. ]
[End of loop. ]
• If PTR<0, then :
(a) Set PTR:=-PTR.
(b) Go to step 2.
[ End of if structure. )
• Exit.
POSTORD(INFO, LEFT, RIGHT, ROOT)
• [Push NULL onto STACK and initialize PTR.
]
Set TOP:=1, STACK[1]:=NULL and
PTR:=ROOT.
• [Push left most path onto stack. ]
Repeat Steps 3 to 5 while PTR!= NULL:
• Set TOP:=TOP+1 and STACK[TOP]:=PTR.
[Pushes PTR on STACK. ]
• If RIGHT[PTR]!=NULL, then:[Push on
STACK. ]
Set TOP:=TOP+1 and
STACK[TOP]:=RIGHT[PTR].
[End of If structure]
• Set PTR:=LEFT[PTR]. [Updates pointer

More Related Content

Similar to binary tree representation and traversal

Similar to binary tree representation and traversal (10)

ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Compiler Design Unit 2
Compiler Design Unit 2Compiler Design Unit 2
Compiler Design Unit 2
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
trees
trees trees
trees
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
21. Heap_new.ppt
21. Heap_new.ppt21. Heap_new.ppt
21. Heap_new.ppt
 
Segment tree
Segment treeSegment tree
Segment tree
 
Chapter 09-Trees
Chapter 09-TreesChapter 09-Trees
Chapter 09-Trees
 

More from Preethi T G

Data communication and networks by B. Forouzan
Data communication and networks by B. ForouzanData communication and networks by B. Forouzan
Data communication and networks by B. ForouzanPreethi T G
 
Hacking and protecting yourself from hackers .
Hacking and protecting yourself from hackers .Hacking and protecting yourself from hackers .
Hacking and protecting yourself from hackers .Preethi T G
 
Multimedia by Tay Vaughan
Multimedia by Tay Vaughan Multimedia by Tay Vaughan
Multimedia by Tay Vaughan Preethi T G
 
National symbols of india
National symbols of indiaNational symbols of india
National symbols of indiaPreethi T G
 
Files in Operating system
Files in Operating system Files in Operating system
Files in Operating system Preethi T G
 
Various cultures in Tamil Nadu
Various cultures in Tamil NaduVarious cultures in Tamil Nadu
Various cultures in Tamil NaduPreethi T G
 
Normalization in relational database management systems
Normalization in relational database management systemsNormalization in relational database management systems
Normalization in relational database management systemsPreethi T G
 
Software Quality Management in Wipro and case tools ,Wipro Introduction and c...
Software Quality Management in Wipro and case tools ,Wipro Introduction and c...Software Quality Management in Wipro and case tools ,Wipro Introduction and c...
Software Quality Management in Wipro and case tools ,Wipro Introduction and c...Preethi T G
 
Internet and world wide web
Internet and world wide webInternet and world wide web
Internet and world wide webPreethi T G
 
Fundamental Rights and Duties
Fundamental Rights and DutiesFundamental Rights and Duties
Fundamental Rights and DutiesPreethi T G
 
Software engineering project(Bikes and scooters rental system)
Software engineering project(Bikes and scooters rental system)Software engineering project(Bikes and scooters rental system)
Software engineering project(Bikes and scooters rental system)Preethi T G
 
Principles of programming languages(Functional programming Languages using LISP)
Principles of programming languages(Functional programming Languages using LISP)Principles of programming languages(Functional programming Languages using LISP)
Principles of programming languages(Functional programming Languages using LISP)Preethi T G
 
Relational Database Management System(TCS)
Relational Database Management System(TCS)Relational Database Management System(TCS)
Relational Database Management System(TCS)Preethi T G
 
Computer organisation and architecture
Computer organisation and architectureComputer organisation and architecture
Computer organisation and architecturePreethi T G
 

More from Preethi T G (14)

Data communication and networks by B. Forouzan
Data communication and networks by B. ForouzanData communication and networks by B. Forouzan
Data communication and networks by B. Forouzan
 
Hacking and protecting yourself from hackers .
Hacking and protecting yourself from hackers .Hacking and protecting yourself from hackers .
Hacking and protecting yourself from hackers .
 
Multimedia by Tay Vaughan
Multimedia by Tay Vaughan Multimedia by Tay Vaughan
Multimedia by Tay Vaughan
 
National symbols of india
National symbols of indiaNational symbols of india
National symbols of india
 
Files in Operating system
Files in Operating system Files in Operating system
Files in Operating system
 
Various cultures in Tamil Nadu
Various cultures in Tamil NaduVarious cultures in Tamil Nadu
Various cultures in Tamil Nadu
 
Normalization in relational database management systems
Normalization in relational database management systemsNormalization in relational database management systems
Normalization in relational database management systems
 
Software Quality Management in Wipro and case tools ,Wipro Introduction and c...
Software Quality Management in Wipro and case tools ,Wipro Introduction and c...Software Quality Management in Wipro and case tools ,Wipro Introduction and c...
Software Quality Management in Wipro and case tools ,Wipro Introduction and c...
 
Internet and world wide web
Internet and world wide webInternet and world wide web
Internet and world wide web
 
Fundamental Rights and Duties
Fundamental Rights and DutiesFundamental Rights and Duties
Fundamental Rights and Duties
 
Software engineering project(Bikes and scooters rental system)
Software engineering project(Bikes and scooters rental system)Software engineering project(Bikes and scooters rental system)
Software engineering project(Bikes and scooters rental system)
 
Principles of programming languages(Functional programming Languages using LISP)
Principles of programming languages(Functional programming Languages using LISP)Principles of programming languages(Functional programming Languages using LISP)
Principles of programming languages(Functional programming Languages using LISP)
 
Relational Database Management System(TCS)
Relational Database Management System(TCS)Relational Database Management System(TCS)
Relational Database Management System(TCS)
 
Computer organisation and architecture
Computer organisation and architectureComputer organisation and architecture
Computer organisation and architecture
 

Recently uploaded

Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxolyaivanovalion
 

Recently uploaded (20)

Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 

binary tree representation and traversal

  • 1. BINARY TREE REPRESENTATION AND TRAVERSAL 19IFTE063_K.S.Jothika 19IFTE073_T.G.Preethi
  • 2. TREE • A Tree is a finite set of one or more nodes • (i). Root (ii). Subtree BASIC TERMINOLOGY • Root=M • Subtree=G, W • Leaves=B, H, J, N, S • Terminal Nodes=B, H, N, S • Non Terminal Nodes=M, G, W, D, P • Level=4 • Height/Depth=ex(J):2 • Degree=G, W • Parent=G, W, D, P, M • Child=G, W, D, J, P, B, H, N, S
  • 3. BINARY TREE •A Binary Tree is set of nodes which is either empty or consists of a root and to disjoint binary trees called LEFT SUBTREE and RIGHT SUBTREE. Example of binary tree: SKEWED BINARY TREE COMPLETE BINARY TREE
  • 4. REPRESENTION OF BINARY TREES ARRAY REPRESENTATION (a)The Root R Of T is stored in TREE[1] (b)If a node N occupies TREE[K] then, • Left child is stored in TREE[2*k] • Right child is stored in TREE[2*k+1] (c)NULL is used to indicate an empty subtree. TREE[1]=NULL
  • 5. • INFO[K] contains the data at node N • LEFT[K]=Left child of node N • RIGHT[K]=Right child of node N (a)The value ROOT=14 indicates that harris is the root of the tree. (b)LEFT[14]=9 indicates that cohen is the left child of harris, RIGHT[14]=7 indicates that lewis is the right child of harris (c)Repeating step (b) untill reach the NULL LINKED REPRESENTATION
  • 6. TRAVERSING BINARY TREES Tree Traversal is the process of visiting each node in the tree exactly once There are three standard ways of traversing a binary tree T with Root R. • Preorder • Inorder • postorder
  • 7. PREORDER TRAVERSAL ITeRATION 1. STACK:NULL set PTR=A 2. STACK:NULL, C Process B 3. STACK:NULL, C Process D 4. STACK: NULL, C, H Process G 5. STACK:NULL, C PTR=H 6. STACK:NULL, C, K No Left child 7. STACK:NULL, C Process K 8. STACK:NULL Process C 9. STACK:NULL, F Process E 10. STACK:NULL, popout F and Process F A, B, D, G, H, K, C, E, F
  • 8. PREORDER TRAVERSAL ALGORITHM PREORD(INFO, LEFT, RIGHT, ROOT) • [Initially push NULL onto STACK, andIinitialize PTR. ] Set TOP:=1, STACK[1]:=NULL and PTR:=ROOT. • Repeat Steps 3 to 5 while PTR!= NULL: • Apply PROCESS to INFO[PTR]. • [Right child?] If RIGHT[PTR]!=NULL, then:[Push on STACK. ] Set TOP:=TOP+1, and STACK[TOP]:=RIGHT[PTR]. [End of If structure] • [Left child? ] If LEFT[PTR]!=NULL, then: Set PTR:=LEFT[PTR]. Else:[Pop from STACK. ] Set PTR:=STACK[TOP] and TOP:=TOP-1. [End of if structure. ] [End of step 2 loop. ] • Exit.
  • 9. INORDER TRAVERSAL ITeRATION 1. STACK:NULL set PTR=A 2. STACK:NULL,A, B, D, G, K 3. STACK:NULL,A, B Set PTR:=H 4. PTR:H, STACK:NULL, A, B, H, L 5. STACK:NULL,A, B Set PTR:=M 6. STACK:NULL,A,B,M 7. STACK:NULL,PTR=C 8. PTR=C, STACK:NULL ,C, E 9. [BT]E, C K, G, D, L, H, M, B, A, E, C
  • 10. INORDER TRAVERSAL ALGORITHM INORD(INFO, LEFT, RIGHT, ROOT) • [Push NULL onto STACK and initialize PTR. ] Set TOP:=1, STACK[1]:=NULL and PTR:=ROOT. • Repeat while PTR!=NULL:[Pushes left most path onto stack. ] (a) Set TOP:=TOP+1 and STACK[TOP]:=PTR. [Saves node. ] (b) Set PTR:=LEFT[PTR]. [Updates PTR. ] [End of loop. ] • Set PTR:=STACK[TOP] and TOP:=TOP-1. [ Pops node from STACK. ] • Repeat Steps 5 to 7 while PTR!=NULL:[Backtracking. ] • Apply PROCESS to INFO[PTR]. • [Right child?] If RIGHT[PTR]!=NULL, then: (a) Set PTR:=RIGHT[PTR]. (b) Go to step 2. [ End of if structure. ] • Set PTR:=STACK[TOP] and TOP:=TOP-1.[Pops node] [End of step 4 loop. ] • Exit.
  • 11. POSTORDER TRAVERSAL ITeRATION 1. STACK:=NULL set PTR:=A 2. STACK:=NULL,A,-C, B,D,-H, G, K 3. POP K, G, -H STACK:NULL,A, -C, B, D PTR=-H Reset PTR:=H 4. PTR:H, STACK:NULL, A,-C, B, D, H, -M, L 5. Pop L, -M 6. PTR=M STACK:NULL,A,-C, B, D, H, M Pop M, H, D, B, -C PTR=C 7. PTR=C, STACK:NULL ,A, C, E Pop and Process E, C, AK, G, L, M, H, D, B, E, C, A
  • 12. POSTORDER TRAVERSAL ALGORITHM • Set PTR:=SRACK[TOP] and TOP:=TOP- 1. [Pops node from STACK. ] • Repeat while PTR>0; (a) Apply PROCESS to INFO[PTR]. (b) Set PTR:=STACK[TOP] and TOP:=TOP-1. [ Pops node from STACK. ] [End of loop. ] • If PTR<0, then : (a) Set PTR:=-PTR. (b) Go to step 2. [ End of if structure. ) • Exit. POSTORD(INFO, LEFT, RIGHT, ROOT) • [Push NULL onto STACK and initialize PTR. ] Set TOP:=1, STACK[1]:=NULL and PTR:=ROOT. • [Push left most path onto stack. ] Repeat Steps 3 to 5 while PTR!= NULL: • Set TOP:=TOP+1 and STACK[TOP]:=PTR. [Pushes PTR on STACK. ] • If RIGHT[PTR]!=NULL, then:[Push on STACK. ] Set TOP:=TOP+1 and STACK[TOP]:=RIGHT[PTR]. [End of If structure] • Set PTR:=LEFT[PTR]. [Updates pointer