Sie sind auf Seite 1von 107

Data Structures and Algorithms

Session 14 Ver. 1.0


Objectives
In this session, you will learn to:
Apply trees to solve programming problems
Implement a threaded binary tree


Data Structures and Algorithms
Session 14 Ver. 1.0
Data on disk files is usually organized as records containing
several fields, one of which is often used as a key field.
The key field is used to uniquely identify each record in a
file.
Indexing is one of the data access methods for accessing
records from the disk files.
Indexing is implemented through a table called index.
Index consists of two entries:
Key fields of all the records
Offset position of each record


The following is an example of an index.
Indexing
Key field Offset
36 0
52 200
24 400
44 600
40 800
68 1000
59 1200
55 1400
72 1600
35 1800
43 2000
Data Structures and Algorithms
Session 14 Ver. 1.0
Indexing (Contd.)
To access the record with key field 59, search the index for
this key value to retrieve its corresponding offset value,
which is 1200.
Read the record from the file starting from this offset
position.

You can implement a binary search tree to store these index
values.
This approach enables faster search for a key value.

52
36
68
24
44
40
59 72
55
43 35
Key field Offset
36 0
52 200
24 400
44 600
40 800
68 1000
59 1200
55 1400
72 1600
35 1800
43 2000
Index Key Fields Stored in a Binary Search Tree
Data Structures and Algorithms
Session 14 Ver. 1.0
Implementing a Threaded Binary Trees
One of the common operations on a binary tree is traversal.
In a linked representation of a binary tree, traversal is
usually implemented through recursion.
As a result, a stack is maintained in the memory.
If the tree is huge, implementing recursion to traverse the
tree would require a lot of memory space.
In the absence of sufficient memory space, implementing
recursion can lead to a memory leak.

Data Structures and Algorithms
Session 14 Ver. 1.0
Defining Threaded Binary Trees
In such a case, it would be good if you have some
mechanism by which you can traverse the tree without
implementing recursion.
You can solve this problem by implementing a threaded
binary tree.
In a binary search tree, there are many nodes that have an
empty left child or empty right child or both.
You can utilize these fields in such a way so that the empty
left child of a node points to its inorder predecessor and
empty right child of the node points to its inorder successor.
Data Structures and Algorithms
Session 14 Ver. 1.0
Consider the following binary search tree.
Most of the nodes in this tree hold a NULL value in their left
or right child fields.
30 50 80
60
72
69
40
. . .
65
.
.
. .
.
Defining Threaded Binary Trees (Contd.)
In this case, it would be good if these NULL fields are
utilized for some other useful purpose.
Data Structures and Algorithms
Session 14 Ver. 1.0
The empty left child field of a node can be used to point to
its inorder predecessor.
Similarly, the empty right child field of a node can be used to
point to its inorder successor.
30 50 80
60
72
69
40
. . . .
.
Defining Threaded Binary Trees (Contd.)
Such a type of binary tree is known as a threaded binary tree.
A field that holds the address of its inorder successor or
predecessor is known as thread.
65
. .
Data Structures and Algorithms
Session 14 Ver. 1.0

