0% found this document useful (0 votes)
93 views

Unit 2 Dsa Tree 2022 Compressed

Uploaded by

Gaurav Malode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Unit 2 Dsa Tree 2022 Compressed

Uploaded by

Gaurav Malode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 153

“TREE”

Prepared By
Prof. Anand N. Gharu
(Assistant Professor)
PVGCOE Computer Dept.

CLASS : SE COMPUTER 2019


SUBJECT : DSA (SEM-II) 14 FEB 2022
.UNIT : II
SYLLABUS
SYLLABUS
Tree- basic terminology, General tree and its representation,
representation using sequential and linked organization, Binary
tree- properties, converting tree to binary tree, binary tree
traversals(recursive and non-recursive)- inorder, preorder, post
order, depth first and breadth first, Operations on binary tree.
Huffman Tree (Concept and Use), Binary Search Tree (BST), BST
operations, Threaded binary search tree- concepts, threading,
insertion and deletion of nodes in in-order threaded binary search
tree, in order traversal of in-order threaded binary search tree.
UNIT-I I
TREE
TERMINOLOGIES IN TREE
 “A tree is a nonlinear hierarchical data
structure that consists of nodes connected by
edges.”
Why Tree Data Structure?
Other data structures such as arrays, linked list, stack, and
queue are linear data structures that store data sequentially.
In order to perform any operation in a linear data structure,
the time complexity increases with the increase in the data
size. But, it is not acceptable in today's computational world.

Different tree data structures allow quicker and easier access


to the data as it is a non-linear data structure.
Terminologies of Tree
• Root :
It is the topmost node of a tree.

• Edge :
It is the link between any two nodes.

• Node :
A node is an entity that contains a key or value and pointers
to its child nodes.
Terminologies of Tree
Terminologies of Tree
• Child node: If the node is a descendant of any node, then
the node is known as a child node.

• Parent: If the node contains any sub-node, then that node


is said to be the parent of that sub-node.

• Sibling: The nodes that have the same parent are known
as siblings.
Terminologies of Tree
• Leaf − The node which does not have any child node is
called the leaf node.

• Subtree − Subtree represents the descendants of a node.

• Path − Path refers to the sequence of nodes along the


edges of a tree
Terminologies of Tree
• Traversing − Traversing means passing through nodes in
a specific order.

• Levels − Level of a node represents the generation of a


node. If the root node is at level 0, then its next child node
is at level 1, its grandchild is at level 2, and so on.

• keys − Key represents a value of a node based on which a


search operation is to be carried out for a node.
Terminologies of Tree
Leaf/External node: Node with no children.

Internal node:
The node having at least a child node is called an internal
node

• Height of a Node :
The height of a node is the number of edges from the node
to the deepest leaf (ie. the longest path from the node to a
leaf node).
Terminologies of Tree
The depth of a node is the number of edges from the root
to the node.
• Height of a Tree :
The height of a Tree is the height of the root node or the
depth of the deepest node.
Terminologies of Tree
• Degree of a Node :
The degree of a node is the total number of branches of that
node.

• Forest :
A collection of disjoint trees is called a forest.
You can create a forest by cutting the root of a tree.
Applications of Tree
• Binary Search Tree (BST) is used to check whether
elements present or not.
• Heap is a type of tree that is used to heap sort.
• Tries are the modified version of the tree used in modem
routing to information of the router.
• The widespread database uses B-tree.
• Compilers use syntax trees to check every syntax of a
program.
Advantages of Tree
1. Trees reflect structural relationships in the data.

2. Trees are used to represent hierarchies.

3. Trees provide an efficient insertion and searching.

4. Trees are very flexible data, allowing to move

subtrees around with minumum effort.


Types of Tree
• General Tree
• Binary Tree
• Binary Search Tree
• AVL Tree
• B-Tree
General Tree
• General Tree
• The general tree is the type of tree where there are no
constraints on the hierarchical structure.
• Properties
• The general tree follows all properties of the tree data structure.
• A node can have any number of nodes.
Binary Tree
• A binary tree has the following properties:

