n + 1 null links out of 2n total links
They replace the null links by pointers, called threads, to other nodes in the tree
To construct the threads, use the following rules:
(1) If ptr -> left_child is null, replace ptr -> left_child with a pointer to the inorder
predecessor of ptr.
(2) If ptr -> right_child is null, replace ptr -> right_child with a pointer to the
inorder successor of ptr.
root
A
C
D
H
B
E
I
F
G
typedef struct threaded_tree *threaded_pointer;
typedef struct threaded_tree {
short int left_thread;
threaded_pointer left_child;
char data;
threaded_pointer right_child;
short int right_thread;
};
Suppose ptr is an arbitrary node in a tree.
If ptr->left_thread = TRUE, then ptr->left_child contains a thread.
Otherwise, it contains a pointer to the left child.
right_thread is defined similarly.
Two dangling threads
left_child of leftmost leaf node and
right_child of rightmost leaf node
=> use a head node
left_thread
left_child
data
right_child
TRUE
right_thread
FALSE
An empty threaded tree
f
f
f
t
D
f
H
t
t
f
A
f
B
f
f
-
t
I
E
t
t
t
f
C
F
t
f
t
G
t
Inorder traversal of a threaded binary tree
For any node ptr, if ptr->right_thread = TRUE, the inorder successor of ptr
is ptr->right_child. Otherwise, it is the leftmost leaf node of ptr’s right
subtree.
We can find the inorder successor of any node in a threaded tree without
using a stack. To perform inorder tree traversal, we make repeated calls
to insucc
Inserting a node into a threaded binary tree as a right child when its
right subtree is empty
(1) change parent -> right-thread to FALSE
(2) set child -> left_thread and child -> right_thread to TRUE
(3) set child -> left_child to point to parent
(4) set child -> right_child to parent -> right_child
(5) change parent -> right_child to point to child
root
root
A
A
B
C
B
parent
D
parent
C
child
(a)
D
child
root
root
A
A
B
B
parent
parent
child
C
C
D
E
child
X
D
F
X
E
F
threaded_pointer insucc ( threaded_pointer tree )
{
/* find the inorder successor of tree in a treaded binary tree
*/
threaded_pointer temp ;
temp = tree->right_child ;
if ( !tree->right_thread )
while ( !temp->left_thread )
temp = temp->left_child ;
return temp ;
}
void tinorder ( threaded_pointer tree)
{
/* traverse the threaded binary tree inorder */
threaded_pointer temp = tree;
for ( ; ; ) {
temp = insucc (temp) ;
if (temp = tree) break ;
printf (“%3c”, temp->data) ;
}
}
void insert_right ( threaded_pointer parent, threaded_pointer child)
{
/* insert child as the right child of parent in a threaded binary tree*/
threaded_pointer temp ;
child->right_child = parent->right_child ;
child->right_thread = parent->right_thread ;
child->left_child = parent ;
child->left_thread = TRUE ;
parent->right_child = child ;
parent->right_thread = FALSE ;
if ( !child->right_thread ) {
temp = insucc(child) ;
temp->left_child = child ;
}
}
© Copyright 2026 Paperzz