Objectives

107
Data Structures and Algorithms Session 14 Ver. 1.0 Objectives In this session, you will learn to: Apply trees to solve programming problems Implement a threaded binary tree

description

Objectives. In this session, you will learn to: Apply trees to solve programming problems Implement a threaded binary tree. Indexing. The following is an example of an index. - PowerPoint PPT Presentation

Transcript of Objectives

Page 1: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Objectives

In this session, you will learn to:Apply trees to solve programming problems

Implement a threaded binary tree

Page 2: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Data on disk files is usually organized as records containing several fields, one of which is often used as a key field.

The key field is used to uniquely identify each record in a file.

Indexing is one of the data access methods for accessing records from the disk files.

Indexing is implemented through a table called index.

Index consists of two entries:Key fields of all the records

Offset position of each record

The following is an example of an index.

Indexing

Key field Offset

36 0

52 200

24 400

44 600

40 800

68 1000

59 1200

55 1400

72 1600

35 1800

43 2000

Page 3: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Indexing (Contd.)

To access the record with key field 59, search the index for this key value to retrieve its corresponding offset value, which is 1200.

Read the record from the file starting from this offset position.

You can implement a binary search tree to store these index values.

This approach enables faster search for a key value.

52

36 68

24 44

40

59 72

55

4335

Key field Offset

36 0

52 200

24 400

44 600

40 800

68 1000

59 1200

55 1400

72 1600

35 1800

43 2000

Index Key Fields Stored in a Binary Search Tree

Page 4: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Implementing a Threaded Binary Trees

One of the common operations on a binary tree is traversal.

In a linked representation of a binary tree, traversal is usually implemented through recursion.

As a result, a stack is maintained in the memory.

If the tree is huge, implementing recursion to traverse the tree would require a lot of memory space.

In the absence of sufficient memory space, implementing recursion can lead to a memory leak.

Page 5: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Defining Threaded Binary Trees

In such a case, it would be good if you have some mechanism by which you can traverse the tree without implementing recursion.

You can solve this problem by implementing a threaded binary tree.

In a binary search tree, there are many nodes that have an empty left child or empty right child or both.

You can utilize these fields in such a way so that the empty left child of a node points to its inorder predecessor and empty right child of the node points to its inorder successor.

Page 6: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Consider the following binary search tree.

Most of the nodes in this tree hold a NULL value in their left or right child fields.

30 50 80

60

72

69

40. ..

65.

.

. .

.

Defining Threaded Binary Trees (Contd.)

In this case, it would be good if these NULL fields are utilized for some other useful purpose.

Page 7: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

The empty left child field of a node can be used to point to its inorder predecessor.

Similarly, the empty right child field of a node can be used to point to its inorder successor.

30 50 80

60

72

69

40. ...

.

Defining Threaded Binary Trees (Contd.)

Such a type of binary tree is known as a threaded binary tree.

A field that holds the address of its inorder successor or predecessor is known as thread.

65. .

Page 8: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Node 30 does not have an inorder predecessor because it is the first node to be traversed in inorder sequence.

Similarly, node 80 does not have an inorder successor.

Defining Threaded Binary Trees (Contd.)

30 50 80

60

72

69

40. ..

65.

.

.

.

Page 9: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Therefore, you take a dummy node called the header node.Header Node

Defining Threaded Binary Trees (Contd.)

30 50 80

60

72

69

40. ..

65.

.

.

.

The right child of the header node always points to itself.

Page 10: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

The threaded binary tree is represented as the left child of the header node. Header Node

Defining Threaded Binary Trees (Contd.)

30 50 80

60

72

69

40. ..

65.

.

.

.

Page 11: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

The left thread of node 30 and the right thread of node 80 point to the header node.

Defining Threaded Binary Trees (Contd.)

Header Node

30 50 80

60

72

69

40. ..

65.

.

.

.

. .

Page 12: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

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

Just a minute

Answer:successor, predecessor

Page 13: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Representing a Threaded Binary Tree

The structure of a node in a threaded binary tree is a bit different from that of a normal binary tree.

Unlike a normal binary tree, each node of a threaded binary tree contains two extra pieces of information, namely left thread and right thread.

Information4631 2389