• Properties
• Follows all properties of the tree data structure.
• Binary trees can have at most two child nodes.
• These two children are called the left child and the right child.
Binary Tree
• A binary tree has the following properties:

• At each level of i, the maximum number of nodes is 2i.


• The height of the tree is defined as the longest path from the root
node to the leaf node. The tree which is shown above has a height
equal to 3. Therefore, the maximum number of nodes at height 3 is
equal to (1+2+4+8) = 15. In general, the maximum number of nodes
possible at height h is (20 + 21 + 22+….2h) = 2h+1 -1.
• The minimum number of nodes possible at height h is equal to h+1.
• If the number of nodes is minimum, then the height of the tree
would be maximum. Conversely, if the number of nodes is
maximum, then the height of the tree would be minimum
Binary Tree Representation
A node of a binary tree is represented by a structure containing a data
part and two pointers to other structures of the same type.

struct node

int data;

struct node *left;

struct node *right;

};
Types of Binary Tree
• Full Binary tree: It is a special type of binary tree. In this tree data
structure, every parent node or an internal node has either two
children or no child nodes.

• .
Types of Binary Tree
• Perfect binary tree: In this type of tree data structure, every
internal node has exactly two child nodes and all the leaf nodes are
at the same level.

• .
Binary Tree
• Complete binary tree: It resembles that of the full binary tree with
a few differences.
1. Every level is completely filled.
2. The leaf nodes lean towards the left of the tree.
3. It is not a requirement for the last leaf node to have the right sibling,
i.e. a complete binary tree doesn’t have to be a full binary tree.
Binary Tree
• Skewed binary tree: It is a pathological or degenerate tree where
the tree is dominated by either the left nodes or the right nodes.
Therefore, there are two types of skewed binary trees, i.e. left-
skewed or the right-skewed binary tree.
Binary Tree
• Balanced binary tree: The difference between the height of the left
and right sub tree for each node is either 0 or 1.
Static Binary Tree Representation :
• Array Representation :
• Binary tree using array represents a node which is numbered sequentially level
by level from left to right. Even empty nodes are numbered.

• Array index is a value in tree nodes and array value gives to the parent node of
that particular index or node. Value of the root node index is always -1 as there
is no parent for root. When the data item of the tree is sorted in an array, the
number appearing against the node will work as indexes of the node in an
array.

• Location number of an array is used to store the size of the tree. The first index
of an array that is '0', stores the total number of nodes. All nodes are numbered
from left to right level by level from top to bottom. In a tree, each node having
an index i is put into the array as its i th element.
Binary Tree Representation using Array
Dynamic -Binary Tree Representation
Linked representation :
Binary trees in linked representation are stored in the memory
as linked lists. These lists have nodes that aren’t stored at
adjacent or neighboring memory locations and are linked to
each other through the parent-child relationship associated
with trees.

In this representation, each node has three different parts –


• pointer that points towards the right node,
• pointer that points towards the left node,
• data element.
Binary Tree Representation
Binary Tree Representation
Binary Tree Representation
Linked representation :
This is the more common representation. All binary trees
consist of a root pointer that points in the direction of the
root node. When you see a root node pointing towards null or
0, you should know that you are dealing with an empty binary
tree. The right and left pointers store the address of the right
and left children of the tree
Operations of Binary Tree
Searching: For searching element 2, we have to traverse all
elements (assuming we do breadth first traversal). Therefore,
searching in binary tree has worst case complexity of O(n).
Insertion: For inserting element as left child of 2, we have to
traverse all elements. Therefore, insertion in binary tree has
worst case complexity of O(n).
Deletion: For deletion of element 2, we have to traverse all
elements to find 2 (assuming we do breadth first traversal).
Therefore, deletion in binary tree has worst case complexity of
O(n).
Binary Tree Examples
Binary Tree Examples
Binary Tree Examples
Binary Tree Examples
Advantages and Disadvantages
Binary Search Tree
• Binary search tree is a data structure that quickly
allows us to maintain a sorted list of numbers.

