0% found this document useful (0 votes)
1K views

Threaded Binary Trees

A threaded binary tree is a binary tree that uses null pointers to link nodes inorder. Each node's right pointer either points to its inorder successor or is null, and each left pointer either points to its inorder predecessor or is null. This allows for faster inorder traversal without recursion or a stack. There are two types: single threaded uses one direction of linking, while double threaded uses both left and right pointers to link nodes inorder. Key operations on a single threaded tree are insertion, which modifies pointers to link a new node at the proper place, and traversal, which uses right pointers to iteratively move through nodes inorder without additional data structures.

Uploaded by

Prakash Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Threaded Binary Trees

A threaded binary tree is a binary tree that uses null pointers to link nodes inorder. Each node's right pointer either points to its inorder successor or is null, and each left pointer either points to its inorder predecessor or is null. This allows for faster inorder traversal without recursion or a stack. There are two types: single threaded uses one direction of linking, while double threaded uses both left and right pointers to link nodes inorder. Key operations on a single threaded tree are insertion, which modifies pointers to link a new node at the proper place, and traversal, which uses right pointers to iteratively move through nodes inorder without additional data structures.

Uploaded by

Prakash Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Threaded Binary Trees

A binary tree is threaded by making all right child pointers that would normally be null point to the
inorder successor of the node (if it exists), and all left child pointers that would normally be null point to
the inorder predecessor of the node.

Use Threaded 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 inorder traversals.
 Threaded binary tree makes the tree traversal faster since we do not need stack or recur-
sion for traversal

Types of threaded binary trees:

 Single Threaded: each node is threaded towards either the in-order predecessor or suc-
cessor (left or right) means all right null pointers will point to inorder successor OR all
left null pointers will point to inorder predecessor.
 Double threaded: each node is threaded towards both the in-order predecessor and suc-
cessor (left and right) means all right null pointers will point to inorder successor AND
all left null pointers will point to inorder predecessor.

Operations on single threaded binary tree :-

The following are the two primary operations in single threaded binary tree

1. Insert node into tree


2. Print or traverse the tree.( here we will see the advantage of threaded tree)
Insert():

The insert operation will be quite similar to Insert operation in Binary search tree with few
modifications.

 To insert a node our first task is to find the place to insert the node.
 Take current = root .
 start from the current and compare root.data with n.
 Always keep track of parent node while moving left or right.
 if current.data is greater than n that means we go to the left of the root, if after moving to left,
the current = null then we have found the place where we will insert the new node. Add the
new node to the left of parent node and make the right pointer points to parent node and right-
Thread = true for new node.


 if current.data is smaller than n that means we need to go to the right of the root, while going
into the right subtree, check rightThread for current node, means right thread is provided and
points to the in order successor, if rightThread = false then and current reaches to null, just
insert the new node else if rightThread = true then we need to detach the right pointer (store
the reference, new node right reference will be point to it) of current node and make it point to
the new node and make the right reference point to stored reference. (See image and code for
better understanding)
Traverse():

traversing the threaded binary tree will be quite easy, no need of any recursion or any stack for
storing the node. Just go to the left most node and start traversing the tree using right pointer and
whenever rightThread = false again go to the left most node in right subtree.

You might also like