Download - Threaded binarytree&heapsort
![Page 1: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/1.jpg)
Threaded Binary Tree
![Page 2: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/2.jpg)
Definition
A binary search tree in which each node uses an otherwise-empty left child link to refer to the node's in-order predecessor and an empty right child link to refer to its in-order successor.
![Page 3: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/3.jpg)
Threaded Binary Trees
• Two many null pointers in current representationof binary trees
n: number of nodesnumber of non-null links: n-1total links: 2nnull links: 2n-(n-1)=n+1
• Replace these null pointers with some useful“threads”.
![Page 4: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/4.jpg)
Threaded Binary Trees (Continued)
If ptr->left_child is null,
replace it with a pointer to the node that would be
visited before ptr in an inorder traversal
If ptr->right_child is null,
replace it with a pointer to the node that would be
visited after ptr in an inorder traversal
![Page 5: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/5.jpg)
A Threaded Binary Tree
A
B C
GE
I
D
H
F
root
dangling
dangling
inorder traversal:
H, D, I, B, E, A, F, C, G
![Page 6: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/6.jpg)
TRUE � � FALSE
Data Structures for Threaded BT
typedef struct threaded_tree
*threaded_pointer;
typedef struct threaded_tree {
short int left_thread;
threaded_pointer left_child;
char data;
threaded_pointer right_child;
short int right_thread; };
left_thread left_child data right_child right_thread
FALSE: childTRUE: thread
![Page 7: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/7.jpg)
• Dangling can be solved as follows
– Introduce a header node.
– The left and right pointer of the header node
are treated as normal links and are initialized
to point to header node itself
![Page 8: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/8.jpg)
Memory Representation of A Threaded BT
f f--
f fA
f fCf fB
t tE t tF t Gf fD
t tIt tH
root
![Page 9: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/9.jpg)
• Right-threaded tree:
• A variant of a threaded tree in which only the right thread, i.e. link to the successor, of each node is maintained.
![Page 10: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/10.jpg)
Advantages of Threaded Tree
• It reduces the time complexity of algorithm.
• Operation like traversal, insertion, deletion can be done without using recursive algorithm.
• It becomes very easy to find out inorder successor of any node ‘n’ in a threaded binary tree. If the right thread of n is true or 1 then the node pointed by its right pointer is the desired information successor. If the right thread of ‘n’ is false or 0 then the inorder successor must be on the right subtree of ‘n’.
![Page 11: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/11.jpg)
Application of Tree
• Expression Tree
• Game Tree
![Page 12: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/12.jpg)
Infix, prefix, and postfix notation
• representation of math expressions as a binary tree
– operators have their left and right operands as
subtrees
– literal values are stored as leaves
• notations
– prefix: Polish notation
– infix: standard notation
– postfix: reverse Polish notation
![Page 13: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/13.jpg)
Expression example
![Page 14: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/14.jpg)
Expression tree example
![Page 15: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/15.jpg)
Game Tree• Tree generated for games such as tic-tac-
toe
• Consider all the possible position for the given position. And find the best solution to win.
![Page 16: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/16.jpg)
Heap Sort
• It is based on tree structure.
• Heap sort is in two phase
– The entries are arranged in the list satisfying
the requirement of a heap
– We repeatedly remove the top of the heap
and promote another entry to take its place.
![Page 17: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/17.jpg)
Construction of heap
• First construct a complete binary tree
• There are two types of heap- min heap or ascending heap or max heap or descending heap
![Page 18: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/18.jpg)
• Consider the elements present at a level one less than the maximum level. They are converted to heap in the same way as replacing the node of a heap.
• In next step the elements that are present at a level two less than the maximum level of the tree are considered
• Likewise in each step one level is decremented and all the subtrees at that level are converted to heaps
• Eg 13, 4, 11, 15, 59, 27, 19, 3, 92, 5
![Page 19: Threaded binarytree&heapsort](https://reader038.vdocuments.us/reader038/viewer/2022100517/55660e4bd8b42a06318b4a35/html5/thumbnails/19.jpg)
Phase two
• Remove the root of tree and place it to last and re-create the heap in descending order and repeat the process.
• Solution on board.