1. It is called a binary tree because each tree node has a


maximum of two children.
2. It is called a search tree because it can be used to search
for the presence of a number in O(log(n)) time
Binary Search Tree
• Properties
• Follows all properties of the tree data structure.
1. The value of the key of the left sub-tree is less than the
value of its parent (root) node's key.

2. The value of the key of the right sub-tree is greater than or


equal to the value of its parent (root) node's key.

3. Both subtrees of each node are also BSTs i.e. they have the
above two properties
Binary Search Tree
• The binary tree on the right isn't a binary search tree
because the right subtree of the node "3" contains a
value smaller than it.
Operation of Binary Search Tree
• Insert − Inserts an element in a tree/create a tree.

• Search − Searches an element in a tree.

• Preorder Traversal − Traverses a tree in a pre-order


manner.

• Inorder Traversal − Traverses a tree in an in-order


manner.

• Postorder Traversal − Traverses a tree in a post-order


manner.
Binary Search Tree
Binary Search Tree
Binary Search Tree
Create the binary search tree using the following data elements.
15, 11, 13, 8, 9, 17, 16, 18
Binary Search Tree
Binary Search Tree
Create the binary search tree using the following data elements.
43, 10, 79, 90, 12, 54, 11, 9, 50
Operation of Binary Search Tree
1. Create: creates an empty tree.

2. Insert: insert a node in the tree.

3. Search: Searches for a node in the tree.

4. Delete: deletes a node from the tree.

5. Inorder: in-order traversal of the tree.

6. Preorder: pre-order traversal of the tree.

7. Postorder: post-order traversal of the tree


Operation of Binary Search Tree
1. Search :
Operation of Binary Search Tree
1. Search :

Algorithm:
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
Operation of Binary Search Tree
Operation of Binary Search Tree
2. Insert :

Algorithm:
If node == NULL
return createNode(data)
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
Operation of Binary Search Tree
Operation of Binary Search Tree
Operation of Binary Search Tree
Operation of Binary Search Tree
Operation of Binary Search Tree
3. Delete :

Algorithm:

Case 1: Deleting a leaf node

We use the following steps to delete a leaf node from BST...

Step 1 - Find the node to be deleted using search operation

Step 2 - Delete the node using free function (If it is a leaf) and

terminate the function.


Operation of Binary Search Tree
3. Delete :
Algorithm:

Case 2: Deleting a node with one child


We use the following steps to delete a node with one child from
BST...
Step 1 - Find the node to be deleted using search operation

Step 2 - If it has only one child then create a link between its
parent node and child node.

Step 3 - Delete the node using free function and terminate the
function.
Operation of Binary Search Tree
3. Delete :
Algorithm:
Case 3: Deleting a node with two children
We use the following steps to delete a node with two children
from BST...

Step 1 - Find the node to be deleted using search operation


Step 2 - If it has two children, then find the largest node in its left
subtree (OR) the smallest node in its right subtree.
Step 3 - Swap both deleting node and node which is found in the
above step.
Step 4 - Then check whether deleting node came to case 1 or case
2 or else goto step 2
Operation of Binary Search Tree
3. Delete :
Algorithm:
Case 3: Deleting a node with two children
We use the following steps to delete a node with two children
from BST...

Step 5 - If it comes to case 1, then delete using case 1 logic.


Step 6- If it comes to case 2, then delete using case 2 logic.
Step 7 - Repeat the same process until the node is deleted from
the tree.
Convert a Generic Tree (N-array Tree) to
Binary Tree
Following are the rules to convert a Generic(N-array
Tree) to Binary Tree:

1. The root of the Binary Tree is the Root of the Generic


Tree.

2. The left child of a node in the Generic Tree is the Left


child of that node in the Binary Tree.

