Skip to content
PostAlgorithm / DSA

DSA Assignment 1

2023-09-16
Back to Blog

DSA-AS-1

Problem I

Algorithm 2 GETminimum(A)

1:SORT(A)

2:if A[0]A[A.length1]<0A[0] - A[A.\text{length} - 1] < 0 then

3:

4:return A[0]A[A.length1]A[0] - A[A.\text{length} - 1]

5:else

6:minmin \gets \infty

7:for i1i \gets 1 to A.length1A.\text{length} - 1 do

8:if A[i]A[i1]<minA[i] - A[i - 1] < min then

9:minA[i]A[i1]min \gets A[i] - A[i - 1]

10:end if

11:end for

12:end if

13:

14:return minmin

Problem II

  1. BST
  2. BST-1
  3. |14|25|40|52|58|85|

Problem III

3.1

a. Initial State

txt
              52|2              
        ┌───────┴───────┐       
      15|1            85|0      
    ┌───┴───┐                   
   8|0    31|0

Insert 82:

txt
              52|2              
        ┌───────┴───────┐       
      15|1            85|1      
    ┌───┴───┐       ┌───┘       
   8|0    31|0    82|0

Insert 6:

txt
                              52|3                              
                ┌───────────────┴───────────────┐               
              15|2                            85|1              
        ┌───────┴───────┐               ┌───────┘               
       8|1            31|0            82|0                      
    ┌───┘                                                       
   6|0

Insert 65:

txt
                              52|3                              
                ┌───────────────┴───────────────┐               
              15|2                            82|1              
        ┌───────┴───────┐               ┌───────┴───────┐       
       8|1            31|0            65|0            85|0      
    ┌───┘                                                       
   6|0

b.

1

txt
                              52|3                              
                ┌───────────────┴───────────────┐               
               8|1                            82|2              
        ┌───────┴───────┐               ┌───────┴───────┐       
       6|0            15|0            65|1            85|1      
                                        └───┐           └───┐   
                                          72|0            95|0

2

txt
                              65|3                              
                ┌───────────────┴───────────────┐               
              15|2                            82|2              
        ┌───────┴───────┐               ┌───────┴───────┐       
       8|1            31|0            72|0            85|1      
    ┌───┘                                               └───┐   
   6|0                                                    95|0

3

txt
                              52|3                              
                ┌───────────────┴───────────────┐               
              15|2                            85|2              
        ┌───────┴───────┐               ┌───────┴───────┐       
       8|1            31|0            65|1            95|0      
    ┌───┘                               └───┐                   
   6|0                                    72|0

3.2

  1. 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 O(logn) because the tree is balanced.

  2. Building the Tree: If we insert n distinct integers into an AVL tree, each insert operation takes O(logn) time. Since there are n elements, the total time to build the tree is O(nlogn).

  3. 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 O(n), as each node is visited exactly once.

  4. Total Time Complexity: The total time complexity of sorting an array using an AVL tree is the time to build the tree (O(nlogn)) plus the time for the in-order traversal (O(n)). Hence, the overall time complexity is O(nlogn).

Therefore, we have shown that using an [[|AVL]] tree to sort an array of distinct integers has a time complexity of O(nlogn).

Problem IV

a.

(L=Size of one blockSize of each employee record=2048128=16)

(M=Size of one blockSize of a primary key + Size of a pointer+1)

(M=20488+4+1=204812+1=170+1=171)

  • L=16: Each leaf node can hold up to 16 records.
  • M=171: Each internal node can have up to 171 children (170 keys + 1 extra pointer).

b.

  1. insert-1
  2. delete-1