Node 30 does not have an inorder predecessor because it is
the first node to be traversed in inorder sequence.
Similarly, node 80 does not have an inorder successor.
Defining Threaded Binary Trees (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
.
.
Data Structures and Algorithms
Session 14 Ver. 1.0
Therefore, you take a dummy node called the header node.
Header Node
Defining Threaded Binary Trees (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
.
.


The right child of the header node always points to itself.
Data Structures and Algorithms
Session 14 Ver. 1.0

The threaded binary tree is represented as the left child of
the header node.
Header Node
Defining Threaded Binary Trees (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
.
.


Data Structures and Algorithms
Session 14 Ver. 1.0

The left thread of node 30 and the right thread of node 80
point to the header node.
Defining Threaded Binary Trees (Contd.)
Header Node
30 50 80
60
72
69
40
. . .
65
.
.
.
.
. .
Data Structures and Algorithms
Session 14 Ver. 1.0
In a threaded binary tree, the right thread of a node points to
its inorder ___________, and the left thread points to its
inorder ____________.
Just a minute
Answer:
successor, predecessor
Data Structures and Algorithms
Session 14 Ver. 1.0


Representing a Threaded Binary Tree
The structure of a node in a threaded binary tree is a bit
different from that of a normal binary tree.
Unlike a normal binary tree, each node of a threaded binary
tree contains two extra pieces of information, namely left
thread and right thread.
Information
4631 2389
Address of
Right Child
Address of
Left Child
Data Left
Thread
Right
Thread
The left and right thread fields of a node can have two
values:
1: Indicates a normal link to the child node
0: Indicates a thread pointing to the inorder predecessor or
inorder successor
Data Structures and Algorithms
Session 14 Ver. 1.0

Various operations in a threaded binary tree are as follows:
Traversal
Search
Insert
Delete


Representing a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
How do you identify a root node in a threaded binary tree?
Just a minute
Answer:
In a threaded binary tree, the root node is identified as the left
child of the header node. If the tree is empty, the left child of
the header node becomes a thread pointing to itself.
Data Structures and Algorithms
Session 14 Ver. 1.0
How is the structure of a node of a threaded binary tree
different from that of a normal binary tree?
Just a minute
Answer:
Each node in a threaded binary tree holds two extra pieces of
information known as left thread and right thread. The value of
these two fields indicates whether the left/right child field of a
node contains a link to a child node or a thread to its inorder
predecessor/successor.
Data Structures and Algorithms
Session 14 Ver. 1.0


Traversing a Threaded Binary Tree
To traverse a threaded binary tree in inorder sequence, you
need to determine the inorder successor of a node at each
step.


Data Structures and Algorithms
Session 14 Ver. 1.0

Write an algorithm to locate the inorder successor of a node
in a threaded binary tree.


Algorithm to find the inorder
successor of a node in a threaded
binary tree.


1. Identify the node for which you want
to locate the inorder successor, and
mark it as currentNode.

2. If the right child of currentNode is a
thread:
a. Mark the right child of
currentNode as successor.
b. Exit.

3. Make currentNode point to its right
child.

4. Repeat step 5 until left child of
currentNode becomes a thread.

5. Make currentNode point to its left
child.

6. Mark currentNode as successor.
Traversing a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0


30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Let us find the inorder successor of node 65
1. Identify the node for which you want
to locate the inorder successor, and
mark it as currentNode.

2. If the right child of currentNode is a
thread:
a. Mark the right child of
currentNode as successor.
b. Exit.

3. Make currentNode point to its right
child.

4. Repeat step 5 until left child of
currentNode becomes a thread.

5. Make currentNode point to its left
child.

6. Mark currentNode as successor.
Traversing a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0


30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Let us find the inorder successor of node 65.
currentNode
1. Identify the node for which you want
to locate the inorder successor, and
mark it as currentNode.

2. If the right child of currentNode is a
thread:
a. Mark the right child of
currentNode as successor.
b. Exit.

3. Make currentNode point to its right
child.

4. Repeat step 5 until left child of
currentNode becomes a thread.

5. Make currentNode point to its left
child.

6. Mark currentNode as successor.
Traversing a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0


30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
Let us find the inorder successor of node 65.
Traversing a Threaded Binary Tree (Contd.)
1. Identify the node for which you want
to locate the inorder successor, and
mark it as currentNode.

2. If the right child of currentNode is a
thread:
a. Mark the right child of
currentNode as successor.
b. Exit.

3. Make currentNode point to its right
child.

4. Repeat step 5 until left child of
currentNode becomes a thread.

5. Make currentNode point to its left
child.

6. Mark currentNode as successor.
Data Structures and Algorithms
Session 14 Ver. 1.0


30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
currentNode
Let us find the inorder successor of node 65.
Traversing a Threaded Binary Tree (Contd.)
1. Identify the node for which you want
to locate the inorder successor, and
mark it as currentNode.

2. If the right child of currentNode is a
thread:
a. Mark the right child of
currentNode as successor.
b. Exit.

3. Make currentNode point to its right
child.

4. Repeat step 5 until left child of
currentNode becomes a thread.

5. Make currentNode point to its left
child.

6. Mark currentNode as successor.
Data Structures and Algorithms
Session 14 Ver. 1.0


30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
Let us find the inorder successor of node 65.
Traversing a Threaded Binary Tree (Contd.)
1. Identify the node for which you want
to locate the inorder successor, and
mark it as currentNode.

2. If the right child of currentNode is a
thread:
a. Mark the right child of
currentNode as successor.
b. Exit.

3. Make currentNode point to its right
child.

4. Repeat step 5 until left child of
currentNode becomes a thread.

5. Make currentNode point to its left
child.

6. Mark currentNode as successor.
Data Structures and Algorithms
Session 14 Ver. 1.0


30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
currentNode
Let us find the inorder successor of node 65.
Traversing a Threaded Binary Tree (Contd.)
1. Identify the node for which you want
to locate the inorder successor, and
mark it as currentNode.

2. If the right child of currentNode is a
thread:
a. Mark the right child of
currentNode as successor.
b. Exit.

3. Make currentNode point to its right
child.

4. Repeat step 5 until left child of
currentNode becomes a thread.

5. Make currentNode point to its left
child.

6. Mark currentNode as successor.
Data Structures and Algorithms
Session 14 Ver. 1.0


30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Let us find the inorder successor of node 65.
currentNode
Traversing a Threaded Binary Tree (Contd.)
1. Identify the node for which you want
to locate the inorder successor, and
mark it as currentNode.

2. If the right child of currentNode is a
thread:
a. Mark the right child of
currentNode as successor.
b. Exit.

3. Make currentNode point to its right
child.

4. Repeat step 5 until left child of
currentNode becomes a thread.

5. Make currentNode point to its left
child.

6. Mark currentNode as successor.
Data Structures and Algorithms
Session 14 Ver. 1.0


30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
successor
Inorder successor
located
Let us find the inorder successor of node 65.
currentNode
Traversing a Threaded Binary Tree (Contd.)
1. Identify the node for which you want
to locate the inorder successor, and
mark it as currentNode.

2. If the right child of currentNode is a
thread:
a. Mark the right child of
currentNode as successor.
b. Exit.

3. Make currentNode point to its right
child.

4. Repeat step 5 until left child of
currentNode becomes a thread.

5. Make currentNode point to its left
child.

6. Mark currentNode as successor.
Data Structures and Algorithms
Session 14 Ver. 1.0

Write an algorithm to traverse a threaded binary tree in
inorder sequence.


Traversing a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0


Traversing a Threaded Binary Tree (Contd.)
Algorithm to traverse a threaded
binary tree in inorder sequence.


1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Data Structures and Algorithms
Session 14 Ver. 1.0


Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0


Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0


Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0


Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0


Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0


Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
30
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
30
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
currentNode
30
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30
currentNode
40
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30
currentNode
40
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30 40
currentNode
50
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30 40
currentNode
50
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30 40 50
currentNode
60
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30 40 50
currentNode
60
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30 40 50 60
currentNode
65
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30 40 50 60
currentNode
65
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30 40 50 60 65
currentNode
69
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30 40 50 60 65
currentNode
69
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30 40 50 60 65 69
currentNode
72
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30 40 50 60 65 69
currentNode
72
currentNode
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0
Traversing a Threaded Binary Tree (Contd.)
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
30 40 50 60 65 69 72
currentNode
80
Traversal complete
1. If the left child of the header node
is a thread pointing to itself:
a. Display Tree is empty.
b. Exit.

2. Mark the left child of the header
node as currentNode.

3. Repeat step 4 until the left child of
currentNode becomes a thread.

4. Make currentNode point to its left
child.

5. Display the information held by
currentNode.

6. Repeat steps 7 and 8 until right
child of currentNode points to the
header node.

7. Find the inorder successor of
currentNode, and mark the inorder
successor as currentNode.

8. Display the information held by the
currentNode.
Data Structures and Algorithms
Session 14 Ver. 1.0

Insert operation refers to the process of inserting a new
node at its appropriate position.
To implement an insert operation in a threaded binary tree,
you first need to locate the position for the new node to be
inserted.
For this, you first need to implement a search operation.
Inserting Nodes in a Threaded Binary Tree
Write an algorithm to locate the position of a new node to be
inserted in a threaded binary tree.


Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Algorithm to locate the
position of a new node in
a threaded binary tree.


Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
parent
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
currentNode
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
parent
currentNode
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
parent
currentNode
parent
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
currentNode
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
parent
currentNode
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
parent
currentNode
parent
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
currentNode
parent
currentNode = NULL
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert node 35
parent
currentNode = NULL
Parent node
located
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. If the left child of the header node is a thread
pointing to itself:
a. Mark head as parent.
b. Exit.
2. Mark the left child of head as currentNode.
3. Mark head as parent.
4. Repeat steps a, b, c, d, and e until currentNode
becomes NULL:
a. Mark currentNode as parent.
b. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a normal link:
i. Make currentNode point to its left
child and go to step 4.
c. If the value of the new node is less than
that of currentNode and the left child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
d. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a normal link:
i. Make currentNode point to its right
child and go to step 4.
e. If the value of the new node is greater than
that of currentNode and the right child of
currentNode is a thread:
i. Mark currentNode as NULL and go
to step 4.
Data Structures and Algorithms
Session 14 Ver. 1.0

Once you locate the parent of the new node to be inserted,
you need to consider the following three cases:
Tree is empty (if parent is the header node)
New node is to be inserted as the left child of its parent
New node is to be inserted as the right child of its parent
Write an algorithm to insert a node in an empty threaded
binary tree.


Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0

If the tree is initially empty, then you
need to insert the new node as the
left child of the header node.

1. Allocate memory for the new
node.

2. Assign value to the data field of
the new node.

3. Set the values of the left and right
thread fields of the new node as
zero.

4. Set the value of the left thread
field of the head node as one.

5. Make the left child field of the
header node as a link pointing to
the new node.

6. Make the left child field of the new
node as a thread pointing to the
header node.

7. Make the right child field of the
new node as a thread pointing to
the header node.
Header Node
Algorithm to insert a node in a
threaded binary tree, which is
initially empty.

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert 65

Data Structures and Algorithms
Session 14 Ver. 1.0


1. Allocate memory for the new
node.

2. Assign value to the data field of
the new node.

3. Set the values of the left and right
thread fields of the new node as
zero.

4. Set the value of the left thread
field of the head node as one.

5. Make the left child field of the
header node as a link pointing to
the new node.

6. Make the left child field of the new
node as a thread pointing to the
header node.

7. Make the right child field of the
new node as a thread pointing to
the header node.
Header Node
New Node
Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert 65

Data Structures and Algorithms
Session 14 Ver. 1.0


1. Allocate memory for the new
node.

2. Assign value to the data field of
the new node.

3. Set the values of the left and right
thread fields of the new node as
zero.

4. Set the value of the left thread
field of the head node as one.

5. Make the left child field of the
header node as a link pointing to
the new node.

6. Make the left child field of the new
node as a thread pointing to the
header node.

7. Make the right child field of the
new node as a thread pointing to
the header node.
Header Node
New Node
65
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
1. Allocate memory for the new
node.

2. Assign value to the data field of
the new node.

3. Set the values of the left and right
thread fields of the new node as
zero.

4. Set the value of the left thread
field of the head node as one.

5. Make the left child field of the
header node as a link pointing to
the new node.

6. Make the left child field of the new
node as a thread pointing to the
header node.

7. Make the right child field of the
new node as a thread pointing to
the header node.
Header Node
New Node
65
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
1. Allocate memory for the new
node.

2. Assign value to the data field of
the new node.

3. Set the values of the left and right
thread fields of the new node as
zero.

4. Set the value of the left thread
field of the head node as one.

5. Make the left child field of the
header node as a link pointing to
the new node.

6. Make the left child field of the new
node as a thread pointing to the
header node.

7. Make the right child field of the
new node as a thread pointing to
the header node.
Header Node
New Node
65
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
1. Allocate memory for the new
node.

2. Assign value to the data field of
the new node.

3. Set the values of the left and right
thread fields of the new node as
zero.

4. Set the value of the left thread
field of the head node as one.

5. Make the left child field of the
header node as a link pointing to
the new node.

6. Make the left child field of the new
node as a thread pointing to the
header node.

7. Make the right child field of the
new node as a thread pointing to
the header node.
Header Node
New Node
65
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
1. Allocate memory for the new
node.

2. Assign value to the data field of
the new node.

3. Set the values of the left and right
thread fields of the new node as
zero.

4. Set the value of the left thread
field of the head node as one.

5. Make the left child field of the
header node as a link pointing to
the new node.

6. Make the left child field of the new
node as a thread pointing to the
header node.

7. Make the right child field of the
new node as a thread pointing to
the header node.
Header Node
New Node
65
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
1. Allocate memory for the new
node.

2. Assign value to the data field of
the new node.

3. Set the values of the left and right
thread fields of the new node as
zero.

4. Set the value of the left thread
field of the head node as one.

5. Make the left child field of the
header node as a link pointing to
the new node.

6. Make the left child field of the new
node as a thread pointing to the
header node.

7. Make the right child field of the
new node as a thread pointing to
the header node.
Header Node
New Node
65
Insertion complete
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
Write an algorithm to insert a node in a non-empty threaded
binary tree.


Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
Algorithm to insert a new node
in a threaded binary tree.
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Inserting Nodes in a Threaded Binary Tree (Contd.)
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert a node 75
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert a node 75
parent
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert a node 75
parent
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert a node 75
75
parent
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert a node 75
75
parent
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert a node 75
75
parent
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert a node 75
75
parent
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert a node 75
75
parent
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert a node 75
75
parent
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert a node 75
75
parent
.
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
.
Insert a node 75
75
parent
.
Insertion complete
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Let us now insert a node as the
right child of its parent.
Insert a node 35
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Insert a node 35
parent
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Insert a node 35
parent
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
35
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Insert a node 35
parent
35
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Insert a node 35
parent
35
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Insert a node 35
parent
35
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Insert a node 35
parent
35
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Insert a node 35
parent
35
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Insert a node 35
parent
35
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Insert a node 35
parent
35
.
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
30 50 80
60
72
69
40
. . .
65
.
.
Header Node
. .
.
Insert a node 35
parent
35
Insertion complete
.
1. Locate the parent of the new node and
mark it as parent.
2. Allocate memory and assign value to
the data field of the new node.
3. Set the values of the left and right
thread fields of the new node as zero.
4. If the value of the new node is less
than that of parent:
a. Make left child field of new node
point to the left child of parent.
b. Make right child field of new
node point to parent.
c. Set the value of the left thread
field of parent as one.
d. Make left child field of parent
point to the new node.
e. Exit.
5. If the value of the node is greater than
that of parent:
a. Make left child field of new node
point to parent.
b. Make right child field of new
node point to the right child of
parent.
c. Set the value of right thread
field of parent as one.
d. Make right child field of parent
point to the new node.
e. Exit.
Inserting Nodes in a Threaded Binary Tree (Contd.)
Data Structures and Algorithms
Session 14 Ver. 1.0
In this session, you learned that:
Binary search trees can be used to implement indexing.
A threaded binary tree is a binary tree in which a node with an
empty left child stores the address of its inorder predecessor
and the empty right child stores the address of its inorder
successor.
In a threaded binary tree, the left and right child field of a
node, which holds the address of its inorder predecessor and
inorder successor, respectively, is called a thread.
You can traverse a threaded binary tree without implementing
recursion.
A threaded binary tree is represented as the left subtree of the
header node.


Summary
Data Structures and Algorithms
Session 14 Ver. 1.0
Summary (Contd.)
In contrast to a normal binary tree, each node in a threaded
binary tree consists of two additional fields to keep track of
whether the left and right child field of a node is a thread or a
link.

Das könnte Ihnen auch gefallen