3. The right sibling of any node in the Generic Tree is the


Right child of that node in the Binary Tree.
61
Convert a Generic Tree(N-array Tree) to Binary Tree
Examples:
Convert the following Generic Tree to Binary Tree:

62
Convert a Generic Tree(N-array Tree) to Binary Tree
Below is the Binary Tree of the above Generic Tree:

63
Convert a Generic Tree(N-array Tree) to Binary Tree
Note: If the parent node has only the right child in the
general tree then it becomes the rightmost child node of the
last node following the parent node in the binary tree.

In the above example, if node B has the right child node L


then in binary tree representation L would be the right child
of node D.

64
Convert a Generic Tree(N-array Tree) to Binary Tree
Examples:
Convert the following Generic Tree to Binary Tree:

65
Convert a Generic Tree(N-array Tree) to Binary Tree
Examples:
Convert the following Generic Tree to Binary Tree:

66
Convert a Generic Tree(N-array Tree) to Binary Tree
Examples:
Convert the following Generic Tree to Binary Tree:

67
Convert a Generic Tree(N-array Tree) to Binary Tree
Examples:

68
Convert a Generic Tree(N-array Tree) to Binary Tree
Examples:

69
Convert a Generic Tree(N-array Tree) to Binary Tree
Examples:

70
Convert a Generic Tree(N-array Tree) to Binary Tree
Examples:

71
Convert a Generic Tree(N-array Tree) to Binary Tree
Examples:

72
Tree Traversal
Traversal of the tree in data structures is a process of visiting each node
and prints their value. There are three ways to traverse tree data
structure.

1. Pre-order Traversal

2. In-Order Traversal

3. Post-order Traversal
Tree Traversal
Tree traversal means traversing or visiting each node of a tree.
Linear data structures like Stack, Queue, linked list have only
one way for traversing, whereas the tree has various ways to
traverse or visit each node. The following are the three different
ways of traversal:

1. Inorder traversal

2. Preorder traversal

3. Postorder traversal
Inorder Traversal
In the in-order traversal, the left subtree is visited first, then the root, and
later the right subtree.

Algorithm:

Step 1- Recursively traverse the left subtree

Step 2- Visit root node

Step 3- Recursively traverse right subtree


Pre-order Traversal
In pre-order traversal, it visits the root node first, then the left subtree,
and lastly right subtree.

Algorithm:

Step 1- Visit root node

Step 2- Recursively traverse the left subtree

Step 3- Recursively traverse right subtree


Post-order Traversal
It visits the left subtree first in post-order traversal, then the right subtree,
and finally the root node.

Algorithm:
Step 1- Recursively traverse the left subtree
Step 2- Recursively traverse right subtree
Step 3- Visit root node
Tree Traversal Example
Tree Traversal Example
Tree Traversal Example

Preorder Traversal - 100 , 20 , 10 , 30 , 200 , 150 , 300


Inorder Traversal- 10 , 20 , 30 , 100 , 150 , 200 , 300
Postorder Traversal : 10 , 30 , 20 , 150 , 300 , 200 , 100
Tree Traversal Example
Tree Traversal Example
Tree Traversal Example
Tree Traversal Example
Tree Traversal Example
Inorder Traversal (recursive version)
void inorder(tree_pointer ptr)
/* inorder tree traversal */
{
A/ B * C * D+ E
if (ptr) {
inorder(ptr->left_child);
printf(“%d”, ptr->data);
indorder(ptr->right_child);
}
} 86
Preorder Traversal (recursive version)
void preorder(tree_pointer ptr)
/* preorder tree traversal */
{
+ * * /AB C D E
if (ptr) {
printf(“%d”, ptr->data);
preorder(ptr->left_child);
predorder(ptr->right_child);
}
} 87
Postorder Traversal (recursive version)
void postorder(tree_pointer ptr)
/* postorder tree traversal */
{
AB / C * D * E +
if (ptr) {
postorder(ptr->left_child);
postdorder(ptr->right_child);
printf(“%d”, ptr->data);
}
} 88
DFS Algorithms
Depth first search (DFS) algorithm starts with the initial node of the
graph G, and then goes to deeper and deeper until we find the goal
node or the node which has no children. The algorithm, then
backtracks from the dead end towards the most recent node that is
yet to be completely unexplored.

