You are on page 1of 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 on disk is an usually of an index. Datafollowingfiles isexampleorganized as records containing several fields, one of which is often used as a key field. Key field Offset The key field is used to uniquely identify each record in a 36 0 file. 52 200 Indexing is one of the data access methods for accessing 24 400 records from the disk files. 44 600 Indexing is implemented through a table called index. 40 800 68 Index consists of two entries: 1000
Key fields of all the records 55 Offset position of each record 72
35 43 59 1200 1400 1600 1800 2000

Ver. 1.0

Session 14

## Data Structures and Algorithms

Indexing (Contd.) To access the record with key field 59, search the index for You can implement a binary search tree to store these index 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 52 Key field Offset position.
36 52 24 44 40 68 59 55 72 35 43 0 200 400 600 800 1000 1200 1400 1600 1800 2000

36
24 40 35 43 44 55 59

68
72

Index
Ver. 1.0

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, would be good if these NULL Consider theitfollowing binary search tree. fields are utilized for some other useful purpose. Most of the nodes in this tree hold a NULL value in their left or right child fields.

. 65 . . 40 . . 72 .

30

. 50 .

69

80

60
Ver. 1.0

Session 14

## Data Structures and Algorithms

Defining Threaded Binary Trees (Contd.)

Such a type of binary tree is node as be used to point tree. The empty left child field of aknowncan a threaded binaryto itsfield thatpredecessor. A inorder holds the address of its inorder successor or predecessor empty right thread. Similarly, the is known as child field of a node can be used to point to its inorder successor.

. 65 . . 40 . . 72 .

30

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

30

50

69

80

60
Ver. 1.0

Session 14

## Data Structures and Algorithms

Defining Threaded Binary Trees (Contd.)

The right child take dummy node called the header node. Therefore, you of theaheader node always points to itself.
Header Node

. 65 . . 40 . . 72 .

30

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

30

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

. 30

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 a node fields of a node can have a bit The left and right threadin a threaded binary tree istwo different values: from that of a normal binary tree. 1: a normal normal link to the node of UnlikeIndicates a binary tree, eachchild nodea threaded binary tree 0: Indicates a thread pointing of the inorder predecessor or contains two extra pieces to information, namely left inorder right thread. thread and successor

4631

Information

2389

Data

Right Thread

Ver. 1.0

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

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 the inorder successor of a node Algorithm to find the locate in a threaded a node tree. threaded 2. If the right child of currentNode is a successor of binary in a thread: binary tree. a. Mark the right child of
b. 3. currentNode as successor. Exit. Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.

4.

5.

6.

Ver. 1.0

Session 14

## Data Structures and Algorithms

Traversing a Threaded Binary Tree (Contd.)
1.

Header Node
2.

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

. 65 .
3.

If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.

. 40 .

. 72 .

4.

5.

30

. 50 .

69

80

6.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

Traversing a Threaded Binary Tree (Contd.)
1.

Header Node
2.

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

currentNode

. 65 .
3.

If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.

. 40 .

. 72 .

4.

5.

30

. 50 .

69

80

6.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

Traversing a Threaded Binary Tree (Contd.)
1.

Header Node
2.

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

currentNode

. 65 .
3.

If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.

. 40 .

. 72 .

4.

5.

30

. 50 .

69

80

6.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

Traversing a Threaded Binary Tree (Contd.)
1.

Header Node
2.

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

currentNode

. 65 .
currentNode
3.

If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.

. 40 .

. 72 .

4.

5.

30

. 50 .

69

80

6.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

Traversing a Threaded Binary Tree (Contd.)
1.

Header Node
2.

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

. 65 .
currentNode
3.

If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.

. 40 .

. 72 .

4.

5.

30

. 50 .

69

80

6.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

Traversing a Threaded Binary Tree (Contd.)
1.

Header Node
2.

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

. 65 .
currentNode
3.

If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.

. 40 .

. 72 .

4.

5.

30

. 50 .

