0% found this document useful (0 votes)
46 views107 pages

10 - DS and Algorithm - Session - 14.pps

The document discusses threaded binary trees. A threaded binary tree utilizes empty child pointers of nodes to link nodes in inorder traversal sequence without recursion. This improves memory efficiency during traversal. The structure of each node contains extra left thread and right thread fields to indicate whether the child pointers are regular links or threads to inorder predecessors/successors. An algorithm is provided to locate the inorder successor of a given node by following child pointers or thread fields.

Uploaded by

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

10 - DS and Algorithm - Session - 14.pps

The document discusses threaded binary trees. A threaded binary tree utilizes empty child pointers of nodes to link nodes in inorder traversal sequence without recursion. This improves memory efficiency during traversal. The structure of each node contains extra left thread and right thread fields to indicate whether the child pointers are regular links or threads to inorder predecessors/successors. An algorithm is provided to locate the inorder successor of a given node by following child pointers or thread fields.

Uploaded by

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

Data Structures and Algorithms

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

Ver. 1.0

Session 14

Data Structures and Algorithms


Indexing
The
is anisexample
of an index.
Datafollowing
on disk files
usually organized
as records containing
several fields, one of which is often used as a key field.
Keyused
field to uniquely
Offset
The key field is
identify each record in a
36
0
file.
52
200
Indexing is one of the data access methods for accessing
24
400
records from the44disk files.
600
Indexing is implemented
through800a table called index.
40
Index consists of68two entries: 1000
59

Ver. 1.0

Key fields of all the records


55
Offset position72of each record

1200

35

1800

43

2000

1400
1600

Session 14

Data Structures and Algorithms


Indexing (Contd.)
To access
the recorda with
key
field 59,
thethese
indexindex
for
You
can implement
binary
search
treesearch
to store
this key value to retrieve its corresponding offset value,
values.
which
is 1200. enables faster search for a key value.
This approach
Read the record from the file starting from this offset
Key field
Offset
52
position.
36
0
52

200

24

400

44

600

40

800

68

1000

59

1200

55

1400

72

1600

35

1800

43

2000

Index
Ver. 1.0

36

68
44

24
40
35

59

72

55
43

Key Fields Stored in a Binary Search Tree


Session 14

Data Structures and Algorithms


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.

Ver. 1.0

Session 14

Data Structures and Algorithms


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.

Ver. 1.0

Session 14

Data Structures and Algorithms


Defining Threaded Binary Trees (Contd.)
In this case,
Consider
theitfollowing
would bebinary
good search
if thesetree.
NULL fields are
utilized
useful
Most of for
thesome
nodesother
in this
tree purpose.
hold a NULL value in their left
or right child fields.

. 65 .
. 40 .

30

. 72 .
. 50 .

69

80

60
Ver. 1.0

Session 14

Data Structures and Algorithms


Defining Threaded Binary Trees (Contd.)
Suchempty
The
a typeleft
of child
binaryfield
treeofisaknown
node can
as abe
threaded
used to binary
point totree.
its
inorder
A field
thatpredecessor.
holds the address of its inorder successor or
Similarly, the is
predecessor
empty
known
right
as child
thread.
field of a node can be used to
point to its inorder successor.

. 65 .

. 40 .

30

. 72 .

50

69

80

60
Ver. 1.0

Session 14

Data Structures and Algorithms


Defining Threaded Binary Trees (Contd.)
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.

. 65 .
. 40 .

30

. 72 .

50

69

80

60
Ver. 1.0

Session 14

Data Structures and Algorithms


Defining Threaded Binary Trees (Contd.)
Therefore,
The
right child
you of
take
theaheader
dummynode
nodealways
called points
the header
to itself.
node.
Header Node

. 65 .
. 40 .

30

. 72 .

50

69

80

60
Ver. 1.0

Session 14

Data Structures and Algorithms


Defining Threaded Binary Trees (Contd.)
The threaded binary tree is represented as the left child of
Header Node
the header node.

