Documente Academic
Documente Profesional
Documente Cultură
Objectives
Ver. 1.0
Session 14
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
36
24 40 35 43 44 55 59
68
72
Index
Ver. 1.0
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
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
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
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
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
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
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
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
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
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
Ver. 1.0
Session 14
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
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
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
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
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
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
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
Header Node
2.
Identify the node for which you want to locate the inorder successor, and mark it as currentNode.
currentNode
. 65 .
currentNode
3.
If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.
. 40 .
. 72 .
4.
5.
30
. 50 .
69
80
6.
60
Ver. 1.0
Session 14
Header Node
2.
Identify the node for which you want to locate the inorder successor, and mark it as currentNode.
. 65 .
currentNode
3.
If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.
. 40 .
. 72 .
4.
5.
30
. 50 .
69
80
6.
60
Ver. 1.0
Session 14
Header Node
2.
Identify the node for which you want to locate the inorder successor, and mark it as currentNode.
. 65 .
currentNode
3.
If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.
. 40 .
. 72 .
4.
5.
30
. 50 .
69 currentNode 60
80
6.
Ver. 1.0
Session 14
Header Node
2.
Identify the node for which you want to locate the inorder successor, and mark it as currentNode.
. 65 .
3.
If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.
. 40 .
. 72 .
4.
5.
30
. 50 .
69 currentNode 60
80
6.
Ver. 1.0
Session 14
Header Node
2.
Identify the node for which you want to locate the inorder successor, and mark it as currentNode.
. 65 .
3.
If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. b. Exit.
Make currentNode point to its right child. Repeat step 5 until left child of currentNode becomes a thread. Make currentNode point to its left child. Mark currentNode as successor.
. 40 .
successor
. 72 .
4.
5.
30
. 50 .
69 currentNode 60
80
6.
Ver. 1.0
Session 14
Ver. 1.0
Session 14
Header Node
2.
. 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
Header Node
2.
. 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
Header Node
2.
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
Header Node
2.
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
Header Node
2.
currentNode currentNode
. 65 .
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
Ver. 1.0
Session 14
Header Node
2.
. 65 .
currentNode
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
Ver. 1.0
Session 14
Header Node
2.
. 65 .
currentNode
3.
4.
. 40 .
. 72 .
5.
6.
30 currentNode
. 50 .
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
Ver. 1.0
Session 14
Header Node
2.
. 65 .
3.
4.
. 40 .
. 72 .
5.
6.
30 currentNode
. 50 .
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
Ver. 1.0
Session 14
. 65 .
3.
4.
. 40 .
. 72 .
5.
6.
30 currentNode
. 50 .
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
Ver. 1.0
Session 14
. 65 .
3.
4.
. 40 .
. 72 .
5.
6.
30 currentNode
. 50 .
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
Ver. 1.0
Session 14
. 65 .
currentNode
3.
4.
. 40 .
. 72 .
5.
6.
30 currentNode
. 50 .
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
Ver. 1.0
Session 14
. 65 .
currentNode
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
Ver. 1.0
Session 14
. 65 .
currentNode
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
currentNode
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
Ver. 1.0
Session 14
. 65 .
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
currentNode
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
Ver. 1.0
Session 14
. 65 .
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
currentNode
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
currentNode
Ver. 1.0
Session 14
. 65 .
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
currentNode
Ver. 1.0
Session 14
currentNode
. 65 .
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
currentNode
Ver. 1.0
Session 14
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
currentNode
. 65 .
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
69
80
7.
currentNode
8.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
60
Ver. 1.0
Session 14
. 65 .
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
69
80
7.
currentNode
8.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
60
Ver. 1.0
Session 14
. 65 .
currentNode
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
69
80
7.
currentNode
8.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
60
Ver. 1.0
Session 14
. 65 .
currentNode
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
69
80
7.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
8.
60
Ver. 1.0
Session 14
. 65 .
currentNode
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
69
80
7.
currentNode
8.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
60
Ver. 1.0
Session 14
Traversal complete
. 65 .
3.
4.
. 40 .
. 72 .
5.
6.
30
. 50 .
69
80
7.
currentNode
8.
Find the inorder successor of currentNode, and mark the inorder successor as currentNode.
Display the information held by the currentNode.
60
Ver. 1.0
Session 14
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
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
.
. 65 . . 40 . . 72 .
2. 3. 4.
30
. 50 .
69
80
60
Ver. 1.0
.
currentNode
2. 3. 4.
. 65 . . 72 .
. 40 .
30
. 50 .
69
80
60
Ver. 1.0
.
. 65 . . 72 .
2. 3. 4.
currentNode
. 40 .
30
. 50 .
69
80
60
Ver. 1.0
.
. 65 . . 72 .
2. 3. 4.
currentNode
. 40 .
30
. 50 .
69
80
60
Ver. 1.0
.
. 65 . . 72 .
2. 3. 4.
. 40 .
30
. 50 .
69
80
60
Ver. 1.0
.
parent currentNode
2. 3. 4.
. 65 . . 72 .
. 40 .
30
. 50 .
69
80
60
Ver. 1.0
.
parent currentNode currentNode
2. 3. 4.
. 65 . . 72 .
. 40 .
30
. 50 .
69
80
60
Ver. 1.0
.
parent
2. 3. 4.
. 65 .
currentNode
. 40 .
. 72 .
30
. 50 .
69
80
60
Ver. 1.0
.
parent
2. 3. 4.
. 65 .
currentNode parent
. 40 .
. 72 .
30
. 50 .
69
80
60
Ver. 1.0
.
. 65 .
currentNode parent
2. 3. 4.
. 40 .
. 72 .
30
. 50 .
69
80
60
Ver. 1.0
.
. 65 .
currentNode parent
2. 3. 4.
. 40 .
. 72 .
30 currentNode
. 50 .
69
80
60
Ver. 1.0
.
. 65 .
parent
2. 3. 4.
. 40 .
. 72 .
30 currentNode
. 50 .
69
80
60
Ver. 1.0
.
. 65 .
parent parent 30 currentNode
2. 3. 4.
. 40 .
. 72 .
. 50 .
69
80
60
Ver. 1.0
.
. 65 . . 40 .
parent 30 currentNode
2. 3. 4.
. 72 .
. 50 .
69
80
60
Ver. 1.0
.
. 65 . . 40 .
parent 30 currentNode
2. 3. 4.
. 72 .
. 50 .
69
80
60
Ver. 1.0
.
. 65 . . 40 .
parent 30 currentNode
2. 3. 4.
. 72 .
. 50 .
69
80
60
Ver. 1.0
.
. 65 . . 40 .
parent 30 currentNode
2. 3. 4.
. 72 .
. 50 .
69
80
60
Ver. 1.0
.
. 65 . . 40 .
parent 30 currentNode currentNode = NULL 60
Ver. 1.0
2. 3. 4.
. 72 .
. 50 .
69
80
.
Parent node located
2. 3. 4.
. 65 . . 72 .
. 40 .
parent 30
. 50 .
69
80
currentNode = NULL 60
Ver. 1.0
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
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
Insert 65
2.
3.
4.
Header Node
5.
6.
7.
New Node
Ver. 1.0
Session 14
2.
3.
4.
Header Node
5.
6.
65
7.
New Node
Ver. 1.0
Session 14
2.
3.
4.
Header Node
5.
6.
65
7.
New Node
Ver. 1.0
Session 14
2.
3.
4.
Header Node
5.
6.
65
7.
New Node
Ver. 1.0
Session 14
2.
3.
4.
Header Node
5.
6.
65
7.
New Node
Ver. 1.0
Session 14
2.
3.
4.
Header Node
5.
6.
65
7.
New Node
Ver. 1.0
Session 14
Insertion complete
2.
3.
4.
Header Node
5.
6.
65
7.
New Node
Ver. 1.0
Session 14
Ver. 1.0
Session 14
2. 3. 4.
. 65 . . 40 . . 72 .
5.
30
. 50 .
69
80
60
Ver. 1.0
Insert a node 75
Header Node
2. 3.
.
. 65 . . 40 . . 72 .
4.
5.
30
. 50 .
69
80
60
Ver. 1.0
Insert a node 75
Header Node
2. 3.
.
. 65 . . 40 . . 72 .
parent
4.
5.
30
. 50 .
69
80
60
Ver. 1.0
Insert a node 75
Header Node
2. 3.
.
. 65 . . 40 . . 72 .
parent
4.
5.
30
. 50 .
69
80
60
Ver. 1.0
Insert a node 75
Header Node
2. 3.
.
. 65 . . 40 . . 72 .
parent
4.
5.
30
. 50 .
69
80
60
Ver. 1.0
75
Insert a node 75
Header Node
2. 3.
.
. 65 . . 40 . . 72 .
parent
4.
5.
30
. 50 .
69
80
60
Ver. 1.0
75
Insert a node 75
Header Node
2. 3.
.
. 65 . . 40 . . 72 .
parent
4.
5.
30
. 50 .
69
80
60
Ver. 1.0
75
Insert a node 75
Header Node
2. 3.
.
. 65 . . 40 . . 72 .
parent
4.
5.
30
. 50 .
69
80
60
Ver. 1.0
75
Insert a node 75
Header Node
2. 3.
.
. 65 . . 40 . . 72 .
parent
4.
5.
30
. 50 .
69
80
60
Ver. 1.0
75
Insert a node 75
Header Node
2. 3.
.
. 65 . . 40 . . 72 .
parent
4.
5.
30
. 50 .
69
80
60
Ver. 1.0
75
Insert a node 75
Header Node
2. 3.
.
. 65 . . 40 . . 72 .
parent
4.
5.
30
. 50 .
69
. 80
60
Ver. 1.0
75
.
. 65 . . 40 . . 72 .
parent
4.
5.
30
. 50 .
69
. 80
60
Ver. 1.0
75
Insert a node 35
Header Node
2. 3. 4.
. 65 .
. 40 .
. 72 .
5.
30
. 50 .
69
80
60
Ver. 1.0
Insert a node 35
Header Node
2. 3. 4.
. 65 .
. 40 .
parent
. 72 .
5.
30
. 50 .
69
80
60
Ver. 1.0
Insert a node 35
Header Node
2. 3. 4.
. 65 .
. 40 .
parent
. 72 .
5.
30
. 50 .
69
80
35
60
Ver. 1.0
Insert a node 35
Header Node
2. 3. 4.
. 65 .
. 40 .
parent
. 72 .
5.
30
. 50 .
69
80
35
60
Ver. 1.0
Insert a node 35
Header Node
2. 3. 4.
. 65 .
. 40 .
parent
. 72 .
5.
30
. 50 .
69
80
35
60
Ver. 1.0
Insert a node 35
Header Node
2. 3. 4.
. 65 .
. 40 .
parent
. 72 .
5.
30
. 50 .
69
80
35
60
Ver. 1.0
Insert a node 35
Header Node
2. 3. 4.
. 65 .
. 40 .
parent
. 72 .
5.
30
. 50 .
69
80
35
60
Ver. 1.0
Insert a node 35
Header Node
2. 3. 4.
. 65 .
. 40 .
parent
. 72 .
5.
30
. 50 .
69
80
35
60
Ver. 1.0
Insert a node 35
Header Node
2. 3. 4.
. 65 .
. 40 .
parent
. 72 .
5.
30
. 50 .
69
80
35
60
Ver. 1.0
Insert a node 35
Header Node
2. 3. 4.
. 65 .
. 40 .
parent
. 72 .
5.
30
. 50 .
69
80
35
60
Ver. 1.0
Header Node
2. 3. 4.
. 65 .
. 40 .
parent
. 72 .
5.
30
. 50 .
69
80
35
60
Ver. 1.0
Ver. 1.0
Session 14
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