69 currentNode 60

80

6.

Ver. 1.0

Session 14

## Data Structures and Algorithms

Traversing a Threaded Binary Tree (Contd.)
1.

Header Node
2.

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

. 65 .
3.

If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.

. 40 .

. 72 .

4.

5.

30

. 50 .

69 currentNode 60

80

6.

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

Header Node
2.

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

. 65 .
3.

If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.

. 40 .

successor

. 72 .

4.

5.

30

. 50 .

69 currentNode 60

80

6.

Ver. 1.0

Session 14

## Data Structures and Algorithms

Traversing a Threaded Binary Tree (Contd.)

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.

Header Node

2.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

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.

Header Node
2.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

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.

Header Node
2.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

currentNode

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

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.

Header Node
2.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

currentNode

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

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.

Header Node
2.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

currentNode currentNode

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

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.

Header Node
2.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .
currentNode

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

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.

Header Node
2.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .
currentNode

3.

4.

. 40 .

. 72 .

5.

6.

30 currentNode

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

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.

Header Node
2.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30 currentNode

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

Traversing a Threaded Binary Tree (Contd.)
30
Header Node
2.
1. If the left child of the header node is a thread pointing to itself: a. Display Tree is empty. b. Exit.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30 currentNode

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

Traversing a Threaded Binary Tree (Contd.)
30
Header Node
2.
1. If the left child of the header node is a thread pointing to itself: a. Display Tree is empty. b. Exit.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30 currentNode

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

Traversing a Threaded Binary Tree (Contd.)
30
Header Node
2.
1. If the left child of the header node is a thread pointing to itself: a. Display Tree is empty. b. Exit.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .
currentNode

3.

4.

. 40 .

. 72 .

5.

6.

30 currentNode

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .
currentNode

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .
currentNode

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .
currentNode

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .
currentNode

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .
currentNode

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

currentNode

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

currentNode

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

currentNode

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

currentNode

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

currentNode

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

currentNode

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

currentNode
8.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

currentNode
8.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .
currentNode

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

currentNode
8.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .
currentNode

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

. 65 .
currentNode

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

currentNode
8.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the 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
Header Node
2.
1. If the left child of the header node is a thread pointing to itself: a. Display Tree is empty. b. Exit.

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

Repeat step 4 until the left child of currentNode becomes a thread. Make currentNode point to its left child. Display the information held by currentNode. Repeat steps 7 and 8 until right child of currentNode points to the header node.

Traversal complete

. 65 .

3.

4.

. 40 .

. 72 .

5.

6.

30

. 50 .

69

80
7.

currentNode
8.

Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.

60

Ver. 1.0

Session 14

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree

Insert an algorithm to locate the position inserting a newto be Write operation refers to the process of of a new node node at its appropriatebinary tree. inserted in a threaded 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.

Ver. 1.0

Session 14

## Data Structures and Algorithms

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

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

2. 3. 4.

. 65 . . 40 . . 72 .

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
. 65 . . 40 . . 72 .

2. 3. 4.

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
currentNode

2. 3. 4.

. 65 . . 72 .

. 40 .

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node parent
1. 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

.
. 65 . . 72 .

2. 3. 4.

currentNode

. 40 .

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node parent
1. 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

.
. 65 . . 72 .

2. 3. 4.

currentNode

. 40 .

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node parent parent currentNode
1. 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

.
. 65 . . 72 .

2. 3. 4.

. 40 .

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
parent currentNode

2. 3. 4.

. 65 . . 72 .

. 40 .

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
parent currentNode currentNode

2. 3. 4.

. 65 . . 72 .

. 40 .

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
parent

2. 3. 4.

. 65 .
currentNode

. 40 .

. 72 .

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
parent

2. 3. 4.

. 65 .
currentNode parent

. 40 .

. 72 .

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
. 65 .
currentNode parent

2. 3. 4.

. 40 .

. 72 .

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
. 65 .
currentNode parent

2. 3. 4.

. 40 .

