threaded binary trees
TRANSCRIPT
-
7/30/2019 Threaded Binary Trees
1/18
Threaded Binary Trees
-
7/30/2019 Threaded Binary Trees
2/18
Demerits of Binary Trees
Binary trees have a lot of wasted space: the leaf
nodes each have 2 null pointers
We can use these pointers to help us in inordertraversals
We have the pointers reference the next node in
an inorder traversal; called threads
We need to know if a pointer is an actual link or a
thread, so we keep a boolean for each pointer
-
7/30/2019 Threaded Binary Trees
3/18
Need for Threaded Binary Tree
A binary tree with n nodes need 2n pointers out
of which (n+1) are null pointers
A.J. Perlis and C.Thomson devised a method toutilize these (n+1) null pointers as threads
Threads that take place of a left child pointer
indicate inorder predecessor and right child
pointer lead to inorder successor
-
7/30/2019 Threaded Binary Trees
4/18
Definition
Threaded binary tree is the left subtree of a root
node whose right child pointer points to itself.
-
7/30/2019 Threaded Binary Trees
5/18
Data Structures for Threaded BT
left_thread left_child data right_child right_t
TRUE FALSE
FALSE: childTRUE: thread
-
7/30/2019 Threaded Binary Trees
6/18
One Way Threading
Thread appears only on the RLINK of a node,
pointing to the inorder successor of the node
8
75
3
11
13
1
6
9
-
7/30/2019 Threaded Binary Trees
7/18
Two Way Threading
Thread appears in both LLINK and RLINK of a
node, pointing to the inorder predecessor and
inorder successor respectively.
8
75
3
11
13
1
6
9
-
7/30/2019 Threaded Binary Trees
8/18
8
Threaded Tree Traversal
We start at the leftmost node in the tree, print it, and
follow its right thread
If we follow a thread to the right, we output the node
and continue to its right
If we follow a link to the right, we go to the leftmost
node, print it, and continue
-
7/30/2019 Threaded Binary Trees
9/18
9
Threaded Tree Traversal
8
75
3
11
13
1
6
9
Start at leftmost node, print it
Output1
-
7/30/2019 Threaded Binary Trees
10/18
10
Threaded Tree Traversal
8
75
3
11
13
1
6
9
Follow thread to right, print node
Output1
3
-
7/30/2019 Threaded Binary Trees
11/18
11
Threaded Tree Traversal
8
75
3
11
13
1
6
9Follow link to right, go to
leftmost node and print
Output1
3
5
-
7/30/2019 Threaded Binary Trees
12/18
12
Threaded Tree Traversal
8
75
3
11
13
1
6
9
Follow thread to right, print node
Output1
3
5
6
-
7/30/2019 Threaded Binary Trees
13/18
13
Threaded Tree Traversal
8
75
3
11
13
1
6
9Follow link to right, go to
leftmost node and print
Output1
3
5
67
-
7/30/2019 Threaded Binary Trees
14/18
14
Threaded Tree Traversal
8
75
3
11
13
1
6
9
Follow thread to right, print node
Output1
3
5
67
8
-
7/30/2019 Threaded Binary Trees
15/18
15
Threaded Tree Traversal
8
75
3
11
13
1
6
9Follow link to right, go to
leftmost node and print
Output1
3
5
67
8
9
-
7/30/2019 Threaded Binary Trees
16/18
16
Threaded Tree Traversal
8
75
3
11
13
1
6
9
Follow thread to right, print node
Output1
3
5
67
8
9
11
-
7/30/2019 Threaded Binary Trees
17/18
17
Threaded Tree Traversal
8
75
3
11
13
1
6
9Follow link to right, go to
leftmost node and print
Output1
3
5
67
8
9
11
13
-
7/30/2019 Threaded Binary Trees
18/18
Inorder Traversal of Threaded Binary Tree
void threadedinorder(Root P)
{
do{
if P TRPOINT = = True then
P = P RLINK;
else
P = P RLINK;
while(P TLPOINT ! = TRUE)
P = P LLINK;
if(P!=ROOT)
print(p data)
}while(P = = ROOT);
}