Dsa Unit IV Tree
Dsa Unit IV Tree
Tree
1
Unit IV - Syllabus
3
Non Linear Data Structure (Tree)
4
Tree Definition
Tree is a non-linear data structure which organizes
data in hierarchical structure and this is a recursive
definition.
5
Tree Definition
⚫A tree is a connected graph without any circuits.
⚫If in a graph, there is one and only one path
between every pair of vertices, then graph is
called as a tree.
6
Tree Terminologies
7
Tree Terminologies
8
Tree Terminologies
9
Tree Terminologies
10
Tree Terminologies
● 1.Root:
− The first node is called as Root Node.
− Every tree must have root node, there must
be only one root node.
− Root node doesn't have any parent.
11
Tree Terminologies
12
Tree Terminologies
● 3. Parent: In a tree data structure, the node which is
predecessor of any node is called as PARENT
NODE. In simple words, the node which has branch
from it to any other node is called as parent node.
Parent node can also be defined as "The node which
has child / children".
13
Tree Terminologies
● 4. Child: The node which has a link from its parent
node is called as child node.
● In a tree, any parent node can have any number of
child nodes.
● In a tree, all the nodes except root are child nodes.
14
Tree Terminologies
15
Tree Terminologies
● 6. Leaf Node :
● The node which does not have a child is called as
LEAF Node.
● The leaf nodes are also called as External Nodes or
'Terminal' node.
16
Tree Terminologies
● 7. Internal Node :
● An internal node is a node with at least one child.
● Nodes other than leaf nodes are called as Internal
Nodes.
● The root node is also said to be Internal Node if the
tree has more than one node. Internal nodes are
also called as 'Non-Terminal' nodes.
17
Tree Terminologies
● 8. Degree :
● In a tree data structure, the total number of
children of a node is called as DEGREE of that
Node.
18
Tree Terminologies
● 9. Level :
● In a tree data structure, the root node is said to be
at Level 0 and the children of root node are at
Level 1 and the children of the nodes which are at
Level 1 will be at Level 2 and so on...
● In a tree each step from top to bottom is called as a
Level and the Level count starts with '0' and
incremented by one.
19
Tree Terminologies
● 10. Height :
● the total number of edges from leaf node to a
particular node in the longest path is called as
HEIGHT of that Node.
● In a tree, height of the root node is said to be height
of the tree.
20
Tree Terminologies
● 11. Depth :
● In a tree data structure, the total number of egdes
from root node to a particular node is called as
DEPTH of that Node.
21
Tree Terminologies
● 12. Path :
● In a tree data structure, the sequence of Nodes and
Edges from one node to another node is called as
PATH between that two Nodes.
● Length of a Path is total number of nodes in that
path. In below example the path A - B - E - J has
length 4.
22
Tree Terminologies
● 13. Sub Tree :
● Every child node will form a subtree on its parent
node.
23
Types of Trees
• General tree
• Binary tree
• Binary Search Tree
24
Types of Trees
• General tree
• Binary tree
• Binary Search Tree
25
General Tree
• A general tree is a data structure in that each node
can have infinite number of children .
• In general tree, root has in-degree 0 and maximum
out-degree n.
• Height of a general tree is the length of longest path
from root to the leaf of tree. Height(T) =
{max(height(child1) , height(child2) , … height(child-
n) ) +1}
26
Binary Tree
• A Binary tree is a data structure in that each node has at most two nodes
left and right
• In binary tree, root has in-degree 0 and maximum out-degree 2.
• In binary tree, each node have in-degree one and maximum out-degree 2.
27
Representation of Binary Tree
1. Array Representation
2. Linked List Representation.
28
Array Representation of Tree
1. To represent a tree in one dimensional array nodes are
marked sequentially from left to right start with root
node.
2. First array location can be used to store no of nodes in a
tree.
29
Linked List Representation of Tree
1. This type of representation is more efficient
as compared to array.
2. Left and right are pointer type fields left holds
address of left child and right holds address of
right child.
3. Struct node
{
struct node * left;
int data;
struct node *right
};
30
Binary Tree Types
1. Extended Binary Tree
2. Complete Binary Tree
3. Full Binary Tree
4. Skewed Binary Tree
5. Strictly Binary Tree
6. Expression Binary tree
31
Complete Binary Tree
A complete binary tree is a tree in which
1.All leaf nodes are at n or n-1 level
2.Levels are filled from left to right
32
Extended Binary Tree
An extended binary tree is a transformation of
any binary tree into a complete binary tree. This
transformation consists of replacing every null
subtree of the original tree with “special nodes” or
“failure nodes” .The nodes from the original tree are
then internal nodes, while the “special nodes” are
external nodes.
33
Full Binary Tree
A full binary tree (sometimes proper binary
tree or 2-tree) is a tree in which every node
other than the leaves has two children or no
children. Every level is completely filled up.
No of nodes= 2h+1 -1
34
Skewed Binary Tree
A binary tree is said to be Skewed Binary
Tree if every node in the tree contains either
only left or only right sub tree. If the node
contains only left sub tree then it is
called left-skewed binary tree and if the tree
contains only right sub tree then it is
called right-skewed binary tree.
35
Strictly Binary Tree
36
Expression Binary Tree
• Expression trees are a special kind of binary tree used to
evaluate certain expressions.
• Two common types of expressions that a binary
expression tree can represent are algebraic and Boolean.
• These trees can represent expressions that contain both
unary and binary operators.
• The leaves of a binary expression tree are operands, such
as constants or variable names, and the other nodes
contain operators.
• Expression tree are used in most compilers.
Expression:→(3 + 5) * (6 - 4)
37
Expression Binary Tree
Structure of an Expression Tree:
*
/ \
+ -
/\ /\
3 5 6 4
38
Expression Binary Tree
Expression:→???
39
Binary tree as an ADT
Here are some basic operations that define the functionality of a binary tree
ADT:
1.Create/Initialize an Empty Binary Tree: Initializes an empty tree with no
nodes or root.
2.Insert (value):Adds a node containing the value to the binary tree,
following specific rules for placement (in binary search trees, this is done
based on key comparisons).
3.Search (value):Checks whether a node with a specified value exists in the
tree.
4.Delete (value):Removes the node containing the specified value,
maintaining the tree structure. In the case of a binary search tree, special
handling is needed to replace the node correctly.
5.Traversal:
a) Pre-order traversal: Visit the root, then the left subtree, then the right subtree.
b) In-order traversal: Visit the left subtree, then the root, then the right subtree.
c) Post-order traversal: Visit the left subtree, then the right subtree, then the root.
d) Level-order traversal: Visit nodes level by level (breadth-first search).
6.Get Root: Returns the root of the binary tree.
40
Binary tree Traversal
1
/\
2 3
/\ \
4 5 6
1. Preorder traversal:-In this traversal method first process root
element, then left sub tree and then right sub tree.
Procedure:-
Step 1: Visit root node
Step 2: Visit left sub tree in preorder
41 Step 3: Visit right sub tree in preorder
Binary tree Traversal
1
/\
2 3
/\ \
4 5 6
2. Inorder traversal:-
In this traversal first visit / process left sub tree, then right sub tree
and then the root element.
Procedure:-
Step 1: Visit left sub tree in postorder
Step 2: Visit right sub tree in postorder
43 Step 3: Visit root node
Binary tree Traversal
Perform inorder, preorder and
postorder traversal of given
tree.
44
Binary tree Traversal
45
Binary tree Traversal
Perform inorder, preorder and
postorder traversal of given
tree.
46
Binary tree Traversal
47
Binary tree Traversal
48
Binary tree Traversal
49
Binary Search Tree
A binary search tree (BST) or "ordered binary
tree" is a empty or in which each node contains a
key that satisfies following conditions:
1. All keys are distinct.’
2. , all elements in its left subtree are less to the node
(<), and all the elements in its right subtree are greater than
the node (>).
50
A binary search tree Not a binary search tree
BST (Insert Operation)
struct node
{
int key;
struct node *left, *right;
};
// Create a node
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
51
BST (Insert Operation)
// Insert a node
struct node *insert(struct node *root, int key)
{
// Return a new node if the tree is empty
if (root == NULL)
return newNode(key);
return root;
}
52
BST(search Operation)
// Search for a value in the BST
struct node* search(struct node *root, int key)
{
if (root == NULL || root->value == value)
{
return root;
}
54
BST(delete Operation)
55
BST(delete Operation)
56
BST(level wise Display)
57
BST(level wise Display)
58
BST(In order Recursive)
// Inorder Traversal
void inorder(struct node *root)
{ struct node
if (root != NULL) {
{ int key;
// Traverse left struct node *left, *right;
inorder(root->left); };
// Traverse root
cout << root->key << " -> ";
// Traverse right
inorder(root->right);
}
}
59
BST(Pre order Recursive)
// pre order Traversal
void preorder(struct node *root)
{ struct node
if (root != NULL) {
{ int key;
// Traverse root struct node *left, *right;
cout << root->key << " };-> ";
// Traverse left
preorder(root->left);
// Traverse right
preorder(root->right);
}
}
60
BST(Post order Recursive)
// post order Traversal
void postorder(struct node *root)
{ struct node
if (root != NULL) {
{ int key;
// Traverse left struct node *left, *right;
postorder(root->left); };
// Traverse right
postorder(root->right);
// Traverse root
cout << root->key << " -> ";
}
61
}
Threaded binary tree
The idea of threaded binary trees is to make inorder
traversal faster and do it without stack and without
recursion. 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).
A threaded binary tree is a type of binary tree data structure
where the empty left and right child pointers in a binary tree
are replaced with threads that link nodes directly to their in-
order predecessor or successor, thereby providing a way to
traverse the tree without using recursion or a stack.
Threaded binary trees can be useful when space is a
concern, as they can eliminate the need for a stack during
traversal. However, they can be more complex to implement
than standard binary trees.
62
Threaded binary tree
63
Threaded binary tree
64
Threaded binary tree
65
Tree Applications
66