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

tree data structure (5)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

tree data structure (5)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

TREE

DATA STRUCTURE
Notes By RajKumar Sharma
A tree is a nonlinear hierarchical data structure
that consists of nodes connected by edges.
tree data structures allow quicker and easier access to the data as it is a non-linear data structure.

In the Tree data structure, the topmost node is known as a root node. Each node contains some data, and data can be
of any type.
1.Root: The root node is the topmost node in the tree hierarchy. In
other words, the root node is the one that doesn't have any
parent. In the above structure, node numbered 1 is the root node
of the tree. If a node is directly linked to some other node, it would
be called a parent-child relationship.

2.A link connecting one node to another is called an edge.


3.Child node: If the node is a descendant(child) of any node,
then the node is known as a child node.
4.Parent: If the node contains any sub-node, then that node is
said to be the parent of that sub-node.

5.Sibling: The nodes that have the same parent are


known as siblings.

6.The tree size is the number of nodes in the tree.

7.Leaf Node:- The node of the tree, which doesn't have


any child node, is called a leaf node
Types of Tree data structure
1.General tree: The general tree is one of the types of tree data structure. In
the general tree, a node can have either 0 or maximum n number of nodes
The topmost node in a general tree is known as a root node. The children
of the parent node are known as subtrees.

There can be n number of subtrees in a


general tree. In the general tree, the
subtrees are unordered as the nodes in the
subtree cannot be ordered.
Binary tree: Here, binary name itself suggests two numbers, i.e., 0 and 1. In
a binary tree, each node in a tree can have utmost two child nodes. Here,
utmost means whether the node has 0 nodes, 1 node or 2 nodes.

Types of Binary Tree

1. Full/ proper/ strict Binary tree


2. Complete Binary tree
3. Balanced Binary tree
4. Perfect Binary Tree
5.Degenerate Binary tree
Full/ proper/ strict Binary tree
The full binary tree is also known as a strict binary tree. The tree can only
be considered as the full binary tree if each node must contain either 0 or
2 children. The full binary tree can also be defined as the tree in which
each node must contain 2 children except the leaf nodes.

In the above tree, we can observe that each


node is either containing zero or two children;
therefore, it is a Full Binary tree.
Complete Binary Tree
The complete binary tree is a tree in which all the nodes are completely filled
except the last level. In the last level, all the nodes must be as left as possible.
In a complete binary tree, the nodes should be added from the left.

The above tree is a complete binary tree


because all the nodes are completely filled,
and all the nodes in the last level are added at
the left first.
Balanced Binary Tree
A balanced binary tree, also referred to as a height-balanced binary tree,
is defined as a binary tree in which the height of the left and right subtree
of any node differ by not more than 1.

difference between the left and the right subtree for any node is not more
than one
the left subtree is balanced
the right subtree is balanced

Unbalanced Binary Tree


with depth at each level
Perfect Binary Tree
A tree is a perfect binary tree if all the internal nodes have 2 children, and
all the leaf nodes are at the same level.

The below tree is not a perfect binary tree


because all the leaf nodes are not at the
same level.
Note: All the perfect binary trees are the complete binary trees as well as
the full binary tree,
Degenerate Binary tree
The degenerate binary tree is a tree in which all the internal nodes have only one children.

The above tree is a degenerate binary tree


because all the nodes have only one child. It
is also known as a right-skewed tree as all the
nodes have a right child only.

The above tree is also a degenerate binary


tree because all the nodes have only one
child. It is also known as a left-skewed tree
as all the nodes have a left child only.
What is a Binary Search tree?
A binary search tree follows some order to arrange the elements. In a Binary search tree,
the value of left node must be smaller than the parent node, and the value of right node
must be greater than the parent node. This rule is applied recursively to the left and right
subtrees of the root.

In the above figure, we can observe that the root


node is 40, and all the nodes of the left subtree
are smaller than the root node, and all the nodes
of the right subtree are greater than the root node
Similarly, we can see the left child of root node is
greater than its left child and smaller than its right
child. So, it also satisfies the property of binary
search tree. Therefore, we can say that the tree in
the above image is a binary search tree.
In the above tree, the value of root node is 40, which is
greater than its left child 30 but smaller than right child of
30, i.e., 55. So, the above tree does not satisfy the property
of Binary search tree. Therefore, the above tree is not a
binary search tree
Example of creating a binary search tree
Now, let's see the creation of binary search tree using an example.
Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
First, we have to insert 45 into the tree as the root of the tree.
Then, read the next element; if it is smaller than the root node, insert it as
the root of the left subtree, and move to the next element.
Otherwise, if the element is larger than the root node, then insert it as the
root of the right subtree.