. 65 .
. 40 .

30

. 72 .

50

69

80

60
Ver. 1.0

Session 14

Data Structures and Algorithms


Defining Threaded Binary Trees (Contd.)
The left thread of node 30 and the right thread of node 80
point to the header node. Header Node

. 65 .
. 40 .
. 30

. 72 .

50

69

80

60
Ver. 1.0

Session 14

Data Structures and Algorithms


Just a minute
In a threaded binary tree, the right thread of a node points to
its inorder ___________, and the left thread points to its
inorder ____________.

Answer:
successor, predecessor

Ver. 1.0

Session 14

Data Structures and Algorithms


Representing a Threaded Binary Tree
structure
of athread
node in
a threaded
binary
a bit
The left
and right
fields
of a node
can tree
haveistwo
different from that of a normal binary tree.
values:
1: Indicates
normal tree,
link toeach
the child
node
Unlike
a normala binary
node
of a threaded binary
Indicatestwo
a thread
to the
inorder predecessor
or
tree 0:
contains
extra pointing
pieces of
information,
namely left
inorder
thread
and successor
right thread.

4631
Left
Address of
Thread Left Child

Ver. 1.0

Information
Data

2389
Address of
Right Child

Right
Thread

Session 14

Data Structures and Algorithms


Representing a Threaded Binary Tree (Contd.)
Various operations in a threaded binary tree are as follows:
Traversal
Search
Insert
Delete

Ver. 1.0

Session 14

Data Structures and Algorithms


Just a minute
How do you identify a root node in a threaded binary tree?

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.

Ver. 1.0

Session 14

Data Structures and Algorithms


Just a minute
How is the structure of a node of a threaded binary tree
different from that of a normal binary tree?

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.
Ver. 1.0

Session 14

Data Structures and Algorithms


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.

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
1.

Identify the node for which you want


to locate the inorder successor, and
mark it as currentNode.

Write
an algorithm
toinorder
locate the inorder successor of a node
Algorithm
to find the
in
a threaded
successor
of abinary
node tree.
in a threaded 2. If the right child of currentNode is a
thread:
binary tree.
a.
Mark the right child of
b.

Ver. 1.0

currentNode as successor.
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.

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
Header Node

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.

. 65 .
. 40 .

30

. 72 .
. 50 .

69

60

Ver. 1.0

80

Let us find the inorder successor of node 65

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
Header Node

currentNode

. 72 .
. 50 .

69

60

Ver. 1.0

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.

. 65 .

. 40 .

30

1.

80

Let us find the inorder successor of node 65.

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
Header Node

currentNode

. 72 .
. 50 .

69

60

Ver. 1.0

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.

. 65 .

. 40 .

30

1.

80

Let us find the inorder successor of node 65.

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
Header Node

currentNode

. 40 .

. 72 .
. 50 .

69

60

Ver. 1.0

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.

. 65 .
currentNode

30

1.

80

Let us find the inorder successor of node 65.

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
Header Node

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.

. 65 .
currentNode

. 40 .

30

. 72 .
. 50 .

69

60

Ver. 1.0

80

Let us find the inorder successor of node 65.

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
Header Node

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.

. 65 .
currentNode

. 40 .

30

. 72 .
. 50 .

69

80
currentNode

60

Ver. 1.0

Let us find the inorder successor of node 65.

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
Header Node

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.

. 65 .
. 40 .

30

. 72 .
. 50 .

69

80
currentNode

60

Ver. 1.0

Let us find the inorder successor of node 65.

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
Inorder successor
located

Header Node

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.

. 65 .
. 40 .

30

successor

. 50 .

. 72 .

69

80
currentNode

60

Ver. 1.0

Let us find the inorder successor of node 65.

Session 14

Data Structures and Algorithms


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

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
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.

Header Node

Algorithm to traverse a threaded


binary tree in inorder sequence.

