SlideShare a Scribd company logo
1 of 41
Threaded BinaryTree
By:-
P.Aruna
I M. Sc IT
Content:-
• Introduction
• Representation of Binary tree
• Advantages of binary tree
• Various operations
• InorderSuccessor
• InorderPredecessor
• InorderTraversal
• Insertion
• Deletion
2
Let’s start with the first set of slides
3
Introduction:-
The fact that binary tree more than 50 percent of
link field are with null values, thereby wasting the
memory space. A clever way to utilize these
Null-link field has been devised by perlis and
Thornton.There idea was to store pointers of
some nodes in these link field;
These extra pointers are called threads and the
tree is specially termed threaded binary tree
4
Representation of threaded
binary tree:-
We have to first decide to which node
threaded should point. In fact there
are three ways to threaded binary tree,
these correspondto in order, preorder,
postorder traversal
The threaded binary tree
corresponding to inorder traversal is
called Inorder threating and that
corresponding to preorder traversal is
called preorder threading similar to
postorder threading
If any node N has its right(left)
link Field empty, then this field
will be initalized by the inorder
successor
5
6
The data content of this header node
is null and this node can be
considered as the inorder
predecessor and the inorder
successor of the first and the last
node respectively in the picture shows
an empty inorder threaded binary
tree. The complete form of an inorder
threaded binary tree is shown in the
picturein case of an empty inorder
threaded binary tree, the left link field
field of the headér node is a threaded
which point it’s self
7
A simple node structure have
LCHILD and RCHILD is not
sufficient. The noade structure that
is required is showing inthe picture,
this node structure includes two
extra field:LTAG and RTAG. They
are binary fields and will stroe their
TRUE Or FALSE.
LTAG=TRUE the pointer of LCHILD
is a threaded
LTAG=FALSE the pointer of LCHILD
is a link
RTAG=TRUE the pointer of RCHILD
is a threaded
RTAG=FALSE the pointer of RCHILD
is a link 8
9
tree
The traversal operations is
faster than that of its unthreaded
version, because with a threaded
binary tree, non recursive
implementation is possible that can
run faster and doesn’t require of
stack management.
We can efficiently determine
the predecessor and successor
nodes starting from any node. In case
of on an unthreaded binary tree,
however, this task is more Time
consuming And difficult.
10
Any node can be accessed
From any other node.
Threads are usually more
to upward whereas link are
downward.
Although insertion into and
deletion from a threaded
tree are time consuming
operation these are very
esay to implement.
11
Various operations on
threaded binary tree:-
• To find inorder successor of
any node
• To find inorder predecessor
of any node
• To find inorder traversal of
any node
• Insertion of a node into a
threaded binary tree
• Deletion of a node from a
threaded binary tree
To find the inorder successor of
any node:-
the address of any node in an inorder
successor is very straightforward.
If N does not have a right child then
the thread Points to it’s inorder
successor
Else
If N has a right sub tree then the left
most node in this right sub tree which
does not have any left child is the
13
The given picture inorder is
BAGEDFC
Inorder successor of ‘B’is ‘A’
Inorder successor of ‘A’is ‘G’
Inorder successor of ‘D’ is ‘F’
Inorder successor of ‘C’ is the
HEATER
We can formulate the procedure
Inordersuccessor(N) to find the
inorder successor of any node N in a
threaded binary tree
14
Algorithm Inordersuccessor:-
Input:PTR is the pointer to any node whose
inorder successor has to be found
Output:-pointer to the inorder successor of
the node PTR
Data structure:-Linked structure of
Threaded binary tree
Steps:-
succ=PTR->RCHILD
If(PTR->RTAG=FALSE)then
While(succ->LTAG=FALSE)
Succ=succ->LCHILD
EndWhile
EndIF
Return(succ)
stop
15
Algorithm Inorderpredecessor:-
Input:PTR is the pointer to any node whose
inorder Predecessor has to be found
Output:-pointer to the inorder Predecessor
of the node PTR
Data structure:-Linked structure of
Threaded binary tree
Steps:-
Pred=PTR->LCHILD
If(PTR->LTAG=FALSE)then
While(Pred->RTAG=FALSE)
Pred=Pred->RCHILD
EndWhile
EndIF
Return(pred)
stop
16
Algorithm InorderTraversal_TBT:-
Input:An inorder threaded binary tree
Output:-Inorder traversal of the threaded
binary tree
Data structure:-Linked structure of
Threaded binary tree having HEADER as the
pointer to root
Steps:-
ptr=HEADER
While(TRUE) do
ptr=Inordersuccessor (ptr)
If(ptr=HEADER)
Exit
Else
Process (ptr)
EndIF
EndWhile
stop
Insertion of a node into a
threaded binary tree
17
18
Add a new
node at
Right child
node NO
Right sub
tree
19
Add a new
node at Left
child node
NO Left sub
tree
20
Add a new node at
Right child node it
has non empty
Right sub tree
21
Add a new
node at Leftt
child node it
has Left sub
tree
22
23
24
25
26
Algorithm InsertInorder_TBT:-
Inputs:-
(a) HEADER, the pointer to the header node of the
threaded binary tree
(b) X is the data of a node after which the insertion
has to be done
(c) N is the data node to be interested
Output:-If X exists in the tree then N is Inserted after X.
Data structure:- Linked structure of threaded binary
tree.
Steps:-
ptr=HEADER->LCHILD
flag=FALSE
While(ptr! =HEADER) and(flag=FALSE)
27
Xptr=ptr
Flag=TRUE
Else
ptr=Inordersuccessor (ptr)
Endif
EndWhile
If(flag=FALSE) then
Print”Node doesn’t exist:no insertion “
Exit
EndIf
Read option=L/R for Left(L) or Right (R)
child
Nptr=GetNode (NODE)
Case: option=‘L’
If(Xptr->LTAG=TRUE) then
Nptr->LCHILD=Xptr->LCHILD
Nptr->LTAG=TRUE
Xptr->LCHILD=Nptr
Xptr->LTAG=FALSE
Nptr-RCHILD=Xptr
Nptr->RTAG=TRUE
Else
Lptr=Xptr->LCHILD
Xptr->LCHILD=Nptr
Xptr->LTAG=FALSE
Nptr->RCHILD=Xptr
Nptr->RTAG=TRUE
Nptr->LCHILD=Lptr
Nptr->LTAG=FALSE
ptr=InorderPredecessor (Xptr)
ptr->RCHILD=Nptr
End If
Case: option=‘R’
If(Xptr->RTAG=TRUE) then
Nptr->RCHILD=Xptr->RCHILD
Nptr->RTAG=TRUE
Xptr->RCHILD=Nptr
Xptr->RTAG=FALSE
Nptr->LCHILD=Xptr
Nptr->LTAG=TRUE 28
Else
Rptr=Xptr->RCHILD
Xptr->RCHILD=Nptr
Xptr->RTAG=FALSE
Nptr->LCHILD=Xptr
Nptr->LTAG=TRUE
Nptr->RCHILD=Rptr
Nptr->RTAG=FALSE
ptr=INSUCC (Xptr)
ptr->LCHILD=Nptr
End If
EndCase
Stop
29
30
Deletion of a node
from a threaded
binary tree
31
Delete a leaf
node right or
left
32
Delete of
node having
right sub
tree or left
sub tree
33
Delete of the
node having both
sub tree
34
35
Algorithm Parent:-
Input:- The pointer PTR of a node whose parent is to be found
out.
Output:-The pointer PARENT node of a given node PTR
Data Structure:- Linked structure of a threaded binary tree.
Steps:-
varptr=PTR
While(varptr, RTAG=0) do
varptr=varptr->RCHILD
End while
pred=varptr->RCHILD
If(pred->LCHILD=ptr)then
parent=pred
Else
pred=pred ->LCHILD
While (pred->LCHILD! =ptr)
Pred=pred->RCHILD
End while
parent=pred
End if
Return(parent)
Stop
36
deleted
Output:- The threaded binary search tree without a
node PTR or it’s data content
Data structure:-Linked structure of threaded binary
tree
Steps:
parent=Parent (PTR)
If(Ptr->LTAG=TRUE) and(PTR->RTAG=TRUE) then
Case=1
Else
If(PTR->LTAG=FALSE) and(PTR->RTAG=FALSE)
then
Case=3
Else
Case=2
End if
End if
37
Do case=1
If(parent->RCHILD=PTR) then
parent->RCHILD=PTR->RCHILD
parent->RTAG=TRUE
Else
If(parent->LCHILD=PTR) then
parent->LCHILD=PTR->LCHILD
parent->LTAG=TRUE
End if
End if
ReturnNode(PTR)
Exit
Endo
Do case=2
// Find the sub tree of the node PTR
If(PTR->RTAG=FALSE) then
child=PTR->RCHILD
Else
If(PTR->LTAG=FALSE) then
Child=PTR->LCHILD
End if
38
//modify the link field of parent
If(parent->RCHILD=PTR) then
parent->RCHILD=child
Else
If(parent->LCHILD=PTR) then
parent->LCHILD=child
Endif
//modify the thread pointer in succeding node of PTR
If(PTR->RTAG=FALSE) then
Succ=inorder successor (PTR)
Succ->LCHILD=parent
Else
If(PTR->LTAG=FALSE) then
Pred=inorderPredecessor (PTR)
Pred->RCHILD=parent
Endif
Endif
Return node (PTR)
Exit
End Do 39
Do case=3
Succ=Inorder successor (PTR)
PTR->DATA=succ->DATA
Delete inorder_TBST(succ)
End Do
Stop
40
Thankyou
41

