Unit 2 Dsa Tree 2022 Compressed
Unit 2 Dsa Tree 2022 Compressed
Prepared By
Prof. Anand N. Gharu
(Assistant Professor)
PVGCOE Computer Dept.
• 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.
• 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.
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.
• 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:
struct node
int data;
};
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.
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.
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:
Step 2 - Delete the node using free function (If it is a leaf) and
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...
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.
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:
Algorithm:
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
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.
Step 2: Push the starting node A on the stack and set its STATUS = 2
(waiting state)
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
graph
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting 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
3. BFS is used to determine the shortest path and minimum spanning tree.
characters.
Huffman Coding Tree
• (i) Data can be encoded efficiently using Huffman Codes.
compressing data.
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
Email : gharu.anand@gmail.com