. 65 .
. 40 .

. 72 .
. 50 .

30

69

60

Ver. 1.0

80

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
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.

Header Node

. 65 .
. 40 .

. 72 .
. 50 .

30

69

60

Ver. 1.0

80

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
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.

Header Node

currentNode

. 65 .

. 40 .

. 72 .
. 50 .

30

69

60

Ver. 1.0

80

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
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.

Header Node

currentNode

. 65 .

. 40 .

. 72 .
. 50 .

30

69

60

Ver. 1.0

80

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
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.

Header Node

currentNode

. 65 .

currentNode

. 40 .

. 72 .
. 50 .

30

69

60

Ver. 1.0

80

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
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.

Header Node

. 65 .
currentNode

. 40 .

. 72 .
. 50 .

30

69

60

Ver. 1.0

80

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
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.

Header Node

. 65 .
currentNode

. 40 .

. 72 .
. 50 .

30

69

80

currentNode

60

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
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.

Header Node

. 65 .
. 40 .

. 72 .
. 50 .

30

69

80

currentNode

60

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
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.

Header Node

. 65 .
. 40 .

. 72 .
. 50 .

30

69

80

currentNode

60

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
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.

Header Node

. 65 .
. 40 .

. 72 .
. 50 .

30

69

80

currentNode

60

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
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.

Header Node

. 65 .
currentNode

. 40 .

. 72 .
. 50 .

30

69

80

currentNode

60

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 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.

Header Node

. 65 .
currentNode

. 40 .

. 72 .
. 50 .

30

69

60

Ver. 1.0

80

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 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.

Header Node

. 65 .
currentNode

. 40 .

. 72 .
. 50 .

30

69

80

currentNode
60

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 40 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.

Header Node

. 65 .
. 40 .

. 72 .
. 50 .

30

69

80

currentNode
60

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 40 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.

Header Node

. 65 .
. 40 .

. 72 .
. 50 .

30

69

80

currentNode
60

Ver. 1.0

currentNode

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 40 50 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.

Header Node

. 65 .
. 40 .

. 72 .
. 50 .

30

69

60

Ver. 1.0

80

currentNode

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 40 50 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.

Header Node

currentNode

. 65 .

. 40 .

. 72 .
. 50 .

30

69

60

Ver. 1.0

80

currentNode

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 40 50 60 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.

Header Node

currentNode

. 65 .

. 40 .

. 72 .
. 50 .

30

69

60

Ver. 1.0

80

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 40 50 60 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.

Header Node

currentNode

. 65 .

. 40 .

. 72 .
. 50 .

30

69

80

currentNode
60

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 40 50 60 65 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.

Header Node

. 65 .
. 40 .

. 72 .
. 50 .

30

69

80

currentNode
60

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 40 50 60 65 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.

Header Node

. 65 .
currentNode

. 40 .

. 72 .
. 50 .

30

69

80

currentNode
60

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 40 50 60 65 69 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.

Header Node

. 65 .
currentNode

. 40 .

. 72 .
. 50 .

30

69

60

Ver. 1.0

80

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 40 50 60 65 69 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.

Header Node

. 65 .
currentNode

. 40 .

. 72 .
. 50 .

30

69

80
currentNode

60

Ver. 1.0

Session 14

Data Structures and Algorithms


Traversing a Threaded Binary Tree (Contd.)
30 40 50 60 65 69 72 80

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.

Header Node

Traversal complete

. 65 .

. 40 .

. 72 .
. 50 .

30

69

80
currentNode

60

Ver. 1.0

Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree
Insert an
operation
refers
to thethe
process
of inserting
newto be
Write
algorithm
to locate
position
of a new a
node
node at its
position.
inserted
in appropriate
a threaded binary
tree.
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.

Ver. 1.0

Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Node the
AlgorithmHeader
to locate
position of a new node in
.
a threaded binary
tree.

2.
3.
4.

. 65 .
. 40 .

