Coa Put Solution
Coa Put Solution
TREE
A tree is a non-linear hierarchical data structure that follows a Parent-
Child relationship. It is a collection of nodes where one node is defined
as ROOT node, and this root can have zero or more children.
Tree Structure
Path:
A Path can be defined as the sequence of edges
from the source node to the destination node.
It can also be defined as the sequence of nodes
from the source node to the destination node.
Subtree:
Node with any number of children is called
subtree.
Binary Tree
A tree T is called a binary tree if T has either 0, 1,
or 2 children.
It can not have more than two children.
There is a unique path from the root node to every
other node.
Types of Binary Tree
• Full/Strictly Binary Tree.
• Perfect Binary Tree.
• Complete Binary Tree.
• Almost Complete Binary Tree.
Full/Strictly Binary Tree:
2. Post-order Traversing
3. Inorder Traversing
Tree Traversals
1. Pre-order Traversing
Pre-order traversing is done by using the following steps. These are: -
• 1) Visit the Root Node and print the data part.
• 2) Recursively Traverse the left subtree in Pre-order.
• 3) Recursively Traverse the Right subtree in pre-order.
2. Post-order Traversing
3. Inorder Traversing
ALGORITHM
Preorder_traversal(Root)
BEGIN:
• IF root == NULL THEN
• RETURN 0
• ELSE
• WRITE (root Data)
• Preorder_Traversal(root Left)
• Preorder_Traversal(root Right)
END;
Inorder Traversing
In Inorder traversing, we compute the following steps. These are
• 1) Recursively visit Left Subtree in Inorder.
• 2) Visit Root Node. Write data part.
• 3) Recursively call Right Subtree in Inorder.
ALGORITHM
Inorder_traversal(Root)
BEGIN:
• IF root == NULL THEN
• RETURN 0;
• ELSE
• Inorder_traversal(rootLeft)
• WRITE (rootData)
• Inorder_traversal(rootRight)
END;
Post order Traversing
In Post order traversing, we compute the following steps. These are
• 1) Recursively visit Left Subtree
• 2) Recursively call Right Subtree
• 3) Visit Root Node. Write data part.
ALGORITHM
Postorder_traversal( Root)
BEGIN:
• IF root == NULL THEN
• RETURN 0;
• ELSE
• Postorder_traversal(rootLeft)
• Postorder_traversal(rootRight)
• WRITE (rootData)
END;
Find:
Iorder
Preorder
PostOrder
Find:
Iorder
Preorder
PostOrder
Find:
Iorder
Preorder
PostOrder
Create a tree
Example1:
Preorder A B D H E C F G
Inorder D H B E A F C G
Create a tree
Example1: Example3:
Preorder A B D H E C F G Postorder H D I E B J F K L G C A
Inorder D H B E A F C G Inorder HDBIEAFJ CKGL
Example 4 :
Example2:
Postorder D F E B G L J K H C A
Preorder F A E K C D H G B Inorder DBFE AGCLJHK
Inorder E A C K F H D B G
Create a tree
Example1:
Preorder A B D H E C F G
Inorder D H B E A F C G
Create a tree
Example2:
Preorder F A E K C D H G B
Inorder E A C K F H D B G
Create a tree
Example3:
Postorder H D I E B J F K L G C A
Inorder HDBIEAFJ CKGL
Example 4 :
Postorder D F E B G L J K H C A
Inorder DBFE AGCLJHK
Binary Tree Algorithms
Finding Count of Nodes in the
Tree
If the node does not exist, its counting is updated as zero. If the node
exists, then the number of nodes considering that as root node will be the
1 (the count of that node) plus the nodes in the Left subtree and Right
subtree of that node computed recursively.
ALGORITHM CountNodes(Root)
BEGIN:
IF Root == NULL THEN
RETURN 0
ELSE
RETURN 1+CountNodes(Root Left) + CountNodes(Root Right)
END;
Finding the count of Leaf Nodes
• If the node does not exist, the count of the leaf will be 0. If the left and right of a node are NULL,
that will be the leaf node, and its counting is updated as 1. If the node exists and it is not the leaf
node, then the number of leaf nodes considering that as root node will be the sum of leaf nodes on
the Left subtree and Right subtree of that node computed recursively.
Algorithm CountLeafNodes(Root)
BEGIN:
Example2: J R D T G E A M H F Q U B
Example 3: A B C D E F G H
Example 4: Z Y X W V T R
Linked list Representation of
BST:
In linked representation, BST is a collection of nodes where each node has at
least four fields.
Left address Field Address to Parent node Data Field Right address Field
•
• Left address Field contains the address of the left child of the node.
• Address to the Parent node contains the address of the parent node.
• Data Field contains the actual value to be stored in the node.
• The right address Field contains the address of the right child of the node.
Searching: Binary Search Tree
ALGORITHM SEARCH_BST_I(ROOT, ITEM)
BEGIN:
WHILE ROOT! = NULL DO
IF ITEM ==ROOTDATA THEN
Return ROOT
ELSE IF ITEM< ROOTDATA
ROOT = ROOTLEFT
ELSE
ROOT = ROOTRIGHT
RETURN NULL
END;
Searching: Binary Search Tree
ALGORITHM SEARCH_BST_I(ROOT, ITEM)
BEGIN: Complexity of Operation
Time Complexity: In general, the time complexity of Search
WHILE ROOT! = NULL DO
operation in BST is O(h), where h is the height of the BST.
IF ITEM ==ROOTDATA THEN In worst-case, iF BST is either Right Skewed BST or Left
Return ROOT Skewed BST, then the time complexity of Search operation in
BST is O(N) complexity where N is the number of nodes in
ELSE IF ITEM< ROOTDATA BST.
ROOT = ROOTLEFT
In the Best or Average case, BST is either complete BST or
ELSE Almost complete BST, so the height of BST will be either log2N
ROOT = ROOTRIGHT or almost log2N. So time complexity is order of log2N.
Space Complexity = O(1)
RETURN NULL
END;
Deletion in BST
Case 2: If the node to be deleted has one child, then simply link the current node’s child to its
parent.
- - clockwise rotation
Logic :
left (a) = right (b)
right (b) = a
-1
50
0 0 Insert
30
50 90
10
0 0
20 35
Logic :
left (a) = right (b)
right (b) = a
a -2
-1
50 50
0 0 Insert -1 b 0
30
50 90 30
50 90
10
0 0 -1 0
20 35 20 35
0
10
RR Rotation
• When BST becomes unbalanced, a node is inserted into the right
subtree of the right subtree of A, then we perform RR rotation,
RR rotation is an anticlockwise rotation, which is applied on edge
below a node having balance factor 2.
a Logic:
right (a) = left (b)
Left (b) = a
b
Logic:
right (a) = left (b)
Left (b) = a
1
50
0 0 Insert
30
50 55
85
0 0
52 70
Logic:
right (a) = left (b)
Left (b) = a
a
1 2
50 50
0 b1
0 0 Insert
30
50 55 30
50 55
85
0 0 1
0 52 70
52 70
0
85
Logic:
right (a) = left (b)
Left (b) = a
a 0
1 2 55
50 50
0 b1 RR 0 1
0 0 Insert 50
30
50 30
50 55 70
55 Rotation
85
0 0
0 0 0
0 1 30 52 85
52 70 52 70
0
85
LR Rotation
-1
30
0 0
20 50 Insert
29
0 0 0
50
12 25 40 60
0 0 0
11 24 27
-1 -2
a
30 30
0 0 +1 0
20 50 Insert b 20 50
29 c
0 0 0 +1 0
50
12 25 40 60 50
12 25 40 60
0 0 0 0 0 1
11 24 27 11 24 27
0
29
-2 -3
a a
30 30
+1 0 -1 0
b 20 50 b 25 50
RR
c Rotation -1
+1 0 +1 0
50
12 25 40 60 50
20 27 40 60
0 0 1 0 0 1
11 24 27 12 24 29
0 0
29 11
-3 1
a
30 25
-1 0 -1 1
b 25 50 20 30
LL
-1 +1 0 Rotation -1 0
0 1
50
20 27 40 60 50
12 24 27 50
0 0 1 0 0 0 0
12 24 11 29 40 60
11
RL Rotation
1
50
0 1
40 60 Insert
65
0 0 0
50
10 49 56 80
0 0
70 85
1 +2
50 50
a
0 1 0 +2
40 60 Insert 40 60
65 b -1
0 0 0 0 0
50
10 49 56 80 10
50 49 56 80
0 0 -1 c 0
70 85 70 85
0
65
+2 +2
50 50
a a
0 +2 0 +2
40 60 40 60
LL
b -1 Rotation b +1
0 0 0 0
50
10 49 56 80 10
50 49 56 70
-1 c 0 0 0
70 85 65 80
0 0
65 85
+2 1
50 50
a
0 0 0
40 60 +2 40 70
RR
b +1 0
0 0 Rotation 0 0 1
50
10 49 56 70 10
50 49 60 80
0 0 0 0 0
65 80 56 65 85
0
85
Construct AVL TREE
1,2,3,4,8,7,6,5,11,10,12
2 a
1
b1
2
0
3
Construct AVL TREE
1,2,3,4,8,7,6,5,11,10,12
Insert 1
Tree is balanced
Insert 2
- As 2 > 1, so insert 2 in 1’s right sub tree.
Insert 3
- As 3 > 1, so insert 3 in 1’s right sub tree.
- As 3 > 2, so insert 3 in 2’s right sub tree.
Tree is balanced after RR Rotation
Insert 4
- As 4 > 2, so insert 4 in 2’s right sub tree.
- As 4 > 3, so insert 4 in 3’s right sub tree.
Tree is Balanced
Insert 8
- As 8 > 2, so insert 8 in 2’s right sub tree.
- As 8 > 3, so insert 8 in 3’s right sub tree.
- As 8 > 4, so insert 8 in 4’s right sub tree
Tree is Balanced after RR Rotation
Insert 7
- As 7 > 2, so insert 7 in 2’s right sub tree.
- As 7 > 4, so insert 7 in 4’s right sub tree.
- As 7 < 8, so insert 7 in 8’s left sub tree
Tree is Balanced after RR Rotation
Insert 6
- As 6 > 4, so insert 6 in 4’s right sub tree.
- As 6 < 8, so insert 6 in 8’s left sub tree.
- As 6 < 7, so insert 6 in 7’s left sub tree
Tree is Balanced after LL Rotation
Insert 5
- As 5 > 4, so insert 5 in 4’s right sub tree.
- As 5 < 7, so insert 5 in 7’s left sub tree.
- As 5 < 6, so insert 5 in 6’s left sub tree
Tree is Balanced
Insert 11
- As 11 > 4, so insert 11 in 4’s right sub tree.
- As 11> 7, so insert 11 in 7’s right sub tree.
- As 11> 8, so insert 11 in 8’s right sub tree.
Tree is Balanced
Insert 10
- As 10 > 4, so insert 10 in 4’s right sub tree.
- As 10 > 7, so insert 10 in 7’s right sub tree.
- As 10 > 8, so insert 10 in 8’s right sub tree
- As 10 < 11, so insert 10 in 11’s left sub tree
Tree is Balanced after RL Rotation
Construct AVL tree
March, May, Nov, Aug, April, Jan, Dec, July, Feb, Jun
May
March Nov
43 95
26 45 80 105
43 95
26 45 105