Address of Right Child

Address of Left Child

DataLeft Thread

Right Thread

The left and right thread fields of a node can have two values:

1: Indicates a normal link to the child node

0: Indicates a thread pointing to the inorder predecessor or inorder successor

Page 14: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

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

Search

Insert

Delete

Representing a Threaded Binary Tree (Contd.)

Page 15: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

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

Just a minute

Answer:In a threaded binary tree, the root node is identified as the left child of the header node. If the tree is empty, the left child of the header node becomes a thread pointing to itself.

Page 16: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

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

Just a minute

Answer:Each node in a threaded binary tree holds two extra pieces of information known as left thread and right thread. The value of these two fields indicates whether the left/right child field of a node contains a link to a child node or a thread to its inorder predecessor/successor.

Page 17: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree

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

Page 18: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

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

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

2. If the right child of currentNode is a thread:

a. Mark the right child of currentNode as successor.

b. Exit.

3. Make currentNode point to its right child.

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

5. Make currentNode point to its left child.

6. Mark currentNode as successor.

Traversing a Threaded Binary Tree (Contd.)

Page 19: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Let us find the inorder successor of node 65

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

2. If the right child of currentNode is a thread:

a. Mark the right child of currentNode as successor.

b. Exit.

3. Make currentNode point to its right child.

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

5. Make currentNode point to its left child.

6. Mark currentNode as successor.

Traversing a Threaded Binary Tree (Contd.)

Page 20: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Let us find the inorder successor of node 65.

currentNode

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

2. If the right child of currentNode is a thread:

a. Mark the right child of currentNode as successor.

b. Exit.

3. Make currentNode point to its right child.

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

5. Make currentNode point to its left child.

6. Mark currentNode as successor.

Traversing a Threaded Binary Tree (Contd.)

Page 21: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.currentNode

Let us find the inorder successor of node 65.

Traversing a Threaded Binary Tree (Contd.)

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

2. If the right child of currentNode is a thread:

a. Mark the right child of currentNode as successor.

b. Exit.

3. Make currentNode point to its right child.

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

5. Make currentNode point to its left child.

6. Mark currentNode as successor.

Page 22: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.currentNode

currentNode

Let us find the inorder successor of node 65.

Traversing a Threaded Binary Tree (Contd.)

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

2. If the right child of currentNode is a thread:

a. Mark the right child of currentNode as successor.

b. Exit.

3. Make currentNode point to its right child.

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

5. Make currentNode point to its left child.

6. Mark currentNode as successor.

Page 23: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.currentNode

Let us find the inorder successor of node 65.

Traversing a Threaded Binary Tree (Contd.)

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

2. If the right child of currentNode is a thread:

a. Mark the right child of currentNode as successor.

b. Exit.

3. Make currentNode point to its right child.

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

5. Make currentNode point to its left child.

6. Mark currentNode as successor.

Page 24: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.currentNode

currentNode

Let us find the inorder successor of node 65.

Traversing a Threaded Binary Tree (Contd.)

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

2. If the right child of currentNode is a thread:

a. Mark the right child of currentNode as successor.

b. Exit.

3. Make currentNode point to its right child.

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

5. Make currentNode point to its left child.

6. Mark currentNode as successor.

Page 25: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Let us find the inorder successor of node 65.

currentNode

Traversing a Threaded Binary Tree (Contd.)

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

2. If the right child of currentNode is a thread:

a. Mark the right child of currentNode as successor.

b. Exit.

3. Make currentNode point to its right child.

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

5. Make currentNode point to its left child.

6. Mark currentNode as successor.

Page 26: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

successor

Inorder successor located

Let us find the inorder successor of node 65.

currentNode

Traversing a Threaded Binary Tree (Contd.)

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

2. If the right child of currentNode is a thread:

a. Mark the right child of currentNode as successor.

b. Exit.

3. Make currentNode point to its right child.

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

5. Make currentNode point to its left child.

6. Mark currentNode as successor.

Page 27: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

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

Traversing a Threaded Binary Tree (Contd.)

Page 28: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

Algorithm to traverse a threaded binary tree in inorder sequence.

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Page 29: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 30: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 31: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 32: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.currentNode

currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 33: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 34: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.currentNode

currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 35: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 36: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

currentNode

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

Page 37: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

currentNode

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

Page 38: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

currentNode

30

currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 39: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30

currentNode

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

Page 40: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30

currentNode

40

currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 41: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30 40

currentNode

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

Page 42: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30 40

currentNode

50

currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 43: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30 40 50

currentNode

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

Page 44: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30 40 50

currentNode

60

currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 45: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30 40 50 60

currentNode

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

Page 46: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30 40 50 60

currentNode

65

currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 47: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30 40 50 60 65

currentNode

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

Page 48: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30 40 50 60 65

currentNode

69

currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 49: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30 40 50 60 65 69

currentNode

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

Page 50: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30 40 50 60 65 69

currentNode

72

currentNode

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 51: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Traversing a Threaded Binary Tree (Contd.)

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

30 40 50 60 65 69 72

currentNode

80

Traversal complete

1. If the left child of the header node is a thread pointing to itself:

a. Display “Tree is empty”.b. Exit.

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

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

4. Make currentNode point to its left child.

5. Display the information held by currentNode.

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

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

8. Display the information held by the currentNode.

Page 52: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Insert operation refers to the process of inserting a new node at its appropriate position.

To implement an insert operation in a threaded binary tree, you first need to locate the position for the new node to be inserted.

For this, you first need to implement a search operation.

Inserting Nodes in a Threaded Binary Tree

Write an algorithm to locate the position of a new node to be inserted in a threaded binary tree.

Page 53: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35 1. If the left child of the header node is a thread pointing to itself:

a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

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

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 54: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35 1. If the left child of the header node is a thread pointing to itself:

a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 55: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 56: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 57: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 58: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

parent

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 59: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 60: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

currentNode

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 61: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

parent

currentNode

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 62: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

parent

currentNode

parent

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 63: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 64: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

currentNode

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 65: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

parent

currentNode

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 66: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

parent

currentNode

parent

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 67: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 68: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 69: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 70: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 71: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

currentNode

parent

currentNode = NULL

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 72: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert node 35

parent

currentNode = NULL

Parent node located

Inserting Nodes in a Threaded Binary Tree (Contd.)1. If the left child of the header node is a thread

pointing to itself:a. Mark head as parent.b. Exit.

2. Mark the left child of head as currentNode.3. Mark head as parent.4. Repeat steps a, b, c, d, and e until currentNode

becomes NULL:a. Mark currentNode as parent.b. If the value of the new node is less than

that of currentNode and the left child of currentNode is a normal link:

i. Make currentNode point to its left child and go to step 4.

c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

d. If the value of the new node is greater than that of currentNode and the right child of currentNode is a normal link:

i. Make currentNode point to its right child and go to step 4.

e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread:

i. Mark currentNode as NULL and go to step 4.

Page 73: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Once you locate the parent of the new node to be inserted, you need to consider the following three cases:

Tree is empty (if parent is the header node)

New node is to be inserted as the left child of its parent

New node is to be inserted as the right child of its parent

Write an algorithm to insert a node in an empty threaded binary tree.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 74: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

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

1. Allocate memory for the new node.

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

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

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

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

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

7. Make the right child field of the new node as a thread pointing to the header node.

Header Node

Algorithm to insert a node in a threaded binary tree, which is initially empty.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Insert 65

Page 75: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

1. Allocate memory for the new node.

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

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

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

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

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

7. Make the right child field of the new node as a thread pointing to the header node.

Header Node

New Node

Inserting Nodes in a Threaded Binary Tree (Contd.)

Insert 65

Page 76: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

1. Allocate memory for the new node.

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

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

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

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

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

7. Make the right child field of the new node as a thread pointing to the header node.

Header Node

New Node

65

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 77: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

1. Allocate memory for the new node.

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

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

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

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

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

7. Make the right child field of the new node as a thread pointing to the header node.

Header Node

New Node

65

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 78: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

1. Allocate memory for the new node.

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

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

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

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

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

7. Make the right child field of the new node as a thread pointing to the header node.

Header Node

New Node

65

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 79: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

1. Allocate memory for the new node.

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

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

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

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

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

7. Make the right child field of the new node as a thread pointing to the header node.

