speeding up traversalsds.nathanielgmartin.com/wk11/w11l2-threaded_binary_trees.pdf · defining...
TRANSCRIPT
![Page 1: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/1.jpg)
Threaded Binary Trees
Speeding up traversals
![Page 2: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/2.jpg)
Objectives
In this session, you will learn toThreaded binary tree
![Page 3: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/3.jpg)
Defining Threaded Binary Trees
• In a binary search tree, there are many nodes that have anempty left child or empty right child or both.
• You can utilize these fields in such a way so that the emptyleft child of a node points to its inorder predecessor andempty right child of the node points to its inorder successor.
![Page 4: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/4.jpg)
Threaded binary Tree
• One way threading:- A thread will appear in a right fieldof a node and will point to the next node in the inordertraversal.
• Two way threading:- A thread will also appear in the leftfield of a node and will point to the preceding node in theinorder traversal.
![Page 5: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/5.jpg)
Consider the following binary search tree.
Most of the nodes in this tree hold a NULL value in their left or right childfields.
30 50 80
60
72
69
40. ..
65.
.
. .
.
Defining Threaded Binary Trees
In this case, it would be good if these NULL fields are utilized for someother useful purpose.
![Page 6: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/6.jpg)
The empty left child field of a node can be used to point to its inorderpredecessor.
Similarly, the empty right child field of a node can be used to point toits in-order successor.
30 50 80
60
72
69
40. ...
.
One Way Threaded Binary Trees
Such a type of binary tree is known as a one way threaded binary tree.
A field that holds the address of its in-order successor is known as thread.
In-order :- 30 40 50 60 65 69 72 80 65. .
![Page 7: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/7.jpg)
The empty left child field of a node can be used to point to its inorder predecessor.
Similarly, the empty right child field of a node can be used to point to its inordersuccessor.Inorder :- 30 40 50 60 65 69 72 80
30 50 80
60
72
69
40. ...
.
Two way Threaded Binary Trees
Such a type of binary tree is known as a threaded binary tree.
A field that holds the address of its inorder successor or predecessor isknown as thread.
65. .
![Page 8: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/8.jpg)
Node 30 does not have an inorder predecessor because itis the first node to be traversed in inorder sequence.
Similarly, node 80 does not have an inorder successor.
30 50 80
60
72
69
40. ..
65.
.
.
.
![Page 9: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/9.jpg)
Therefore, you take a dummy node called the header node.
Header Node
Two way Threaded Binary Trees with header Node
30 50 80
60
72
69
40. ..
65.
.
.
.
The right child of the header node always points to itself.
![Page 10: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/10.jpg)
The threaded binary tree is represented as the left child of the header node.
Header Node
30 50 80
60
72
69
40. ..
65.
.
.
.
![Page 11: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/11.jpg)
The left thread of node 30 and the right thread of node 80point to the header node. Header Node
30 50 80
60
72
69
40. ..
65.
.
.
.
. .
![Page 12: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/12.jpg)
Representing a Threaded Binary Tree
The structure of a node in a threaded binary tree is a bit different fromthat of a normal binary tree.
Unlike a normal binary tree, each node of a threaded binary treecontains two extra pieces of information, namely left thread and rightthread.
Information4631 2389
Address ofRight Child
Address ofLeft Child
DataLeftThread
RightThread
The left and right thread fields of a node can have two values:1: Indicates a normal link to the child node
0: Indicates a thread pointing to the inorder predecessor or inordersuccessor
![Page 13: Speeding up traversalsds.nathanielgmartin.com/wk11/W11L2-Threaded_binary_trees.pdf · Defining Threaded Binary Trees • In a binary search tree, there are many nodes that have an](https://reader036.vdocuments.mx/reader036/viewer/2022062605/5fcd1fcf7f25cd54182b4f41/html5/thumbnails/13.jpg)
In a threaded binary tree, the right thread of a node points toits inorder ___________, and the left thread points to itsinorder ____________.
Answer:successor, predecessor