0% found this document useful (0 votes)
18 views5 pages

Dsa Assignment No 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Dsa Assignment No 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment -5

Title:
Convert given binary tree into threaded binary tree. Analyze time and space complexity of the
algorithm.

Learning Objective:

To understand the basic concept of Non-Linear Data Structure “threaded binary tree” and its use
in Data structure.

Learning Outcome:

To convert the Binary Tree into threaded binary tree and analyze its time and space complexity.

Theory –

Threaded Binary Tree

Node Structure in Threaded Binary Tree :-


L bit L link Data R link R bit
1) L bit and R bit are either 0 or 1.
2) If L bit is 1 means there is a left child and if 0 means there is no left child.
3) If R bit is 1 means there is a right child and if 0 means there is no right child.
4) L link points to left child node and R link points right child node.
5) Data is the actual element or the value.

 Idea of Threaded Binary Tree is to make inorder traversal faster and do it without stack and without
recursion.
 In a simple threaded binary tree, the NULL right pointers are used to store inorder successor.
Wherever a right pointer is NULL, it is used to store inorder successor.
 Inorder traversal of a Binary tree is either be done using recursion or with the use of a auxiliary stack.

There are two types of Threaded Binary Tree :

1) Single Threaded:
Where a NULL right pointer is made to point to the inorder successor Following diagram shows an
example Single Threaded Binary Tree. The dotted lines represent threads.
Inorder Traversal = 4 2 5 1 6 3 7

1 -999 0

1 1 0

1 3 1
1 2 1

0 4 0 0 5 0 0 6 0 0 7 0

2 ) Double Threaded:
Where both left and right NULL pointers are made to point to inorder predecessor and inorder
successor, respectively. The predecessor threads are useful for reverse inorder traversal and
postorder traversal.
The threads are also useful for fast accessing ancestors of a node.

1 -999 0

1 1 0

1 3 1
1 2 1

0 4 0 0 5 0 0 6 0 0 7 0

Representation of a Threaded Node :


Following is C++ Representation of a double threaded node.

struct Node
{
int data;
Node *left, *right;
bool lbit, rbit;
}

Conversion of Binary Tree to Threaded Binary Tree


Initialize pointers: Traverse the binary tree in-order, and for each node, if its left child is null, make it
point to the predecessor (the previous node in in-order traversal). Similarly, if the right child is null, make it
point to the successor (the next node in in-order traversal).
Threading process: While traversing in-order, if the left child of a node is null, link it to its in-order
predecessor. If the right child is null, link it to its in-order successor.
Final threading: Finally, make the left pointer of the leftmost node point to the in-order predecessor that is
dummy node, and make the right pointer of the rightmost node point to the in-order successor that is dummy
node.

Time Complexity Analysis:

● Traversing the binary tree to thread the nodes takes O(n) time, where n is the number of nodes in the tree.
● Finding the leftmost and rightmost nodes takes O(n) time in total.
● Overall, the time complexity of the algorithm is O(n).

Space Complexity Analysis:

● The space complexity primarily depends on the recursion stack during in-order traversal, which could be
O(h), where h is the height of the tree. In the worst case (for skewed trees), it could be O(n).
● Additionally, the algorithm uses a constant amount of extra space for auxiliary variables.
● Thus, the overall space complexity is O(n).

Expected Outcome:
The binary tree into threaded binary tree in one single traversal with no extra space required.

Conclusion: Able to convert the Binary Tree into threaded binary tree and analyze its time and
space complexity.

You might also like