More Related Content

What's hot (20)

AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Trees
TreesTrees
Trees
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Tree
TreeTree
Tree
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
single linked list
single linked listsingle linked list
single linked list
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Linked list
Linked listLinked list
Linked list
 
Binary tree
Binary tree Binary tree
Binary tree
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 

Similar to In-depth guide to threaded binary trees

Similar to In-depth guide to threaded binary trees (20)

Data Structures
Data StructuresData Structures
Data Structures
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
 
Data Structure
Data StructureData Structure
Data Structure
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Ds 111011055724-phpapp01
Ds 111011055724-phpapp01
 
Binary tree
Binary treeBinary tree
Binary tree
 
Trees
TreesTrees
Trees
 
Binary trees
Binary treesBinary trees
Binary trees
 
Trees.pptx
Trees.pptxTrees.pptx
Trees.pptx
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Trees
TreesTrees
Trees
 
Trees in data structrures
Trees in data structruresTrees in data structrures
Trees in data structrures
 
binarytrie.pdf
binarytrie.pdfbinarytrie.pdf
binarytrie.pdf
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURES
 
Technical aptitude questions_e_book1
Technical aptitude questions_e_book1Technical aptitude questions_e_book1
Technical aptitude questions_e_book1
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
Data structure-question-bank
Data structure-question-bankData structure-question-bank
Data structure-question-bank
 
