CSC403: Homework

Contents [0/2]

Homework [1/2]
HW Answers [2/2]

(Click here for one slide per page)


Homework [1/2]

HW Answers [2/2]

3.1.10

  k  v  #  list representation (key|value)

  E  0  0  E|0
  A  1  1  A|1  -> E|0
  S  2  2  S|2  -> A|1  -> E|0
  Y  3  3  Y|3  -> S|2  -> A|1 -> E|0
  Q  4  4  Q|4  -> Y|3  -> S|2 -> A|1 -> E|0
  U  5  5  U|5  -> Q|4  -> Y|3 -> S|2 -> A|1 -> E|0
  E  6  6  U|5  -> Q|4  -> Y|3 -> S|2 -> A|1 -> E|6
  S  7  4  U|5  -> Q|4  -> Y|3 -> S|7 -> A|1 -> E|6
  T  8  6  T|8  -> U|5  -> Q|4 -> Y|3 -> S|7 -> A|1 -> E|6
  I  9  7  I|9  -> T|8  -> U|5 -> Q|4 -> Y|3 -> S|7 -> A|1 -> E|6
  O  10 8  O|10 -> I|9  -> T|8 -> U|5 -> Q|4 -> Y|3 -> S|7 -> A|1 -> E|6
  N  11 9  N|11 -> O|10 -> I|9 -> T|8 -> U|5 -> Q|4 -> Y|3 -> S|7 -> A|1 -> E|6
  ------------------------------------------------------------------------------  
  Total = 55 compares

  If N is the number of put operations, then the maximum is N*N/2
  (= 12*12/2 = 72 in this case).

  The worst case occurs in the case that all keys are unique.

3.1.11

  Binary search does the comparison to key[mid], where
    mid = lo + (hi - lo) / 2;
  if hi-lo is odd, pick the middle.
  if hi-lo is even, pick the lower of the middle two

  In the table below, I show the comparisons in order in the key
  array.  So, for example, when inserting S, the algorithm first
  compares to A, then E.

  k  v  #  KEY ARRAY                         VALUE ARRAY

  E  0  0  E1                                0                                      
  A  1  1  A1 E2                             1  0                                  
  S  2  2  A  E1 S2                          1  0  2                              
  Y  3  2  A  E1 S2 Y3                       1  0  2   3                          
  Q  4  3  A  E  Q1 S2 Y3                    1  0  4   2   3                      
  U  5  3  A2 E3 Q1 S  U  Y                  1  0  4   2   5  3                  
  E  6  3  A  E  Q1 S3 U2 Y                  1  6  4   2   5  3                  
  S  7  3  A  E  Q1 S3 U2 Y                  1  0  4   7   5  3                  
  T  8  3  A  E2 Q3 S1 T  U  Y               1  0  4   7   8  5  3              
  I  9  3  A  E2 I3 Q1 S  T  U  Y            1  0  9   4   7  8  5  3          
  O  10 3  A  E2 I3 O4 Q1 S  T  U  Y         1  0  9  10   4  7  8  5  3     
  N  11 4  A  E  I  N  O  Q  S  T  U  Y      1  0  9  11  10  4  7  8  5  3
  --------------------------------------------------------------------------
  Total = 30 compares
  
  Binary search allows N keys to be put() about 2N compares (= 24 in
  this case).  The number is low, since about half the time, we have
  one more comparison, making it really closer to 2.5N (= 30 in this case).

3.2.2

  In general: repeatedly insert either the max or min remaining key.

  Here are some examples:

  A,C,E,H,R,S,X // sort
  A,C,E,H,R,X,S
  A,C,E,H,X,S,R
  A,C,E,X,S,R,H
  A,C,X,S,R,H,E
  A,X,S,R,H,E,C
  X,S,R,H,E,C,A // reverse sort
  A,X,C,S,E,R,H // alternate
  X,A,S,C,R,E,H
  
  Are the following correct?

  A,S,C,X,E,R,H
  X,R,S,H,E,C,A
  X,E,R,C,S,H,A

3.2.3

  In general:
    keep a set of sets, initially just one subset containing all keys
    choose a subset and remove it from the set of sets
    in the BST, insert the middle key from the subset
    to the set of sets, add the two subsets containing the smaller and larger keys

  Another way to view that:
    Draw the perfectly balanced tree that you want.
    Always insert parent before child.

  In this case the tree we want is:

                H
              /   \
            C       S
           / \     / \
          A   E   R   X

  Here are some examples:

  H,C,A,E,S,R,X // pre-order
  H,C,S,A,E,R,X // level-order
  H,C,E,A,S,X,R
  H,S,X,R,C,E,A
  H,S,R,X,C,A,E
  H,S,R,X,C,E,A

3.2.4

  d