BADSIS Assignment 3
BADSIS Assignment 3
Pre-order traversal:-To traverse a non-empty binary tree by pre-order method, the following
operations are performed recursively until all nodes are visited:
i. Visit the root node.
ii. Traverse the left sub-tree fully.
iii. Traverse the right sub-tree.
The pre-order traversal of above tree is A,Q,W,Z,C,H,G,D
In-order Traversal:- To traverse a non-empty binary tree by in-order method, the following
operations are performed recursively until all nodes are visited:
i. Traverse the left sub-tree.
ii. Now, visit the root node.
iii. Then, finally traverse the right sub-tree.
The in-order traversal of the tree is Q, Z, W, C, A, H, D, G
Post-Order traversal:- To traverse a non-empty binary tree by post-order method, the
following operations are performed recursively until all nodes are visited:
i. Traverse the left sub-tree.
ii. Now, move to the right sub tree
iii. Then, finally visit the root node.
The post-order traversal of the tree is Z, C, W, Q, D, G, H, A
2 3
0 1 2
8 5
6 7
3 4
Initial Input
Key Array 0 1 2 3 4
∞ ∞ ∞ ∞ ∞
MST Array F F F F F
Parent Array -1 -1 -1 -1 -1
Final Input
Key Array 0 1 2 3 4
∞ 2 3 6 5
MST Array T T T T T
Parent Array -1 0 1 0 1
In Kruskal’s Algorithm, we use the concepts of “ Minimum spanning tree” and “ Disjoint
Set”. Since it is a greedy algorithm, we try to span the entire tree without making a cycle (as
it would defeat the purpose of the minimum spanning tree) and according to the disjoint set,
we ensure that the same parent which has already been used is not used (again to prevent
from making a cycle). We again use 3 Arrays for this, i.e., “Weight” ; “Source” and
“Destination” (in my example, I will implement this format) and thorough this we ensure that
the whole tree is spanned.
7 6 5
1 2
After sorting:
Weight Source Destination
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
Final Result
7
1 2 3
4 9
2
4
0 8 4
8
7 6 5
1 2
Ans.) K-means uses a pre-specified number of clusters .This method assigns records to each
cluster to find the mutually exclusive cluster of spherical shape based on distance. It needs
advance knowledge of K, i.e., no. of clusters one wants to divide data into. One can use
median or mean as a cluster centre to represent each cluster. In K Means clustering, since one
start with random choice of clusters, the results produced by running the algorithm many
times may differ. K- means clustering a simply a division of the set of data objects into non-
overlapping subsets (clusters) such that each data object is in exactly one subset).
Example: -
(6, 9)
Hierarchical clustering is used to group the unlabelled datasets into a cluster and also
known as hierarchical cluster analysis or HCA. In this algorithm, we develop the hierarchy of
clusters in the form of a tree, and this tree-shaped structure is known as the dendrogram. The
hierarchical clustering technique has two approaches:
In the k-means clustering that there are some challenges with this algorithm, which are a
predetermined number of clusters, and it always tries to create the clusters of the same size.
To solve these two challenges, we can opt for the hierarchical clustering algorithm because,
in this algorithm, we don't need to have knowledge about the predefined number of clusters.
Example: - It is used to sort country data, below is an example of US States arrest data