DSA Assignment 1

Problem I

Algorithm GETminimum(A)

SORT(A)

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

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

else

minmin \gets \infty

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

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

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

end if

end for

end if

return minmin

Problem II

  1. BST
  2. BST-1
  3. |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

  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 because the tree is balanced.

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

  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 , 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 () 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.

  1. insert-1
  2. delete-1