Now, let's see the process of creating the


Binary search tree using the given data
element. The process of creating the BST is
shown below -
Step 1 - Insert 45.
Step 2 - Insert 15.
As 15 is smaller than 45, so insert it as the root node of the left subtree.

Step 3 Insert 79.


As 79 is greater than 45, so insert it as the root node of
the right subtree.
Step 4 - Insert 90.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.

Step 5 - Insert 10.


10 is smaller than 45 and 15, so it will be inserted
as a left subtree of 15.
Step 6 - Insert 55.
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree
of 79.
Step 7 - Insert 12.
12 is smaller than 45 and 15 but greater
than 10, so it will be inserted as the right
subtree of 10.
Step 8 - Insert 20.
20 is smaller than 45 but greater than 15, so it will be inserted as the right
subtree of 15.

Step 9 - Insert 50.


50 is greater than 45 but smaller than 79 and
55. So, it will be inserted as a left subtree of 55.
Tree traversal (Inorder, Preorder an Postorder)
Traversal is a process to visit all the nodes of a tree and may print their values
too. Because, all nodes are connected via edges links we always start from the
root head node. That is, we cannot random access a node in tree.
There are three ways which we use to traverse a tree −
Preorder traversal
Inorder traversal
Postorder traversal
Preorder traversal
This technique follows the 'root left right' policy. It means that, first root node is
visited after that the left subtree is traversed recursively, and finally, right subtree
is recursively traversed. As the root node is traversed before (or pre) the left and
right subtree, it is called preorder traversal
Algorithm
Until all nodes of the tree are not visited
Step 1 - Visit the root node
Step 2 - Traverse the left subtree recursively.
Step 3 - Traverse the right subtree recursively.

A→B→D→E→C→F→G
Preorder traversal

start applying the preorder traversal on the above tree. First, we traverse the root
node A; after that, move to its left subtree B, which will also be traversed in
preorder.
So, for left subtree B, first, the root node B is traversed itself; after that, its left
subtree D is traversed. Since node D does not have any children, move to right
subtree E. As node E also does not have any children, the traversal of the left
subtree of root node A is completed.
Now, move towards the right subtree of root node A that is C. So, for right subtree
C, first the root node C has traversed itself; after that, its left subtree F is
traversed. Since node F does not have any children, move to the right subtree G.
As node G also does not have any children, traversal of the right subtree of root
node A is completed.
Therefore, all the nodes of the tree are traversed. So, the output of the preorder
traversal of the above tree is -
Postorder traversal
This technique follows the 'left-right root' policy. It means that the first left
subtree of the root node is traversed, after that recursively traverses the right
subtree, and finally, the root node is traversed. As the root node is traversed after
(or post) the left and right subtree, it is called postorder traversal.
Algorithm
Until all nodes of the tree are not visited
Step 1 - Traverse the left subtree recursively.
Step 2 - Traverse the right subtree recursively.
Step 3 - Visit the root node.

D→E→B→F→G→C→A
Inorder traversal
This technique follows the 'left root right' policy. It means that first left subtree is
visited after that root node is traversed, and finally, the right subtree is traversed.
As the root node is traversed between the left and right subtree, it is named
inorder traversal.
Algorithm
Until all nodes of the tree are not visited
Step 1 - Traverse the left subtree recursively.
Step 2 - Visit the root node.
Step 3 - Traverse the right subtree recursively.

D→B→E→A→F→C→G
Example 1 : Consider the binary Tree Traverse it using,
(a) Preorder Traversal
(b) Inorder Traversal
(c) Postorder Traversal
Consider the binary Tree T shown in Fig. Traverse it using,
(a) Preorder Traversal
(b) Inorder Traversal
(c) Postorder Traversa
binary Tree T has 8 nodes. The inorder and preorder traversals results into
following sequences of nodes :

Inorder : 56 20 78 2 10 90 30 100
Preorder : 10 20 56 78 2 30 90 100

