SlideShare a Scribd company logo
Chapter4
TREE
Tree:
The hierarchical representation of data is handle by using a data structure is called as
Tree. On which data is represent indifferent levels.
The first level of a tree is called as the root of the tree and if having the left and right sub
trees.
FIG : Tree
A binarytree T is definedasa finite setof elementcallednodes.Such that
1. T isemptycallednull tree or emptytree.
2. T containsdistinguishednode Rcalledrootof the tree,and remaining nodesof T froman
orderedpairof disjointbinarytree T1 & T2.
If T1 isnon emptythen itsroot is calledleftsuccessorof R.similarlyif calledasrightsuccessor
of R.
A
B C
D E G H
F J K
L
Root +
Left Right A B
a b c d a b dc
Above diagram representbinarytree T which consistof 11 nodesrepresentedbylettersA to
L excluding I. The root of the T isthe node A whichisat the topof the diagram.A left
downward stranded line fromunnode N indicatesthe left successor of N and rightdown
wordslanted line from N indicatesthatrightsuccessorof N.such that,
1. B isthe leftsuccessor andC isthe right successorof node A.
2. The leftsubtree of root A consist of the node B,D,E,Fand the right subtree of A consistof
the node C,G,H,J,K,L
Any node N inbinary tree T has either 0,1,2 successor unnode A,B,C& H have to
successor the nodesD,F,G,L& K have no successor. The nodeswith no successor are
calleda terminal nodes.
Binary tree T & T’ are said to be similar if they have the same shape.
Representation of binary tree incomputermemory:
Binarytree is representationin computermemory usingthree arrayas
INFO,LEFT,RIGHT.INFO isrepresentactuallynode information.Leftrepresentleftchild
address,Rightrepresentrightchildaddress.
Root isthe special pointervariablewhichisrepresentroot node firstnode addressof
binary tree if ROOT = NULL. Then binarytree isempty.
0 A 0
0 B 0
X L X
0 C 0
X D X 0 E X X G X
0 H 0
X F X
J X0 X K X
Root = 1
AVAL = 12
FIG : Memoryrepresentation of binary tree
Traversing of binary tree
A
B C
D E F G
a) Preorder[ Root ,Left, Right]
ABDECFG
b) In order[Left, Root,Right]
DBEAFCG
c) Postorder [Left,Right,Root]
DEBFGCA
A
B C
D E
F
a) Preorder = ABDCEF
b) In order = BDEFC
c) Postorder = DBFECA
A
B C
D E G H
F J
a) Preorder = ABDEFCGHJ
b) In order = DBEFAGCJH
c) Postorder = DFEBGJHCA
INFO LEFT RIGHT
1 A 2 3
2 B 4 5
3 C 7 8
4 D 0 0
5 E 6 0
6 F 0 0
7 G 0 0
8 H 9 10
9 J 11 0
10 K 0 0
11 L 0 0
12 13
13 14
14 0
Write an algorithm to traverse a binary tree inpreorder.
Algorithm:PRIORDER[ INFO, LEFT , RIGHT , ROOT]
Consider abinary tree T already exit in computer memory. This algorithm traverse tree T
inpreorder way and apply PROCESSon each element of tree T. An array STACK isused to templar
holdaddress of a node.
Step1 : Set PTR := ROOT , TOP := 1, STACK[TOP] : = NULL
Step2 : Repeat steps(3) to (5) while PTR!= NULL.
Step3 : Apply PROCESS on INFO[PTR]
Step4 : If RIGHT [PTR] ! = NULL , then;
SetTOP := TOP+ 1 , STACK[TOP] := RIGHT [PTR]
Step5 : If LEFT [PTR] != NULL , then;
Set PTR := LEFT [PTR]
Else
Set PTR := STACK[TOP] & TOP:= TOP -1.
Step 6 : Exit.
Inorder
Write an algorithmto traverse tree in order
Algorithm:INORDER [INFO,LEFT,RIGHT,ROOT]
Consider T isa binary tree alreadyexitin computermemory.This algorithmtraverse tree Tin
a inorder way & apply PROCESSto each node an array STACKis usedto temporary holdthe address
of nodes.
Step1 : Set PTR := ROOT ,TOP:= 1, STACK[TOP] := NULL
Step2 : Repeatfollowingstepswhile PTR!= NULL
(a) SetTOP := TOP+1, STACK[TOP] := PTR
(b) Set PTR := LEFT [PTR]
Step3 : Set PTR := STACK[TOP],TOP:= TOP-1
Step4 : Repeatsteps(5) to (7) while PTR!= NULL
Step5: Apply process on INFO[PTR]
Step6 : Check if RIGHT [PTR] != NULL , then;
(a) SetPTR := RIGHT [PTR]
(b) Go to step(2)
Step7 : SetPTR := STACK[TOP]
Step8 : Exit
Output:
A
B C
D E F
Top = 1 2 3 4
Stack = Null A B D
PTR = A B D Null
Output: D
Pstorder
Write algorithm to traverse tree inpostorder
Algorithm:POSTORDER[INFO,LEFT,RIGHT,ROOT]
A binarytree T is in memory.This algorithm doesapost order traverse of T, applyingan
operation PROCESStoeach of it’s nodes.An array STACK is usedto temporary holdthe address of
nodes.
Step1 : Set TOP:= 1, STACK[1] := NULL & PTR := ROOT
Step2: Repeat step(3) to (5) while PTR!= NULL
Step3 : SetTOP := TOP+1 & STACK [TOP] := PTR
Step4: If RIGHT [PTR] != NULL , then;
SetTOP := TOP+1 & STACK [TOP] := RIGHT [PTR]
Step5 : Set PTR := LEFT[PTR]
Step6 : SetPTR := STACK[TOP] &TOP := TOP-1
Step7: Repeat while PTR>0
(a) ApplyPROCESStoINFO[PTR]
(b) SetPTR := STACK[TOP] & TOP := TOP-1
Step8 : If PTR < 0,then;
(a) SetPTR := -PTR
(b) Go to step(2)
Step9: Exit.
Threads& In order Threading:
HEAD
Headernode
Above figure we can observe that near about half of the entries inthe pointer field LEFT
& RIGHT will contains NULL elements. This space must be efficiently used byreplacing byNULL
entriesby some othertype of information meanswe can replace certain NULL entries by special
pointers which pointsto nodeshigher inthe tree.These special pointer are called threads &
binary tree with such pointare called threadedtree.
The threads ina threaded tree must be distinguished insome way from ordinary
pointers. The threadsina diagram of a threaded tree are usually indicatedby dottedlines.Ina
computer memory an extra1-bit TAG field may be usedto distinguished threads from ordinary
pointers. Oralternatively,threads may be denoted by negative integerswhen ordinary pointers
are denoted bypositive integers.
There are many ways to thread a binary treesT, inone way threading of T, a threadwill
appearin a right field of node & will point tothe next node in the INORDER traverse of T. In two
way threading of T. threadwill appear inthe LEFT field of the node & will point tothe preceding
node inthe INORDER traversal of T.
When T doesnot have a header node then the LEFT pointer of last node will containNULL
value whenT have a header node the LEFT pointerof first node andthe RIGHT pointerof lastnode
pointto the header node .
0
0 x
0 A 0
0 B 0 0 C 0
X D X X E X X G X X H X
Fig (a) : One way INORDERthreading
Fig (a) : One way INORDERthreading
Fig (a) : One way INORDERthreading
A
B C
D E G H
B C
D E G H
B C
D E G H
A
A
ROOT INFO LEFT RIGHT
1
2
3
4
5
6
7
8
9
10
11
12
Fig: Representationof threadinginmemory
B
C
D
E
F
4
0
0
0
0
3
4
0
6
0
1 A 2 3
General Tree :
The general tree issetof element iscallednode.
1. T containsa distinguishedelementR,called the rootof T.
2. The remainingelement of Tfrom an orderedcollection of zero or more disjointtrees
T1,T2,……..Tm.
The trees T1,T2,…………Tm are called subtreesof the root R, and the roots of T1,T2…..Tm are
calledsuccessorof R. If N isa node with successorsS1,S2,……….Sm, then N is calledthe
parent of the Si’s the Si’s are called children of N , and the Si’sare calledsiblingsof each
other.
General tree isrootedi.e.tree orderedie. The childrenof each node N of tree have a specific
order considerageneral tree of 13 nodes.
A
B C D
E F G H J K
L M N
Fig: General Tree
Binarytree & general tree is differentobjecttwo basic are
1. BT ismay be emptybetween GT is notempty.
2. Suppose a node has only one child when then the child is differentsheet by the left
child & right child but no such different exitinaGT.
Computer Representation of GT.
The GT ismay be maintain in memory by linked representation which uses three
parallel array.INFO,CHILD,SIBL & pointer variable ROOT.
A
B C D
E F
Fig : General Tree
ROOT INFO CHILD SIBL
1
2
3
4
5
6
Fig: memoryrepresentation of GT
A
B
C
D
E
F
2
5
0
0
0
0
0
3
4
0
6
0
1