for sbi so Ds c c++ unix rdbms sql cn os
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn os
 

Recently uploaded

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
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
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
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
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
 
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
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

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
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
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
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
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
 
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
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

In-depth guide to threaded binary trees

  • 2. Content:- • Introduction • Representation of Binary tree • Advantages of binary tree • Various operations • InorderSuccessor • InorderPredecessor • InorderTraversal • Insertion • Deletion 2
  • 3. Let’s start with the first set of slides 3 Introduction:- The fact that binary tree more than 50 percent of link field are with null values, thereby wasting the memory space. A clever way to utilize these Null-link field has been devised by perlis and Thornton.There idea was to store pointers of some nodes in these link field; These extra pointers are called threads and the tree is specially termed threaded binary tree
  • 4. 4 Representation of threaded binary tree:- We have to first decide to which node threaded should point. In fact there are three ways to threaded binary tree, these correspondto in order, preorder, postorder traversal The threaded binary tree corresponding to inorder traversal is called Inorder threating and that corresponding to preorder traversal is called preorder threading similar to postorder threading
  • 5. If any node N has its right(left) link Field empty, then this field will be initalized by the inorder successor 5
  • 6. 6
  • 7. The data content of this header node is null and this node can be considered as the inorder predecessor and the inorder successor of the first and the last node respectively in the picture shows an empty inorder threaded binary tree. The complete form of an inorder threaded binary tree is shown in the picturein case of an empty inorder threaded binary tree, the left link field field of the headér node is a threaded which point it’s self 7
  • 8. A simple node structure have LCHILD and RCHILD is not sufficient. The noade structure that is required is showing inthe picture, this node structure includes two extra field:LTAG and RTAG. They are binary fields and will stroe their TRUE Or FALSE. LTAG=TRUE the pointer of LCHILD is a threaded LTAG=FALSE the pointer of LCHILD is a link RTAG=TRUE the pointer of RCHILD is a threaded RTAG=FALSE the pointer of RCHILD is a link 8
  • 9. 9 tree The traversal operations is faster than that of its unthreaded version, because with a threaded binary tree, non recursive implementation is possible that can run faster and doesn’t require of stack management. We can efficiently determine the predecessor and successor nodes starting from any node. In case of on an unthreaded binary tree, however, this task is more Time consuming And difficult.
  • 10. 10 Any node can be accessed From any other node. Threads are usually more to upward whereas link are downward. Although insertion into and deletion from a threaded tree are time consuming operation these are very esay to implement.
  • 11. 11 Various operations on threaded binary tree:- • To find inorder successor of any node • To find inorder predecessor of any node • To find inorder traversal of any node • Insertion of a node into a threaded binary tree • Deletion of a node from a threaded binary tree
  • 12. To find the inorder successor of any node:- the address of any node in an inorder successor is very straightforward. If N does not have a right child then the thread Points to it’s inorder successor Else If N has a right sub tree then the left most node in this right sub tree which does not have any left child is the
  • 13. 13 The given picture inorder is BAGEDFC Inorder successor of ‘B’is ‘A’ Inorder successor of ‘A’is ‘G’ Inorder successor of ‘D’ is ‘F’ Inorder successor of ‘C’ is the HEATER We can formulate the procedure Inordersuccessor(N) to find the inorder successor of any node N in a threaded binary tree
  • 14. 14 Algorithm Inordersuccessor:- Input:PTR is the pointer to any node whose inorder successor has to be found Output:-pointer to the inorder successor of the node PTR Data structure:-Linked structure of Threaded binary tree Steps:- succ=PTR->RCHILD If(PTR->RTAG=FALSE)then While(succ->LTAG=FALSE) Succ=succ->LCHILD EndWhile EndIF Return(succ) stop
  • 15. 15 Algorithm Inorderpredecessor:- Input:PTR is the pointer to any node whose inorder Predecessor has to be found Output:-pointer to the inorder Predecessor of the node PTR Data structure:-Linked structure of Threaded binary tree Steps:- Pred=PTR->LCHILD If(PTR->LTAG=FALSE)then While(Pred->RTAG=FALSE) Pred=Pred->RCHILD EndWhile EndIF Return(pred) stop
  • 16. 16 Algorithm InorderTraversal_TBT:- Input:An inorder threaded binary tree Output:-Inorder traversal of the threaded binary tree Data structure:-Linked structure of Threaded binary tree having HEADER as the pointer to root Steps:- ptr=HEADER While(TRUE) do ptr=Inordersuccessor (ptr) If(ptr=HEADER) Exit Else Process (ptr) EndIF EndWhile stop
  • 17. Insertion of a node into a threaded binary tree 17
  • 18. 18 Add a new node at Right child node NO Right sub tree
  • 19. 19 Add a new node at Left child node NO Left sub tree
  • 20. 20 Add a new node at Right child node it has non empty Right sub tree
  • 21. 21 Add a new node at Leftt child node it has Left sub tree
  • 22. 22
  • 23. 23
  • 24. 24
  • 25. 25
  • 26. 26 Algorithm InsertInorder_TBT:- Inputs:- (a) HEADER, the pointer to the header node of the threaded binary tree (b) X is the data of a node after which the insertion has to be done (c) N is the data node to be interested Output:-If X exists in the tree then N is Inserted after X. Data structure:- Linked structure of threaded binary tree. Steps:- ptr=HEADER->LCHILD flag=FALSE While(ptr! =HEADER) and(flag=FALSE)
  • 27. 27 Xptr=ptr Flag=TRUE Else ptr=Inordersuccessor (ptr) Endif EndWhile If(flag=FALSE) then Print”Node doesn’t exist:no insertion “ Exit EndIf Read option=L/R for Left(L) or Right (R) child Nptr=GetNode (NODE) Case: option=‘L’ If(Xptr->LTAG=TRUE) then Nptr->LCHILD=Xptr->LCHILD Nptr->LTAG=TRUE Xptr->LCHILD=Nptr Xptr->LTAG=FALSE Nptr-RCHILD=Xptr Nptr->RTAG=TRUE
  • 28. Else Lptr=Xptr->LCHILD Xptr->LCHILD=Nptr Xptr->LTAG=FALSE Nptr->RCHILD=Xptr Nptr->RTAG=TRUE Nptr->LCHILD=Lptr Nptr->LTAG=FALSE ptr=InorderPredecessor (Xptr) ptr->RCHILD=Nptr End If Case: option=‘R’ If(Xptr->RTAG=TRUE) then Nptr->RCHILD=Xptr->RCHILD Nptr->RTAG=TRUE Xptr->RCHILD=Nptr Xptr->RTAG=FALSE Nptr->LCHILD=Xptr Nptr->LTAG=TRUE 28
  • 30. 30 Deletion of a node from a threaded binary tree
  • 31. 31 Delete a leaf node right or left
  • 32. 32 Delete of node having right sub tree or left sub tree
  • 33. 33 Delete of the node having both sub tree
  • 34. 34
  • 35. 35
  • 36. Algorithm Parent:- Input:- The pointer PTR of a node whose parent is to be found out. Output:-The pointer PARENT node of a given node PTR Data Structure:- Linked structure of a threaded binary tree. Steps:- varptr=PTR While(varptr, RTAG=0) do varptr=varptr->RCHILD End while pred=varptr->RCHILD If(pred->LCHILD=ptr)then parent=pred Else pred=pred ->LCHILD While (pred->LCHILD! =ptr) Pred=pred->RCHILD End while parent=pred End if Return(parent) Stop 36
  • 37. deleted Output:- The threaded binary search tree without a node PTR or it’s data content Data structure:-Linked structure of threaded binary tree Steps: parent=Parent (PTR) If(Ptr->LTAG=TRUE) and(PTR->RTAG=TRUE) then Case=1 Else If(PTR->LTAG=FALSE) and(PTR->RTAG=FALSE) then Case=3 Else Case=2 End if End if 37
  • 38. Do case=1 If(parent->RCHILD=PTR) then parent->RCHILD=PTR->RCHILD parent->RTAG=TRUE Else If(parent->LCHILD=PTR) then parent->LCHILD=PTR->LCHILD parent->LTAG=TRUE End if End if ReturnNode(PTR) Exit Endo Do case=2 // Find the sub tree of the node PTR If(PTR->RTAG=FALSE) then child=PTR->RCHILD Else If(PTR->LTAG=FALSE) then Child=PTR->LCHILD End if 38
  • 39. //modify the link field of parent If(parent->RCHILD=PTR) then parent->RCHILD=child Else If(parent->LCHILD=PTR) then parent->LCHILD=child Endif //modify the thread pointer in succeding node of PTR If(PTR->RTAG=FALSE) then Succ=inorder successor (PTR) Succ->LCHILD=parent Else If(PTR->LTAG=FALSE) then Pred=inorderPredecessor (PTR) Pred->RCHILD=parent Endif Endif Return node (PTR) Exit End Do 39
  • 40. Do case=3 Succ=Inorder successor (PTR) PTR->DATA=succ->DATA Delete inorder_TBST(succ) End Do Stop 40