Header Node

New Node

65

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 80: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

1. Allocate memory for the new node.

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

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

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

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

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

7. Make the right child field of the new node as a thread pointing to the header node.

Header Node

New Node

65

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 81: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

1. Allocate memory for the new node.

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

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

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

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

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

7. Make the right child field of the new node as a thread pointing to the header node.

Header Node

New Node

65

Insertion complete

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 82: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Write an algorithm to insert a node in a non-empty threaded binary tree.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 83: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Algorithm to insert a new node in a threaded binary tree.

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Inserting Nodes in a Threaded Binary Tree (Contd.)1. Locate the parent of the new node and

mark it as parent.2. Allocate memory and assign value to

the data field of the new node.3. Set the values of the left and right

thread fields of the new node as zero.4. If the value of the new node is less

than that of parent:a. Make left child field of new node

point to the left child of parent.b. Make right child field of new

node point to parent.c. Set the value of the left thread

field of parent as one.d. Make left child field of parent

point to the new node.e. Exit.

5. If the value of the node is greater than that of parent:

a. Make left child field of new node point to parent.

b. Make right child field of new node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Page 84: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert a node 75

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 85: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert a node 75

parent

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 86: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert a node 75

parent

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 87: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert a node 75

75

parent

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 88: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert a node 75

75

parent

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 89: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert a node 75

75

parent

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 90: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert a node 75

75

parent

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 91: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert a node 75

75

parent

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 92: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert a node 75

75

parent

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 93: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert a node 75

75

parent

.

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 94: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

.

Insert a node 75

75

parent

.

Insertion complete1. Locate the parent of the new node and

mark it as parent.2. Allocate memory and assign value to

the data field of the new node.3. Set the values of the left and right

thread fields of the new node as zero.4. If the value of the new node is less

than that of parent:a. Make left child field of new node

point to the left child of parent.b. Make right child field of new

node point to parent.c. Set the value of the left thread

field of parent as one.d. Make left child field of parent

point to the new node.e. Exit.

5. If the value of the node is greater than that of parent:

a. Make left child field of new node point to parent.

b. Make right child field of new node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 95: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Let us now insert a node as the right child of its parent.

Insert a node 35

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 96: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Insert a node 35

parent

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 97: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Insert a node 35

parent

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

35

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 98: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Insert a node 35

parent

35

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 99: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Insert a node 35

parent

35

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 100: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Insert a node 35

parent

35

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 101: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Insert a node 35

parent

35

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 102: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Insert a node 35

parent

35

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 103: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Insert a node 35

parent

35

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 104: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Insert a node 35

parent

35

.

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 105: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

30 50 80

60

72

69

40. ..

65.

.

Header Node

. .

.

Insert a node 35

parent

35

Insertion complete

.

1. Locate the parent of the new node and mark it as parent.

2. Allocate memory and assign value to the data field of the new node.

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

4. If the value of the new node is less than that of parent:

a. Make left child field of new node point to the left child of parent.

b. Make right child field of new node point to parent.

c. Set the value of the left thread field of parent as one.

d. Make left child field of parent point to the new node.

e. Exit.5. If the value of the node is greater than

that of parent:a. Make left child field of new node

point to parent.b. Make right child field of new

node point to the right child of parent.

c. Set the value of right thread field of parent as one.

d. Make right child field of parent point to the new node.

e. Exit.

Inserting Nodes in a Threaded Binary Tree (Contd.)

Page 106: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

In this session, you learned that:Binary search trees can be used to implement indexing.

A threaded binary tree is a binary tree in which a node with an empty left child stores the address of its inorder predecessor and the empty right child stores the address of its inorder successor.

In a threaded binary tree, the left and right child field of a node, which holds the address of its inorder predecessor and inorder successor, respectively, is called a thread.

You can traverse a threaded binary tree without implementing recursion.

A threaded binary tree is represented as the left subtree of the header node.

Summary

Page 107: Objectives

Data Structures and Algorithms

Session 14Ver. 1.0

Summary (Contd.)

In contrast to a normal binary tree, each node in a threaded binary tree consists of two additional fields to keep track of whether the left and right child field of a node is a thread or a link.