DSA-AS-1
Problem I
\begin{algorithm}
\caption{GETminimum(A)}
\begin{algorithmic}
\State \textbf{SORT}(A)
\If{$A[0] - A[A.\text{length} - 1] < 0$}
\State \Return $A[0] - A[A.\text{length} - 1]$
\Else
\State $min \gets \infty$
\For{$i \gets 1$ \textbf{to} $A.\text{length} - 1$}
\If{$A[i] - A[i - 1] < min$}
\State $min \gets A[i] - A[i - 1]$
\EndIf
\EndFor
\EndIf
\State \Return $min$
\end{algorithmic}
\end{algorithm}
Problem II
- |14|25|40|52|58|85|
Problem III
3.1
a. Initial State
52|2
┌───────┴───────┐
15|1 85|0
┌───┴───┐
8|0 31|0
Insert 82:
52|2
┌───────┴───────┐
15|1 85|1
┌───┴───┐ ┌───┘
8|0 31|0 82|0
Insert 6:
52|3
┌───────────────┴───────────────┐
15|2 85|1
┌───────┴───────┐ ┌───────┘
8|1 31|0 82|0
┌───┘
6|0
Insert 65:
52|3
┌───────────────┴───────────────┐
15|2 82|1
┌───────┴───────┐ ┌───────┴───────┐
8|1 31|0 65|0 85|0
┌───┘
6|0
b.
1
52|3
┌───────────────┴───────────────┐
8|1 82|2
┌───────┴───────┐ ┌───────┴───────┐
6|0 15|0 65|1 85|1
└───┐ └───┐
72|0 95|0
2
65|3
┌───────────────┴───────────────┐
15|2 82|2
┌───────┴───────┐ ┌───────┴───────┐
8|1 31|0 72|0 85|1
┌───┘ └───┐
6|0 95|0
3
52|3
┌───────────────┴───────────────┐
15|2 85|2
┌───────┴───────┐ ┌───────┴───────┐
8|1 31|0 65|1 95|0
┌───┘ └───┐
6|0 72|0
3.2
Insertion Time: To insert an element into an [[AVL]] tree, we perform a binary search to find the correct position for the new element and then possibly perform some rotations to maintain the [[AVL]] property. The time complexity of both these operations is
because the tree is balanced. Building the Tree: If we insert
distinct integers into an [[AVL|AVL]] tree, each insert operation takes time. Since there are elements, the total time to build the tree is . In-order Traversal: Once the [[AVL]] tree is built, we can perform an in-order traversal to retrieve the elements in sorted order. An in-order traversal of a binary search tree visits the nodes in ascending order, which is what we want for sorting. The time complexity for in-order traversal is
, as each node is visited exactly once. Total Time Complexity: The total time complexity of sorting an array using an [[AVL|AVL]] tree is the time to build the tree (
) plus the time for the in-order traversal ( ). Hence, the overall time complexity is .
Therefore, we have shown that using an [[|AVL]] tree to sort an array of distinct integers has a time complexity of
Problem IV
a.
: Each leaf node can hold up to 16 records. : Each internal node can have up to 171 children (170 keys + 1 extra pointer).
b.