. 72 .
. 50 .

30

69

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
. 65 .
. 40 .

. 72 .
. 50 .

30

69

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
currentNode

. 65 .

. 40 .

. 72 .
. 50 .

30

69

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node
parent

currentNode

2.
3.
4.

.
. 65 .

. 40 .

. 72 .
. 50 .

30

69

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node
parent

currentNode

2.
3.
4.

.
. 65 .

. 40 .

. 72 .
. 50 .

30

69

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node
parent

2.
3.
4.

parent
currentNode

. 65 .

. 40 .

. 72 .
. 50 .

30

69

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
parent
currentNode

. 65 .

. 40 .

. 72 .
. 50 .

30

69

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
parent
currentNode

. 65 .

currentNode

. 40 .

. 72 .
. 50 .

30

69

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
parent

. 65 .
currentNode

. 40 .

. 72 .
. 50 .

30

69

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
parent

. 65 .
currentNode
parent

. 40 .

. 72 .
. 50 .

30

69

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
. 65 .
currentNode
parent

. 40 .

. 72 .
. 50 .

30

69

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
. 65 .
currentNode
parent

. 40 .

. 72 .
. 50 .

30

69

currentNode

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
. 65 .
parent

. 40 .

. 72 .
. 50 .

30

69

currentNode

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
. 65 .
parent

. 40 .

. 72 .

parent

. 50 .

30

69

currentNode

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
. 65 .
. 40 .

. 72 .

parent

. 50 .

30

69

currentNode

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
. 65 .
. 40 .

. 72 .

parent

. 50 .

30

69

currentNode

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
. 65 .
. 40 .

. 72 .

parent

. 50 .

30

69

currentNode

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
. 65 .
. 40 .

. 72 .

parent

. 50 .

30

69

currentNode

60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
. 65 .
. 40 .

. 72 .

parent

. 50 .

30

69

currentNode
currentNode = NULL
60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert node 35
Header Node

2.
3.
4.

.
Parent node
located

. 65 .

. 40 .

. 72 .

parent

. 50 .

30

69

currentNode = NULL
60
Ver. 1.0

80

If the left child of the header node is a thread


pointing to itself:
a.
Mark head as parent.
b.
Exit.
Mark the left child of head as currentNode.
Mark head as parent.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
Once an
youalgorithm
locate the
the new
node
to be
inserted,
Write
toparent
insert aofnode
in an
empty
threaded
you need
to consider the following three cases:
binary
tree.
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

Ver. 1.0

Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
If the tree
Insert
Algorithm
65 is
to initially
insert aempty,
node inthen
a you
need to insert
thetree,
newwhich
nodeisas the
threaded
binary
left childempty.
of the header node.
initially

Header Node

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.

Session 14

Data Structures and Algorithms


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

Header Node

New Node

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.

Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)

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.

65
New Node

Ver. 1.0

Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)

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.

65
New Node

Ver. 1.0

Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)

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.

65
New Node

Ver. 1.0

Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)

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.

65
New Node

Ver. 1.0

Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)

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.

65
New Node

Ver. 1.0

Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)

Insertion complete

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.

65
New Node

Ver. 1.0

Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
Write an algorithm to insert a node in a non-empty threaded
binary tree.

Ver. 1.0

Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Header
Node
to
insert
a new

Algorithm
node
in a threaded binary tree.

2.
3.
4.

. 65 .
. 40 .

. 72 .
5.

30

. 50 .

69

60
Ver. 1.0

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert a node 75

2.

Header Node

3.

4.

. 65 .
. 40 .

. 72 .
5.

30

. 50 .

69

60
Ver. 1.0

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert a node 75

2.

Header Node

3.

4.

. 65 .
. 40 .

. 72 .
parent
5.

30

. 50 .

69

60
Ver. 1.0

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert a node 75

2.

Header Node

3.

4.

. 65 .
. 40 .

. 72 .
parent
5.

30

. 50 .

69