The data structure which is being used in DFS is stack. The process
is similar to BFS algorithm. In DFS, the edges that leads to an
unvisited node are called discovery edges while the edges that leads
to an already visited node are called block edges.
DFS Algorithms
DFS is an algorithm for finding or traversing graphs or trees
in depth-ward direction. The execution of the algorithm
begins at the root node and explores each branch before
backtracking. It uses a stack data structure to remember, to
get the subsequent vertex, and to start a search, whenever a
dead-end appears in any iteration.

The full form of DFS is Depth-first search.


DFS Algorithms
DFS Algorithms
Step 1: SET STATUS = 1 (ready state) for each node in G

Step 2: Push the starting node A on the stack and set its STATUS = 2
(waiting state)

Step 3: Repeat Steps 4 and 5 until STACK is empty

Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed
state)
Step 5: Push on the stack all the neighbours of N that are in the ready state
(whose STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
DFS Algorithms Example

The printing sequence of the graph will be :


H→A→D→F→B→C→G→E
Complexity of DFS Algorithms
Complexity of Depth First Search

The time complexity of the DFS algorithm is

represented in the form of O(V + E), where V is the

number of nodes and E is the number of edges.

The space complexity of the algorithm is O(V).


Application of DFS Algorithms
1. For finding the path

2. To test if the graph is bipartite

3. For finding the strongly connected components of a

graph

4. For detecting cycles in a graph


Example of DFS Algorithms
BFS Algorithms
1. Breadth-first search is a graph traversal algorithm that starts
traversing the graph from the root node and explores all the
neighboring nodes. Then, it selects the nearest node and
explores all the unvisited nodes. While using BFS for traversal,
any node in the graph can be considered as the root node.

2. BFS is the most commonly used approach. It is a recursive


algorithm to search all the vertices of a tree or graph data
structure. BFS puts every vertex of the graph into two categories
- visited and non-visited. It selects a single node in a graph and,
after that, visits all the nodes adjacent to the selected node
BFS Algorithms
BFS Algorithms
BFS Algorithms
Rule 1 − Visit the adjacent unvisited vertex. Mark it as
visited. Display it. Insert it in a queue.

Rule 2 − If no adjacent vertex is found, remove the first


vertex from the queue.

Rule 3 − Repeat Rule 1 and Rule 2 until the queue is


empty.
BFS Algorithms
Step 1: SET STATUS = 1 (ready state) for each node in G

Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)

Step 3: Repeat Steps 4 and 5 until QUEUE is empty

Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed


state).

Step 5: Enqueue all the neighbours of N that are in the ready state (whose
STATUS = 1) and set

their STATUS = 2
(waiting state)
[END OF LOOP]
Example of BFS Algorithms
Complexity of BFS Algorithms
The time complexity of the BFS algorithm is

represented in the form of O(V + E), where V is the

number of nodes and E is the number of edges.

The space complexity of the algorithm is O(V).


Application of DFS Algorithms
1. BFS can be used to find the neighboring locations from a given source
location.

2. In a peer-to-peer network, BFS algorithm can be used as a traversal


method to find all the neighboring nodes. Most torrent clients, such as
BitTorrent, uTorrent, etc. employ this process to find "seeds" and
"peers" in the network.

3. BFS is used to determine the shortest path and minimum spanning tree.

4. BFS is also used in Cheney's technique to duplicate the garbage


collection.

5. It can be used in ford-Fulkerson method to compute the maximum flow


Application of DFS Algorithms
1. To build index by search index

2. For GPS navigation

3. Path finding algorithms

4. Cycle detection in an undirected graph

