Data Structures
Tuesday, May 8, 2012
Thursday, May 3, 2012
class Queue<T>
{
SLL<T> list = new SLL<T>();
public int size() { return list.size(); }
public void enqueue(T t) { list.addAtTail(t); }
public T dequeue(void) { return list.removeFront(); }
}
class Stack<T>
{
SLL<T> list = new SLL<T>();
public int size() { return list.size(); }
public void push(T t) { list.addAtFront(t); }
public T dequeue(void) { return list.removeFront(); }
}
class Stack<T>
{
ArrayList<T> list = new ArrayList<T>();
public int size() { return list.size(); }
public void push(T t) { list.add(list.size(), t); }
public T pop(void) { return list.remove(list.size()); }
}
class Stack<T> extends SLL<T>
{
public int size() { return super.size(); }
public void push(T t) { addAtFront(t); }
public T dequeue(void) { return removeFront(); }
}
// NOT adapter pattern
class Stack<T>
{
SLNode<T> head, tail;
void push(T t) {
SLNode n = new Node<T>(t,head);
head = n;
size++;
}
T pop() {
SLNode n = head;
head = head.next;
return n.value;
size--;
}
}
class PQ<T>
{
Comparator<T> comp;
SLL<T> list = new SLL<T>();
PQ(Comparator<T> c)
{
comp = c;
}
void enqueue(T t) { list.insertAtEnd(t); }
T dequeue()
{
T theMin = list.getFirstItem();
for (T element : list)
{
if(comp.compare(theMin, element) < 0)
{
theMin = element;
}
}
// change to explicit iterator
for (T element : list)
{
if(comp.compare(theMin, element) == 0)
{
it.remove();
return theMin;
}
}
}
}
Tuesday, May 1, 2012
Tuesday, April 3, 2012
http://www2.research.att.com/~bs/whitespace98.pdf
HW:
step 1)
find the freq table
2) charfreq class, comparable
3) revisit your generic binarytree class, where binarytrees should also be comparable. based on the value stored at the root.
4) Take your freq table, create a forest of trees, where each tree contains a charFreq.
Revisit your priorityqueuue. make sure it is generic.
5) Algorithm for creating a Huffman Tree:
a) Take your freq table, create a forest of trees, where each tree contains a charFreq.
b) As you create each tree in your forest, insert it into the PQ.
c) While PQ.size() != 1
Tree T1 = PQ.dequeue();
Tree T2 = PQ.dequeue();
Tree T3 = join(T1, T2); // where root node's charfreq's freq is the sum of the freq of roots of T1 and T2
PQ.enqueue(T3);
outside of while loop:
d) Tree Huffman = PQ.dequeue();
6) Use the Huffman tree to encode a text file. (You may encode it as a string first.)
Do a visit of each node. If go to left, append a '0', else append a '1'.
Write to a table what string corresponds to a given letter.
Then, loop thru your file, creating an appropriate text string.
Using bitshifting, << >> | &, generate byte array. Write to a file.
7) Open a file, use your existing Huffman Tree, convert bit pattern (or string) to original text.
Tuesday, March 20, 2012
Solutions to sample midterm
12)
class Node
{
int val;
Node next;
}
13)
a) class SLL
{
Node head, tail;
int size;
// constructor present but not given here
// choose not to make header or trailer
}
b) void insertBefore(Node n, int x)
{
Node m = new Node(x);
if (n==head)
{
m.next = n;
head = m;
}
else
{
// traverse from head
for (Node cur = head; cur.next!=n; cur=cur.next)
; // do nothing
cur.next=m;
m.next=n;
}
size++;
}
c) void insertAfter(Node n, int x)
{
Node m=new Node(x);
m.next = n.next;
n.next = m;
if (n==tail)
m = tail;
size++;
}
d) void remove(int value)
{
if (size==0)
return;
Node prev = null;
for(Node cur = head; cur != null && cur.val != value; prev=cur, cur=cur.next)
; // do nothing
if (prev==null) // found item at head
{
head=head.next;
size--;
}
else if (cur != null && cur.val == value) // item found
{
prev.next = cur.next; // leapfrog
}
if (prev.next==null)
tail=prev;
}
d) void removeSpan(Node start, Node finish)
{
start.next = finish;
}
e) void swap(Node n, Node m)
{
int temp = n.val;
n.val = m.val;
m.val = temp;
}
Tuesday, March 13, 2012
Thursday, March 8, 2012
Subscribe to:
Posts (Atom)