60
Ver. 1.0

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert a node 75

2.

Header Node

3.

4.

. 65 .
. 40 .

. 72 .
parent
5.

30

. 50 .

69

60
Ver. 1.0

80

75

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert a node 75

2.

Header Node

3.

4.

. 65 .
. 40 .

. 72 .
parent
5.

30

. 50 .

69

60
Ver. 1.0

80

75

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert a node 75

2.

Header Node

3.

4.

. 65 .
. 40 .

. 72 .
parent
5.

30

. 50 .

69

60
Ver. 1.0

80

75

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert a node 75

2.

Header Node

3.

4.

. 65 .
. 40 .

. 72 .
parent
5.

30

. 50 .

69

60
Ver. 1.0

80

75

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert a node 75

2.

Header Node

3.

4.

. 65 .
. 40 .

. 72 .
parent
5.

30

. 50 .

69

60
Ver. 1.0

80

75

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert a node 75

2.

Header Node

3.

4.

. 65 .
. 40 .

. 72 .
parent
5.

30

. 50 .

69

60
Ver. 1.0

80

75

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insert a node 75

2.

Header Node

3.

4.

. 65 .
. 40 .

. 72 .
parent
5.

30

. 50 .

60
Ver. 1.0

. 80

69

75

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Insertion complete

Insert a node 75

2.

Header Node

3.

4.

. 65 .
. 40 .

. 72 .
parent
5.

30

. 50 .

60
Ver. 1.0

. 80

69

75

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Header Node

Insert a node 35

Let us now insert a node as the


right child of its parent.

2.
3.
4.

. 65 .
. 40 .

. 72 .
5.

30

. 50 .

69

60

Ver. 1.0

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Header Node

Insert a node 35

2.
3.
4.

. 65 .
. 40 .

. 72 .

parent
5.

30

. 50 .

69

60

Ver. 1.0

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Header Node

Insert a node 35

2.
3.
4.

. 65 .
. 40 .

. 72 .

parent
5.

. 50 .

30

35

Ver. 1.0

69

60

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Header Node

Insert a node 35

2.
3.
4.

. 65 .
. 40 .

. 72 .

parent
5.

. 50 .

30

35

Ver. 1.0

69

60

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Header Node

Insert a node 35

2.
3.
4.

. 65 .
. 40 .

. 72 .

parent
5.

. 50 .

30

35

Ver. 1.0

69

60

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Header Node

Insert a node 35

2.
3.
4.

. 65 .
. 40 .

. 72 .

parent
5.

. 50 .

30

35

Ver. 1.0

69

60

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Header Node

Insert a node 35

2.
3.
4.

. 65 .
. 40 .

. 72 .

parent
5.

. 50 .

30

35

Ver. 1.0

69

60

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Header Node

Insert a node 35

2.
3.
4.

. 65 .
. 40 .

. 72 .

parent
5.

. 50 .

30

35

Ver. 1.0

69

60

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Header Node

Insert a node 35

2.
3.
4.

. 65 .
. 40 .

. 72 .

parent
5.

. 50 .

30

35

Ver. 1.0

69

60

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
1.

Header Node

Insert a node 35

2.
3.
4.

. 65 .
. 40 .

. 72 .

parent
5.

30

. 50 .

35

Ver. 1.0

69

60

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Inserting Nodes in a Threaded Binary Tree (Contd.)
Insertion complete

Insert a node 35

1.

Header Node

2.
3.
4.

. 65 .
. 40 .

. 72 .

parent
5.

30

. 50 .

35

Ver. 1.0

69

60

80

Locate the parent of the new node and


mark it as parent.
Allocate memory and assign value to
the data field of the new node.
Set the values of the left and right
thread fields of the new node as zero.
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.
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.
Session 14

Data Structures and Algorithms


Summary
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.

Ver. 1.0

Session 14

Data Structures and Algorithms


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.

Ver. 1.0

Session 14

You might also like