5. In minimum spanning tree


Application of DFS Algorithms
BFS Vs DFS
sn BFS DFS
BFS finds the shortest path to the DFS goes to the bottom of a subtree,
1
destination. then backtracks.
The full form of BFS is Breadth- The full form of DFS is Depth First
2
First Search. Search.
It uses a queue to keep track of It uses a stack to keep track of the
3
the next location to visit. next location to visit.
BFS traverses according to tree DFS traverses according to tree
4
level. depth.
5 It is implemented using FIFO list. It is implemented using LIFO list.
It requires more memory as It requires less memory as compare
6
BFS Vs DFS
sn BFS DFS
There is no need of There is a need of backtracking
7
backtracking in BFS. in DFS.
You can never be trapped into You can be trapped into infinite
8
finite loops. loops.
If you do not find any goal, you
If you do not find any goal, the
may need to expand many
9 leaf node backtracking may
nodes before the solution is
occur.
found.
Huffman Coding Tree
• Huffman Coding is a technique of compressing data

to reduce its size without losing any of the details. It

was first developed by David Huffman.

• Huffman Coding is generally useful to compress the

data in which there are frequently occurring

characters.
Huffman Coding Tree
• (i) Data can be encoded efficiently using Huffman Codes.

• (ii) It is a widely used and beneficial technique for

compressing data.

• (iii) Huffman's greedy algorithm uses a table of the

frequencies of occurrences of each character to build up an

optimal way of representing each character as a binary

string.
Algorithms of Huffman Coding
Huffman (C)
1. n=|C|
2. Q ← C
3. for i=1 to n-1
4. do
5. z= allocate-Node ()
6. x= left[z]=Extract-Min(Q)
7. y= right[z] =Extract-Min(Q)
8. f [z]=f[x]+f[y]
9. Insert (Q, z)
10. return Extract-Min (Q)
Huffman’s Tree Example
Huffman’s Tree Example
Huffman’s Tree Example
Huffman’s Tree Example
Huffman’s Tree Example
Huffman’s Tree Example
Huffman’s Tree Example
Huffman’s Tree Example
Huffman’s Tree Example
Huffman’s Tree Example
Huffman’s Tree Example
Huffman’s Tree Example
Huffman’s Coding Complexity
1. The time complexity for encoding each unique character

based on its frequency is O(nlog n).

2. Extracting minimum frequency from the priority queue

takes place 2*(n-1) times and its complexity is O(log n).

Thus the overall complexity is O(nlog n).


Applications of Huffman’s Coding
1. Huffman coding is used in conventional compression

formats like GZIP, BZIP2, PKZIP, etc.

2. For text and fax transmissions.


Binary tree vs Binary search tree
Points Binary Tree Binary Search Tree

A Binary Tree is a non-linear


data structure in which a A Binary Search Tree is an
node can have 0, 1 or 2 organized binary tree with a
Definit
nodes. Individually, each structured organization of
ion
node consists of a left pointer, nodes. Each subtree must also
right pointer and data be of that particular structure.
element.
The values of left subtree of a
There is no required particular node should be
Struct
organization structure of the lesser than that node and the
ure
nodes in the tree. right subtree values should be
greater.
Binary tree vs Binary search tree
points Binary Tree Binary Search Tree

Opera As these are sorted binary


The operations that can be
tions trees, they are used for fast
performed are deletion,
Perfor and efficient binary search,
insertion and traversal
med insertion and deletion.
There are several types. Most
common ones are the The most popular ones are
Types Complete Binary Tree, Full AVL Trees, Splay Trees,
Binary Tree, Extended Binary Tango Trees, T-Trees.
Tree
Threaded Binary Tree
1. The idea of threaded binary trees is to make inorder
traversal faster and do it without stack and without
recursion.

2. A binary tree is made threaded by making all right


