SlideShare a Scribd company logo
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

PDF
Binary tree
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
PPTX
Threaded Binary Tree
PPTX
AVL Tree in Data Structure
PDF
Graph Representation
PPTX
Data Structure and Algorithms Merge Sort
PPTX
Linked list
PDF
Binary tree
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Threaded Binary Tree
AVL Tree in Data Structure
Graph Representation
Data Structure and Algorithms Merge Sort
Linked list

What's hot (20)

PPT
1.1 binary tree
PDF
Bca data structures linked list mrs.sowmya jyothi
PPT
Binary search tree in data structures
PPT
Linked List
PPT
Binary search tree(bst)
PPTX
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
PPTX
Queue - Data Structure - Notes
PPTX
Linked list
PPTX
Stacks and Queue - Data Structures
PPTX
Binary Search Tree
PPSX
Data Structure (Tree)
PPTX
Linked list and its operations - Traversal
PPT
Data Structures with C Linked List
PPT
Operations on linked list
PPTX
Doubly linked list (animated)
PDF
UNIT I LINEAR DATA STRUCTURES – LIST
PPTX
Linear data structure concepts
PDF
Applications of stack
PPSX
PDF
Array data structure
1.1 binary tree
Bca data structures linked list mrs.sowmya jyothi
Binary search tree in data structures
Linked List
Binary search tree(bst)
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
Queue - Data Structure - Notes
Linked list
Stacks and Queue - Data Structures
Binary Search Tree
Data Structure (Tree)
Linked list and its operations - Traversal
Data Structures with C Linked List
Operations on linked list
Doubly linked list (animated)
UNIT I LINEAR DATA STRUCTURES – LIST
Linear data structure concepts
Applications of stack
Array data structure
Ad

Similar to Threaded binary tree (20)

PPTX
Data Structures
PPT
Unit8 C
PPTX
Binary tree and operations
PPTX
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
PPTX
Data Structure
PDF
Tree and binary tree
PPTX
Ds 111011055724-phpapp01
PDF
Data Structure Lecture 3 Linked Lists.pdf
PPTX
Binary tree
PPT
PPT
Binary trees
PDF
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
PPTX
Tree structure and its definitions with an example
PPTX
Linked List Data structure using C programming and all the detailed informat...
PPT
Trees
DOCX
Trees in data structrures
PDF
binarytrie.pdf
PPT
Introduction to data structure by anil dutt
PPTX
Unit 1 LINEAR DATA STRUCTURES
Data Structures
Unit8 C
Binary tree and operations
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structure
Tree and binary tree
Ds 111011055724-phpapp01
Data Structure Lecture 3 Linked Lists.pdf
Binary tree
Binary trees
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Tree structure and its definitions with an example
Linked List Data structure using C programming and all the detailed informat...
Trees
Trees in data structrures
binarytrie.pdf
Introduction to data structure by anil dutt
Unit 1 LINEAR DATA STRUCTURES
Ad

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Complications of Minimal Access Surgery at WLH
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Trump Administration's workforce development strategy
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
STATICS OF THE RIGID BODIES Hibbelers.pdf
Updated Idioms and Phrasal Verbs in English subject
Paper A Mock Exam 9_ Attempt review.pdf.
Complications of Minimal Access Surgery at WLH
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Orientation - ARALprogram of Deped to the Parents.pptx
Final Presentation General Medicine 03-08-2024.pptx
01-Introduction-to-Information-Management.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Cell Types and Its function , kingdom of life
Trump Administration's workforce development strategy
Anesthesia in Laparoscopic Surgery in India
Yogi Goddess Pres Conference Studio Updates
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

Threaded binary tree

  • 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