. 72 .

30 currentNode

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
. 65 .
parent

2. 3. 4.

. 40 .

. 72 .

30 currentNode

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
. 65 .
parent parent 30 currentNode

2. 3. 4.

. 40 .

. 72 .

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
. 65 . . 40 .
parent 30 currentNode

2. 3. 4.

. 72 .

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
. 65 . . 40 .
parent 30 currentNode

2. 3. 4.

. 72 .

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
. 65 . . 40 .
parent 30 currentNode

2. 3. 4.

. 72 .

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
. 65 . . 40 .
parent 30 currentNode

2. 3. 4.

. 72 .

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
. 65 . . 40 .
parent 30 currentNode currentNode = NULL 60
Ver. 1.0

2. 3. 4.

. 72 .

. 50 .

69

80

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insert node 35
Header Node
1. 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

.
Parent node located

2. 3. 4.

. 65 . . 72 .

. 40 .
parent 30

. 50 .

69

80

currentNode = NULL 60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)

Once you locate the insert of the new node to threaded Write an algorithm toparent a node in an empty be inserted, you need to binary tree. 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

Ver. 1.0

Session 14

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. Allocate memory for the new node. 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. Set the value of the left thread field of the head node as one. Make the left child field of the header node as a link pointing to the new node. Make the left child field of the new node as a thread pointing to the header node. Make the right child field of the new node as a thread pointing to the header node.

If the 65 Inserttree is initiallyaempty,in a you Algorithm to insert node then need to insert the new nodeis the threaded binary tree, which as left child of the header node. initially empty.

2.

3.

4.

Header Node

5.

6.

7.

Ver. 1.0

Session 14

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. Allocate memory for the new node. 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. Set the value of the left thread field of the head node as one. Make the left child field of the header node as a link pointing to the new node. Make the left child field of the new node as a thread pointing to the header node. Make the right child field of the new node as a thread pointing to the header node.

Insert 65

2.

3.

4.

Header Node

5.

6.

7.

New Node

Ver. 1.0

Session 14

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. Allocate memory for the new node. 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. Set the value of the left thread field of the head node as one. Make the left child field of the header node as a link pointing to the new node. Make the left child field of the new node as a thread pointing to the header node. Make the right child field of the new node as a thread pointing to the header node.

2.

3.

4.

Header Node

5.

6.

65
7.

New Node

Ver. 1.0

Session 14

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. Allocate memory for the new node. 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. Set the value of the left thread field of the head node as one. Make the left child field of the header node as a link pointing to the new node. Make the left child field of the new node as a thread pointing to the header node. Make the right child field of the new node as a thread pointing to the header node.

2.

3.

4.

Header Node

5.

6.

65
7.

New Node

Ver. 1.0

Session 14

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. Allocate memory for the new node. 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. Set the value of the left thread field of the head node as one. Make the left child field of the header node as a link pointing to the new node. Make the left child field of the new node as a thread pointing to the header node. Make the right child field of the new node as a thread pointing to the header node.

2.

3.

4.

Header Node

5.

6.

65
7.

New Node

Ver. 1.0

Session 14

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. Allocate memory for the new node. 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. Set the value of the left thread field of the head node as one. Make the left child field of the header node as a link pointing to the new node. Make the left child field of the new node as a thread pointing to the header node. Make the right child field of the new node as a thread pointing to the header node.

2.

3.

4.

Header Node

5.

6.

65
7.

New Node

Ver. 1.0

Session 14

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. Allocate memory for the new node. 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. Set the value of the left thread field of the head node as one. Make the left child field of the header node as a link pointing to the new node. Make the left child field of the new node as a thread pointing to the header node. Make the right child field of the new node as a thread pointing to the header node.

2.

3.

4.

Header Node

5.

6.

65
7.

New Node

Ver. 1.0

Session 14

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. Allocate memory for the new node. 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. Set the value of the left thread field of the head node as one. Make the left child field of the header node as a link pointing to the new node. Make the left child field of the new node as a thread pointing to the header node. Make the right child field of the new node as a thread pointing to the header node.

