Refactoring Assignment
Refactoring Assignment
1. (1 point) Read http://en.wikipedia.org/wiki/Code refactoring first. Use your own word to briefly
explain what is code refactoring?
2. (4 points) The following code has passed all test cases. First read the code and understand how the
code works. Then refactoring the code little by little. While you are refactoring the code, write down
the changes that you made. For example:
1. The comment should not use vague word. It should not use ”bubble down to its appropriate
position”. It should say ”bubble down until both its children are greater”
2. ...
3. ...
The bubbleDown method in Heap.java of Assignment 3.
/∗ ∗
∗ The e n t r y s h o u l d b e b u b b l e d down t o i t s a p p r o p r i a t e p o s i t i o n
∗ @param p
∗/
@TimeComplexity ( ”O( l g n ) ” )
p r i v a t e void bubbleDown ( P o s i t i o n <Entry<K, V>> p )
{
/ / W h i l e t h e n o d e s t i l l n e e d s t o b e b u b b l e d down , k e e p l o o p i n g t h e f o l l o w i n g .
/ / TCJ : B e c a u s e t h e maximum a m o u n t o f t i m e s t h i s l o o p c a n r u n i s d e t e r m i n e d b y t h e h e i g h t
// o f t h e t r e e , w h i c h i s e q u a l t o l g n , t h a t i s t h e t i m e c o m p l e x i t y o f t h e f u n c t i o n .
w h i l e ( t r e e . i s I n t e r n a l ( p ) &&
( t r e e . h a s L e f t ( p ) && ( t r e e . l e f t ( p ) . e l e m e n t ( ) . g e t K e y ( ) . compareTo ( p . e l e m e n t ( ) . g e t K e y ( ) ) > 0 ) | |
( t r e e . h a s R i g h t ( p ) && t r e e . r i g h t ( p ) . e l e m e n t ( ) . g e t K e y ( ) . compareTo ( p . e l e m e n t ( ) . g e t K e y ( ) ) > 0 ) ) )
{
boolean l e s s T h a n L e f t = f a l s e ;
boolean l e s s T h a n R i g h t = f a l s e ;
i f ( l e s s T h a n L e f t && l e s s T h a n R i g h t )
{
i f ( t r e e . l e f t ( p ) . e l e m e n t ( ) . g e t K e y ( ) . compareTo ( t r e e . r i g h t ( p ) . e l e m e n t ( ) . g e t K e y ( ) ) > 0 )
{
t r e e . swapElements ( p , t r e e . l e f t ( p ) ) ;
p = tree . l e f t (p ) ;
}
else
{
t r e e . swapElements ( p , t r e e . r i g h t ( p ) ) ;
p = tree . right (p ) ;
}
}
e l s e i f ( l e s s T h a n L e f t && ! l e s s T h a n R i g h t )
{
t r e e . swapElements ( p , t r e e . l e f t ( p ) ) ;
p = tree . l e f t (p ) ;
}
e l s e i f ( ! l e s s T h a n L e f t && l e s s T h a n R i g h t )
{
t r e e . swapElements ( p , t r e e . r i g h t ( p ) ) ;
p = tree . right (p ) ;
}
}
}
1
(b) The new source code
/ / TCJ : B e c a u s e we d o n o t k n o w w h e r e we a r e s u p p o s e d t o a d d t h e e n t r y , we
// need t o i t e r a t e t h r o u g h t h e e n t i r e s e q u e n c e t o f i n d i t , c a u s i n g i t t o run
// i n O( n ) t i m e .
for ( int index = 0; index < d a t a . s i z e ( ) ; ++i n d e x )
{
// D e t e r m i n e s i f t h e e n t r y h a s f o u n d i t s p r o p e r p l a c e .
i f ( d a t a . a t I n d e x ( i n d e x ) . e l e m e n t ( ) . g e t K e y ( ) . compareTo ( r e t . g e t K e y ( ) ) > 0 )
{
// i f i t has , add i t i n and b r e a k t h e l o o p .
d a t a . add ( i n d e x , r e t ) ;
index = data . s i z e ( ) ;
added = t r u e ;
}
}
// I f t h e a d d e d v a r i a b l e i s s t i l l f a l s e , t h i s means
// t h e d a t a must go i n t h e n e x t empty i n d e x .
i f ( ! added )
{
/ / Add t h e d a t a t o t h e n e x t e m p t y i n d e x .
d a t a . addLast ( r e t ) ;
}
4. (1 point) Print and sign all team member’s names below to acknowledge that you have carefully re-
viewed the code line by line with your team and refactored the code to your best. Each team needs to
have two or three team members. Each student may work in multiple teams.