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
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
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)
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
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