Insertion complete

2.

3.

4.

Header Node

5.

6.

65
7.

New Node

Ver. 1.0

Session 14

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)

Ver. 1.0

Session 14

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

## Header Node to insert a new

2. 3. 4.

. 65 . . 40 . . 72 .
5.

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 75

Header Node

2. 3.

.
. 65 . . 40 . . 72 .

4.

5.

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 75

Header Node

2. 3.

.
. 65 . . 40 . . 72 .
parent

4.

5.

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 75

Header Node

2. 3.

.
. 65 . . 40 . . 72 .
parent

4.

5.

30

. 50 .

69

80

60
Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 75

Header Node

2. 3.

.
. 65 . . 40 . . 72 .
parent

4.

5.

30

. 50 .

69

80

60
Ver. 1.0

75

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 75

Header Node

2. 3.

.
. 65 . . 40 . . 72 .
parent

4.

5.

30

. 50 .

69

80

60
Ver. 1.0

75

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 75

Header Node

2. 3.

.
. 65 . . 40 . . 72 .
parent

4.

5.

30

. 50 .

69

80

60
Ver. 1.0

75

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 75

Header Node

2. 3.

.
. 65 . . 40 . . 72 .
parent

4.

5.

30

. 50 .

69

80

60
Ver. 1.0

75

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 75

Header Node

2. 3.

.
. 65 . . 40 . . 72 .
parent

4.

5.

30

. 50 .

69

80

60
Ver. 1.0

75

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 75

Header Node

2. 3.

.
. 65 . . 40 . . 72 .
parent

4.

5.

30

. 50 .

69

80

60
Ver. 1.0

75

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 75

Header Node

2. 3.

.
. 65 . . 40 . . 72 .
parent

4.

5.

30

. 50 .

69

. 80

60
Ver. 1.0

75

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insertion complete
Insert a node 75
Header Node
1. 2. 3. 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

.
. 65 . . 40 . . 72 .
parent

4.

5.

30

. 50 .

69

. 80

60
Ver. 1.0

75

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 35

Header Node

2. 3. 4.

. 65 .

. 40 .

. 72 .
5.

30

. 50 .

69

80

60

Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 35

Header Node

2. 3. 4.

. 65 .

. 40 .
parent

. 72 .
5.

30

. 50 .

69

80

60

Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 35

Header Node

2. 3. 4.

. 65 .

. 40 .
parent

. 72 .
5.

30

. 50 .

69

80

35

60

Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 35

Header Node

2. 3. 4.

. 65 .

. 40 .
parent

. 72 .
5.

30

. 50 .

69

80

35

60

Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 35

Header Node

2. 3. 4.

. 65 .

. 40 .
parent

. 72 .
5.

30

. 50 .

69

80

35

60

Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 35

Header Node

2. 3. 4.

. 65 .

. 40 .
parent

. 72 .
5.

30

. 50 .

69

80

35

60

Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 35

Header Node

2. 3. 4.

. 65 .

. 40 .
parent

. 72 .
5.

30

. 50 .

69

80

35

60

Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 35

Header Node

2. 3. 4.

. 65 .

. 40 .
parent

. 72 .
5.

30

. 50 .

69

80

35

60

Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 35

Header Node

2. 3. 4.

. 65 .

. 40 .
parent

. 72 .
5.

30

. 50 .

69

80

35

60

Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
1. 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

Insert a node 35

Header Node

2. 3. 4.

. 65 .

. 40 .
parent

. 72 .
5.

30

. 50 .

69

80

35

60

Ver. 1.0

## Data Structures and Algorithms

Inserting Nodes in a Threaded Binary Tree (Contd.)
Insertion complete
Insert a node 35
1. 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

Header Node

2. 3. 4.

. 65 .

. 40 .
parent

. 72 .
5.

30

. 50 .

69

80

35

60

Ver. 1.0

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