More Related Content

What's hot

Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
Aakash deep Singhal
 

What's hot (20)

(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
 
binary search tree
binary search treebinary search tree
binary search tree
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Python lecture 07
Python lecture 07Python lecture 07
Python lecture 07
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
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 Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
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
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Traversals | Data Structures
Traversals | Data StructuresTraversals | Data Structures
Traversals | Data Structures
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in india
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 

Similar to Chapter 4

ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
khitishlpu
 
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
ANANDSHOE
 
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithm
Trector Rancor
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
Siddhi Viradiya
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
Aakash deep Singhal
 

Similar to Chapter 4 (20)

ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
chapter5.PPT
chapter5.PPTchapter5.PPT
chapter5.PPT
 
7.tree
7.tree7.tree
7.tree
 
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
(a)PreOrder, PostOrder and Inorder are three ways you can iterate .pdf
 
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithm
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Chapter 09-Trees
Chapter 09-TreesChapter 09-Trees
Chapter 09-Trees
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
binarytrie.pdf
binarytrie.pdfbinarytrie.pdf
binarytrie.pdf
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
1st lecture.ppt
1st lecture.ppt1st lecture.ppt
1st lecture.ppt
 
Data Structures
Data StructuresData Structures
Data Structures
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Zippers
ZippersZippers
Zippers
 
03-Lists.ppt
03-Lists.ppt03-Lists.ppt
03-Lists.ppt
 

Recently uploaded

Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 

Recently uploaded (20)

Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 

Chapter 4

  • 1. Chapter4 TREE Tree: The hierarchical representation of data is handle by using a data structure is called as Tree. On which data is represent indifferent levels. The first level of a tree is called as the root of the tree and if having the left and right sub trees. FIG : Tree A binarytree T is definedasa finite setof elementcallednodes.Such that 1. T isemptycallednull tree or emptytree. 2. T containsdistinguishednode Rcalledrootof the tree,and remaining nodesof T froman orderedpairof disjointbinarytree T1 & T2. If T1 isnon emptythen itsroot is calledleftsuccessorof R.similarlyif calledasrightsuccessor of R. A B C D E G H F J K L Root + Left Right A B a b c d a b dc
  • 2. Above diagram representbinarytree T which consistof 11 nodesrepresentedbylettersA to L excluding I. The root of the T isthe node A whichisat the topof the diagram.A left downward stranded line fromunnode N indicatesthe left successor of N and rightdown wordslanted line from N indicatesthatrightsuccessorof N.such that, 1. B isthe leftsuccessor andC isthe right successorof node A. 2. The leftsubtree of root A consist of the node B,D,E,Fand the right subtree of A consistof the node C,G,H,J,K,L Any node N inbinary tree T has either 0,1,2 successor unnode A,B,C& H have to successor the nodesD,F,G,L& K have no successor. The nodeswith no successor are calleda terminal nodes. Binary tree T & T’ are said to be similar if they have the same shape. Representation of binary tree incomputermemory: Binarytree is representationin computermemory usingthree arrayas INFO,LEFT,RIGHT.INFO isrepresentactuallynode information.Leftrepresentleftchild address,Rightrepresentrightchildaddress. Root isthe special pointervariablewhichisrepresentroot node firstnode addressof binary tree if ROOT = NULL. Then binarytree isempty. 0 A 0 0 B 0 X L X 0 C 0 X D X 0 E X X G X 0 H 0 X F X J X0 X K X
  • 3. Root = 1 AVAL = 12 FIG : Memoryrepresentation of binary tree Traversing of binary tree A B C D E F G a) Preorder[ Root ,Left, Right] ABDECFG b) In order[Left, Root,Right] DBEAFCG c) Postorder [Left,Right,Root] DEBFGCA A B C D E F a) Preorder = ABDCEF b) In order = BDEFC c) Postorder = DBFECA A B C D E G H F J a) Preorder = ABDEFCGHJ b) In order = DBEFAGCJH c) Postorder = DFEBGJHCA INFO LEFT RIGHT 1 A 2 3 2 B 4 5 3 C 7 8 4 D 0 0 5 E 6 0 6 F 0 0 7 G 0 0 8 H 9 10 9 J 11 0 10 K 0 0 11 L 0 0 12 13 13 14 14 0
  • 4. Write an algorithm to traverse a binary tree inpreorder. Algorithm:PRIORDER[ INFO, LEFT , RIGHT , ROOT] Consider abinary tree T already exit in computer memory. This algorithm traverse tree T inpreorder way and apply PROCESSon each element of tree T. An array STACK isused to templar holdaddress of a node. Step1 : Set PTR := ROOT , TOP := 1, STACK[TOP] : = NULL Step2 : Repeat steps(3) to (5) while PTR!= NULL. Step3 : Apply PROCESS on INFO[PTR] Step4 : If RIGHT [PTR] ! = NULL , then; SetTOP := TOP+ 1 , STACK[TOP] := RIGHT [PTR] Step5 : If LEFT [PTR] != NULL , then; Set PTR := LEFT [PTR] Else Set PTR := STACK[TOP] & TOP:= TOP -1. Step 6 : Exit.
  • 5. Inorder Write an algorithmto traverse tree in order Algorithm:INORDER [INFO,LEFT,RIGHT,ROOT] Consider T isa binary tree alreadyexitin computermemory.This algorithmtraverse tree Tin a inorder way & apply PROCESSto each node an array STACKis usedto temporary holdthe address of nodes. Step1 : Set PTR := ROOT ,TOP:= 1, STACK[TOP] := NULL Step2 : Repeatfollowingstepswhile PTR!= NULL (a) SetTOP := TOP+1, STACK[TOP] := PTR (b) Set PTR := LEFT [PTR] Step3 : Set PTR := STACK[TOP],TOP:= TOP-1 Step4 : Repeatsteps(5) to (7) while PTR!= NULL Step5: Apply process on INFO[PTR] Step6 : Check if RIGHT [PTR] != NULL , then; (a) SetPTR := RIGHT [PTR] (b) Go to step(2) Step7 : SetPTR := STACK[TOP] Step8 : Exit Output: A B C D E F Top = 1 2 3 4 Stack = Null A B D PTR = A B D Null Output: D
  • 6. Pstorder Write algorithm to traverse tree inpostorder Algorithm:POSTORDER[INFO,LEFT,RIGHT,ROOT] A binarytree T is in memory.This algorithm doesapost order traverse of T, applyingan operation PROCESStoeach of it’s nodes.An array STACK is usedto temporary holdthe address of nodes. Step1 : Set TOP:= 1, STACK[1] := NULL & PTR := ROOT Step2: Repeat step(3) to (5) while PTR!= NULL Step3 : SetTOP := TOP+1 & STACK [TOP] := PTR Step4: If RIGHT [PTR] != NULL , then; SetTOP := TOP+1 & STACK [TOP] := RIGHT [PTR] Step5 : Set PTR := LEFT[PTR] Step6 : SetPTR := STACK[TOP] &TOP := TOP-1 Step7: Repeat while PTR>0 (a) ApplyPROCESStoINFO[PTR] (b) SetPTR := STACK[TOP] & TOP := TOP-1 Step8 : If PTR < 0,then; (a) SetPTR := -PTR (b) Go to step(2) Step9: Exit.
  • 7. Threads& In order Threading: HEAD Headernode Above figure we can observe that near about half of the entries inthe pointer field LEFT & RIGHT will contains NULL elements. This space must be efficiently used byreplacing byNULL entriesby some othertype of information meanswe can replace certain NULL entries by special pointers which pointsto nodeshigher inthe tree.These special pointer are called threads & binary tree with such pointare called threadedtree. The threads ina threaded tree must be distinguished insome way from ordinary pointers. The threadsina diagram of a threaded tree are usually indicatedby dottedlines.Ina computer memory an extra1-bit TAG field may be usedto distinguished threads from ordinary pointers. Oralternatively,threads may be denoted by negative integerswhen ordinary pointers are denoted bypositive integers. There are many ways to thread a binary treesT, inone way threading of T, a threadwill appearin a right field of node & will point tothe next node in the INORDER traverse of T. In two way threading of T. threadwill appear inthe LEFT field of the node & will point tothe preceding node inthe INORDER traversal of T. When T doesnot have a header node then the LEFT pointer of last node will containNULL value whenT have a header node the LEFT pointerof first node andthe RIGHT pointerof lastnode pointto the header node . 0 0 x 0 A 0 0 B 0 0 C 0 X D X X E X X G X X H X
  • 8. Fig (a) : One way INORDERthreading Fig (a) : One way INORDERthreading Fig (a) : One way INORDERthreading A B C D E G H B C D E G H B C D E G H A A
  • 9. ROOT INFO LEFT RIGHT 1 2 3 4 5 6 7 8 9 10 11 12 Fig: Representationof threadinginmemory B C D E F 4 0 0 0 0 3 4 0 6 0 1 A 2 3
  • 10. General Tree : The general tree issetof element iscallednode. 1. T containsa distinguishedelementR,called the rootof T. 2. The remainingelement of Tfrom an orderedcollection of zero or more disjointtrees T1,T2,……..Tm. The trees T1,T2,…………Tm are called subtreesof the root R, and the roots of T1,T2…..Tm are calledsuccessorof R. If N isa node with successorsS1,S2,……….Sm, then N is calledthe parent of the Si’s the Si’s are called children of N , and the Si’sare calledsiblingsof each other. General tree isrootedi.e.tree orderedie. The childrenof each node N of tree have a specific order considerageneral tree of 13 nodes. A B C D E F G H J K L M N Fig: General Tree Binarytree & general tree is differentobjecttwo basic are 1. BT ismay be emptybetween GT is notempty. 2. Suppose a node has only one child when then the child is differentsheet by the left child & right child but no such different exitinaGT. Computer Representation of GT.
  • 11. The GT ismay be maintain in memory by linked representation which uses three parallel array.INFO,CHILD,SIBL & pointer variable ROOT. A B C D E F Fig : General Tree ROOT INFO CHILD SIBL 1 2 3 4 5 6 Fig: memoryrepresentation of GT A B C D E F 2 5 0 0 0 0 0 3 4 0 6 0 1