tree data structure (5)
tree data structure (5)
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.
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
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
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
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 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:
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!