child pointers that would normally be NULL point to
the inorder successor of the node (if it exists)
Threaded Binary Tree
In the linked representation of binary trees, more than one
half of the link fields contain NULL values which results in
wastage of storage space. If a binary tree consists of n nodes
then n+1 link fields contain NULL values. So in order to
effectively manage the space, a method was devised by Perlis
and Thornton in which the NULL links are replaced with
special links known as threads. Such binary trees with threads
are known as threaded binary trees. Each node in a
threaded binary tree either contains a link to its child node or
thread to other nodes in the tree.
Threaded Binary Tree
Threaded Binary Tree
There are two types of threaded binary trees.

Single Threaded: Where a NULL right pointers is made to


point to the inorder successor (if successor exists)

Double Threaded: Where both left and right NULL pointers


are made to point to inorder predecessor and inorder
successor respectively. The predecessor threads are useful for
reverse inorder traversal and postorder traversal.
Threaded Binary Tree
Threaded Binary Tree
Threaded Binary Tree
Threaded Binary Tree
Threaded Binary Tree
Insertion in Threaded Binary Tree
Insertion in Binary threaded tree is similar to insertion in
binary tree but we will have to adjust the threads after
insertion of each element.

Case 1: Insertion in empty tree

Case 2: When new node inserted as the left child

Case 3: When new node is inserted as the right child


Insertion in Threaded Binary Tree
Following example show a node being inserted as left child
of its parent.
Insertion in Threaded Binary Tree
After insertion of 13 element
Insertion in Threaded Binary Tree
Following example shows a node being inserted as right
child of its parent.
Insertion in Threaded Binary Tree
After 15 inserted
Deletion in Threaded Binary Tree
In deletion, first the key to be deleted is searched, and then
there are different cases for deleting the Node in which key is
found.

Case A: Leaf Node need to be deleted

Case B: Node to be deleted has only one child

Case C: Node to be deleted has two children


Deletion in Threaded Binary Tree
Case A: Leaf Node need to be deleted

In BST, for deleting a leaf Node the left or right pointer of


parent was set to NULL. Here instead of setting the pointer to
NULL it is made a thread.

If the leaf Node is to be deleted is left childs of its parent


then after deletion, left pointer of parent should become a
thread pointing to its predecessor of the parent Node after
deletion.
Deletion in Threaded Binary Tree
Case B: Node to be deleted has only one child

After deleting the Node as in a BST, the inorder successor


and inorder predecessor of the Node are found out.
Deletion in Threaded Binary Tree
Case C: Node to be deleted has two children :

We find inorder successor of Node ptr (Node to be deleted)


and then copy the information of this successor into Node
ptr. After this inorder successor Node is deleted using either
Case A or Case B
Deletion in Threaded Binary Tree
Case A: Leaf Node need to be deleted
Deletion in Threaded Binary Tree
Case A: Leaf Node need to be deleted
Deletion in Threaded Binary Tree
Case B: Node to be deleted has only one child
Deletion in Threaded Binary Tree
Case A: Leaf Node need to be deleted
Advantages Threaded Binary Tree
1. In threaded binary tree, linear and fast traversal of
nodes in the tree so there is no requirement of stack.
If the stack is used then it consumes a lot of memory
and time.

2. It is more general as one can efficiently determine


the successor and predecessor of any node by simply
following the thread and links. It almost behaves like
a circular linked list.
Disadvantages Threaded Binary Tree
1. When implemented, the threaded binary tree needs
to maintain the extra information for each node to
indicate whether the link field of each node points to
an ordinary node or the node's successor and
predecessor.

2. Insertion into and deletion from a threaded binary


tree are more time consuming since both threads and
ordinary links need to be maintained.
Applications Threaded Binary Tree
1. The idea of threaded binary trees is to make inorder
traversal of the binary tree faster and do it without
using any extra space, so sometimes in small
systems where hardware is very limited we use
threaded binary tree for better efficiency of the
software in a limited hardware space.
THANK YOU!!!
My Blog : https://anandgharu.wordpress.com/

Email : gharu.anand@gmail.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy