JMCanning78 / DemoRepo

0 stars 0 forks source link

Implement Insert and Delete operations in OrderedArray #20

Open JMCanning78 opened 4 years ago

JMCanning78 commented 4 years ago

Implement the Insert and Delete buttons within the the OrderedArray visualization. These operations use the binary search to locate a cell. Insert should either be disabled or report an error if the array is full. Once it finds the position for the key to insert or delete, animate the loop that assigns values to their neighboring cell. The code for the insert and delete routines is:

   def insert(self, item):    # Insert item into the correct position
      j = self.find(item)     # Find where item should go

      for k in range(self.__nItems, j, -1): # Move bigger items right
         self.__a[k] = self.__a[k-1]

      self.__a[j] = item      # Insert the item
      self.__nItems += 1      # Increment the number of items

   def delete(self, item):             # Delete any occurrence
      j = self.find(item)              # Try to find the item
      if j < self.__nItems and self.__a[j] == item:  # If found,
         self.__nItems -= 1            # One fewer at end
         for k in range(j, self.__nItems): # Move bigger items left
            self.__a[k] = self.__a[k+1]
         return True                   # Return success flag

      return False            # Made it here; item not found

The other operations on the array are in separate issues.

JMCanning78 commented 4 years ago

This issue depends on issue: #19.