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
Sample midterm
Data Structures Midterm Name: __________________________
1) Draw a picture of a Doubly Linked List, with nodes holding values of 4, 5, and 7. You should not only have a head and tail, but also a header and a trailer.
2) Draw and describe the steps involved in removing the middle element (5).
3) Deleting from the end of a singly linked list is O(n). Demonstrate the steps in deleting from the end of such a list and explain why it is O(n).
4) Deleting an element from the middle of a singly linked list (the node is passed in) is O(n). Explain, in a sentence or two, why.
5) Deleting from the end of a doubly Linked list, where we have a reference to the tail, is on the order of what? Explain why.
6) Draw a circular array with a couple of elements and a front and rear.
7) How would you implement (in words) removal from the end of the circular array? What data members would you modify? How would you handle boundary conditions?
8) How would you implement removal from the middle of a vector based on a normal array, rather than a circular array? Explain in words.
9) Give one reason it might be better to use an array rather than a singly-linked list to implement a stack.
10) f(n) = 15n^2 + 10n + 1. This is O of what? Give a proof with c and n_0.
12) Write a Java class for a node for a singly-linked list.
13) Write code for a Singly Linked List class, which makes use of that node. Make a head, a tail, and a size. Include the following methods (others may exist, but you need not write them), and make certain to handle the boundary conditions and to check for error conditions:
insertBefore(Node n, int x);
insertAfter(Node n, int x);
remove(int value);
removeSpan(Node start, Node finish);
swap(Node n, Node m);
1) Draw a picture of a Doubly Linked List, with nodes holding values of 4, 5, and 7. You should not only have a head and tail, but also a header and a trailer.
2) Draw and describe the steps involved in removing the middle element (5).
3) Deleting from the end of a singly linked list is O(n). Demonstrate the steps in deleting from the end of such a list and explain why it is O(n).
4) Deleting an element from the middle of a singly linked list (the node is passed in) is O(n). Explain, in a sentence or two, why.
5) Deleting from the end of a doubly Linked list, where we have a reference to the tail, is on the order of what? Explain why.
6) Draw a circular array with a couple of elements and a front and rear.
7) How would you implement (in words) removal from the end of the circular array? What data members would you modify? How would you handle boundary conditions?
8) How would you implement removal from the middle of a vector based on a normal array, rather than a circular array? Explain in words.
9) Give one reason it might be better to use an array rather than a singly-linked list to implement a stack.
10) f(n) = 15n^2 + 10n + 1. This is O of what? Give a proof with c and n_0.
12) Write a Java class for a node for a singly-linked list.
13) Write code for a Singly Linked List class, which makes use of that node. Make a head, a tail, and a size. Include the following methods (others may exist, but you need not write them), and make certain to handle the boundary conditions and to check for error conditions:
insertBefore(Node n, int x);
insertAfter(Node n, int x);
remove(int value);
removeSpan(Node start, Node finish);
swap(Node n, Node m);
Tuesday, March 6, 2012
Thursday, March 1, 2012
class ArrayStack<T> implements Iterable<T>
{
// ...
// various methods we have
Iterator<T> iterator()
{
// will take snapshot approach MyNiftyArrayStackIterator<T> mnasi= new MyNiftyArrayStackIterator<T>(a.clone(), size);
return mnasi;
}
}
class MyNiftyArrayStackIterator<T> implements Iterator<T>
{
int currElem = 0;
MyNiftyArrayStackIterator(T b[], int size) { save to local vars; }
boolean hasNext() { return currElem < size; }
T next() { return b[currElem++]; }
}
Subscribe to:
Posts (Atom)