step-1 10

Inorder : 56 20 78 2 Inorder : 90 30 100


Preorder : 20 56 78 2 Preorder : 30 90 100

step-2 10
Inorder : 90 30 100
Inorder : 56 20 78 2 Preorder : 30 90 100
Preorder : 20 56 78 2
20
step-3
56 Inorder : 78 2
Preorder : 78 2
10
Inorder : 90 30 100
Inorder : 56 20 78 2 Preorder : 30 90 100
Preorder : 20 56 78 2
20
step-3
56 Inorder : 78 2
Preorder : 78 2

Inorder : 90 30 100

step-4 10
Preorder : 30 90 100
step-5
20 30
10
Inorder : 90 30 100
100
56
Preorder : 30 90 100
20 78 90
56 78 2

2
Q1A binary Tree T has 9 nodes. The inorder and preorder
traversals results into following sequences of nodes :

Inorder : E A C K F H D B G
Preorder : F A E K C D H G B

Construct Tree from Given


Inorder and Preorder Traversals
Tree traversal algorithms are fundamental in
understanding and reconstructing binary trees. Given
the Inorder and Preorder traversals of a binary tree, it is
possible to reconstruct the original tree.
Difference between Binary Tree and Binary Search Tree:
Feature Binary Tree Binary Search Tree ( BST )

A tree data structure


A binary tree in which for each node, all elements in its left subtree are
where each node can
Definition less than the node, and all elements in its right subtree are greater than
have at most two
the node.
children nodes.

Nodes are inserted


Nodes are inserted according to their values, maintaining the BST
Node Insertion without any specific
property.
order.
Q1. Construct Tree from Given Inorder and Preorder
Traversals
Heap Data Structure
What is Heap?
A heap is a complete binary tree, and the binary tree is a tree in which the node can have utmost two children

What is a complete binary tree?


A complete binary tree is a binary tree in which all the levels except the last level, i.e., leaf node should be completely filled,
and all the nodes should be left-justified.

The above figure shows that all the internal nodes are
completely filled except the leaf node, but the leaf nodes are
added at the right part; therefore, the above tree is not a
we can observe that all the internal nodes are completely complete binary tree.
filled except the leaf node; therefore, we can say that the
above tree is a complete binary tree.
How can we arrange the nodes in the Tree?
There are two types of the heap:
Min Heap
Max heap

Min-Heap − Where the value of the root node is less than or equal to either of its children.
Let's understand the min-heap through an example.
Max Heap: The value of the parent node is greater than or equal to its children.

Max-Heap − Where the value of the root node is greater than or equal to either of its children.

The above tree is a max heap tree as it satisfies the property of the max heap.
Max Heap Construction Algorithm

Step 1 − Create a new node at the end of heap.


Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
Insertion in the max Heap tree
44, 33, 77, 11, 55, 88, 66
First, we have to insert the element in such a way that the property of the complete binary tree must be
maintained.
Secondly, the value of the parent node should be greater than the either of its child.

Step 1: First we add the 44 element in the tree as shown below:

Step 2: The next element is 33. As we know that insertion in the binary tree always
starts from the left side so 44 will be added at the left of 33 as shown below:
Step 3: The next element is 77 and it will be added to the right of the 44 as shown below:

As we can observe in the above tree that it does not satisfy the
max heap property, i.e., parent node 44 is less than the child 77.
So, we will swap these two values as shown below:
Step 4: The next element is 11. The node 11 is added to the left of 33 as shown below:

Step 5: The next element is 55. To make it a complete binary tree, we will
add the node 55 to the right of 33 as shown below:

As we can observe in the above figure that it does not satisfy


the property of the max heap because 33<55, so we will swap
these two values as shown below:
Step 6: The next element is 88. The left subtree is
completed so we will add 88 to the left of 44 as shown
below:
As we can observe in the above figure that it does not satisfy the property of the max heap because 44<88, so we will
swap these two values as shown below:
Again, it is violating the max heap property because 88>77 so we will swap these two values as shown below:

88

88 77

44
66
The elements 32, 15, 20, 30, 12, 25, 16 are inserted one
by one in the given order into a Max Heap. The
resultant Max Heap is.
Which of the following is the valid min heap?
THANKYOU
Good luck with your upcoming exams,
you'll do amazing!

[email protected]

You might also like