יום שני, 28 בנובמבר 2011

Queues & Heaps


QUEUES & HEAPS
Queues

7_1
Yes it works in the FIFO behavior.
7_2
Using implementation of LinkedLists is ideal because they are very flexible at adding new objects at the front and the back, in retrospect I am not sure why you couldn't also use ArrayLists. I am not sure how to calculate the BigO here.
7_3
The add() method adds to the beginning whilst the addLast() adds to the end. I believe addLast() was used because in the case of Queues, adding means adding to the end.
7_4
I had to look at the API.
import java.util.ArrayList;
public class ArrayListQueue implements Queue 
private ArrayList list;

public void ListQueue() 
    list = new ArrayList(); 
}//constructor

public void enqueue(Object x) 
    list.add(x); 
}//enqueue

public Object dequeue()
    return list.remove(0); 
}//dequeue

public Object peekFront()
    return list.get(0); 
}//peekFront

public boolean isEmpty() 
    return list.size() == 0; 
}//isEmpty

}//class ListQueue

7_5
Ease - Almost the same ease, almost all the methods were still one liners.
Efficiency - Not a change at all, ArrayLists still get the job done and the ease of writing its code is not much harder.

Heaps

Started.

Binary Trees 2

6_6

יום ראשון, 20 בנובמבר 2011

Binary Trees 1


6_1
There are four leaves, 3 nodes have two children, and it would take 2 iterations to find out that 12 is not in it, i believe

6_2
Yes it worked.

6_3
I don't know why it took me so long to figure out where to put the code
public void add(Object newValue)
{
    TreeNode newNode = new TreeNode(newValue,null,null);

    if(root==null)//bst is empty, make root reference node referenced by newNode
    {
        root=newNode;
    }//if

    else//search for place to insert node referenced by newNode
    {
        TreeNode T=root;//T is a traversing TreeNode reference (pointer)
        TreeNode follow=null;//follows one node behind T

        while(T!=null)
        {
            follow=T;
            if (((Comparable)newNode.getValue()).compareTo(T.getValue())==0){return;}
            if (((Comparable)newNode.getValue()).compareTo(T.getValue())>0)
            {//the value in node referenced by newNode >= 
              //value in node referenced by T (go right)
                T=T.getRight();
            }//if
            else
            {//go left
                T=T.getLeft();
            }//else
        }//while

        //follow will now reference (point to) the node where insertion to be made 
        if (((Comparable)newNode.getValue()).compareTo(follow.getValue())>=0)
        {//the value in node referenced by newNode >=
          //value in node referenced by follow (link right)
            follow.setRight(newNode);
        }//if
        else
        {//link left
            follow.setLeft(newNode);
        }//else

    }//else
}//add

}//class BST

6_4
I am not sure why

6_5
public void find(Object target){
if(root==null){return;}
TreeNode T=root;//T is a traversing TreeNode reference (pointer)
    TreeNode follow=null;//follows one node behind T
    while(T!=null); {
    follow = T;
    if (((Comparable)newNode.getValue()).compareTo(T.getValue())==0) {return T;}
    if (((Comparable)newNode.getValue()).compareTo(T.getValue())>0) {T=T.getRight();}
    else {T=T.getLeft();}
    }
return null;
}