11 views

Uploaded by Ashish Srivastava

You are on page 1of 107

Objectives

Apply trees to solve programming problems Implement a threaded binary tree

Ver. 1.0

Session 14

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

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

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

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

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

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

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

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

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

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

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

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

Representing a Threaded Binary Tree (Contd.)

Traversal Search Insert Delete

Ver. 1.0

Session 14

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.

Ver. 1.0

Session 14

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

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

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

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

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

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

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.

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

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.

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

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.

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

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.

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

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.

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

Traversing a Threaded Binary Tree (Contd.)

Ver. 1.0

Session 14

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.

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

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.

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

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.

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

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

currentNode

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

currentNode

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

currentNode

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

8.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

60

Ver. 1.0

Session 14

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.

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.

Display the information held by the currentNode.

60

Ver. 1.0

Session 14

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Inserting Nodes in a Threaded Binary Tree (Contd.)

Ver. 1.0

Session 14

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

2. 3. 4.

. 65 . . 40 . . 72 .

5.

30

. 50 .

69

80

60

Ver. 1.0

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

- Threaded Binary TreeUploaded bypramodsoni0007
- Binary TreeUploaded bySoubhik Biswas
- Binary TreeUploaded byHolly Anderson
- Data StructureUploaded byhbgfjghjj hhfh
- 09 DS and Algorithm Session 13Uploaded byAshish Srivastava
- CH5-3 (1)Uploaded byNikithakrish
- Oracle Demantra CTO LoadUploaded byajay78_hi
- Threaded-Binary-Tree.pdfUploaded byAnonymous igVRM2mA6k
- CSC148_TT2AM_2013WUploaded byexamkiller
- lab3Uploaded byDeepakGupta
- DiameterUploaded byRavinder Singh
- Sol 9Uploaded byamysure1
- 10 TreesUploaded byDũng Gooner
- Data Structure & ProgrammingUploaded byGnv Sandeep
- Notes2 TreesUploaded byAyush Tibrewala
- DomUploaded byEma
- 13-treesUploaded byLakshay Virmani
- rpartUploaded byRobert Robinson
- COS 126_ Data CompressionUploaded byivaneshubham
- 1590591585Uploaded byMostafa Hamza
- Tech Questions in InterviewUploaded byGajarajakrishnan Janarthanam
- CSCI203 Spring 2010 Workshops Lab 7[1] With Answers!Uploaded bytechnofreak9
- datastructures-110225222818-phpapp01.pptUploaded bySoumyaranjan Rath
- Answer All the QuestionsUploaded bypitchrks19841
- Moduri de parcurgereUploaded byAlex_GP
- Lecture 7.pdfUploaded byAvanakshSingh
- UIUC Final 3.1Uploaded byAce Nhat
- Chaos Game RepresentationpptUploaded byDorys Morgado
- Data Structures AptitudeUploaded byapi-19457068
- Exam2005 BlankUploaded bytanmaya1991

- 03 ASP.net Session04Uploaded byvijaysharma24x7
- 15 ASP.net Session22Uploaded byvijaysharma24x7
- 16 ASP.net Session23Uploaded byvijaysharma24x7
- 12 ASP.net Session17Uploaded byvijaysharma24x7
- 09 ASP.net Session13Uploaded byvijaysharma24x7
- 11 ASP.net Session16Uploaded byvijaysharma24x7
- 04 ASP.net Session05Uploaded byvijaysharma24x7
- 08 ASP.net Session11Uploaded byvijaysharma24x7
- 02 ASP.net Session02Uploaded byAshish Srivastava
- 13 ASP.net Session19Uploaded byAshish Srivastava
- 06 ASP.net Session08Uploaded byGirish Naik
- 05 ASP.net Session07Uploaded byvijaysharma24x7
- 07 ASP.net Session10Uploaded byvijaysharma24x7
- 10 ASP.net Session14Uploaded byvijaysharma24x7
- 14 ASP.net Session20Uploaded byvijaysharma24x7
- CPB2_XP_08Uploaded byAshish Srivastava
- 01_RDBMS_Session01Uploaded byAshish Srivastava
- Sess-5Uploaded byAshish Srivastava
- Sess-6Uploaded byAshish Srivastava
- Sess-7Uploaded byAshish Srivastava
- 03_RDBMS_Session03Uploaded byAshish Srivastava
- 12 DS and Algorithm Session 17Uploaded byAshish Srivastava
- CEH V7 Module 01Uploaded byDesmond Devendran
- 02_RDBMS_Session02Uploaded byAshish Srivastava
- Sess-3Uploaded byAshish Srivastava
- 11 DS and Algorithm Session 16Uploaded byAshish Srivastava
- Sess-1Uploaded byAshish Srivastava
- Sess-2Uploaded byAshish Srivastava
- 01 ASP.net Session01Uploaded byGirish Naik
- Sess-4Uploaded byAshish Srivastava

- TutorialsUploaded bysaibabu998
- 10.1.1.26Uploaded bywalteram
- Sony MZ-RH1 Service ManualUploaded byGus Solomon
- 19454811-CH08-Managing-A-competency-based-approach-Hellriegel-Jackson.pptUploaded byJohn
- Recent Data Class n Delivery ClassUploaded bySaranya Shanmugam
- v700 storage.pdfUploaded byMdlamini1984
- STIX Font License 2010Uploaded byblueavian
- DDS-50-2007(part-1)Uploaded byTalal Ghazi
- Coal India Limited CIL General Knowledge Question Paper Answers Model PaperUploaded byAshish Mahapatra
- Verilog Code for AluUploaded bymeaow88
- 642-832 TSHOOT Exam TopicsUploaded byBidhan Halder
- E-learning tools (English as a second language)Uploaded bymkamijo
- express tv tuner cardUploaded bygenioushelp
- Nipper Studio Beginners Guide 2.2_0Uploaded byMuhammad Arif Rahman
- Circular Fit by Several Algorithms in RUploaded byNikhil Rastogi
- dna computer pptUploaded byapi-3787760
- DataWarehouseUploaded byAshutosh
- Preface Text Sage 2016 19eUploaded bycabamaro
- Oracle ConfiguratorUploaded bytsrikanthreddy92
- Working With Inx File FormatUploaded byvhizzie
- PythonUploaded byPradeep Kumar
- puzzels on cUploaded byUjjwal Vaddepati
- Computer Science & Engineering: An International Journal (CSEIJ)Uploaded bycseij
- HCMSUploaded bytechcare123
- XP Plus Brochure 10 2007.Uploaded byloginrr
- KPIwalkthroughUploaded byMati Selg
- Tremolator ManualUploaded byCasar1973
- JDE Auto EMail Setup in Employee Queue managerUploaded byArjun Cool
- D61820GC20_agUploaded byhenry eduardo
- Hcl Summer ReportUploaded bykeviv_virgo8189