aboutsummaryrefslogtreecommitdiffstats
path: root/Huffman.cs
blob: 0fb446246e410848718150488061cc2424088850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* filename: Huffman.cs
 * Class: CS 456-001
 * Professor: Dr. John Matta
 * Due Date: Sept. 20th, 2021
 * Name:
 * Partner:
 */
 //TODO: ADD NAMES

public class Huffman {
    private List<Node> C;
    private PriorityQueue pq;
    private Node huffmanTree;
    public Huffman(List<Node> C) {
        //initialize
        this.C = new List<Node>(C);
        pq = new PriorityQueue();
        Node huffmanTree = null;
    }
    private void setHuffmanTree() {

        for(int i = 1; i < C.Count; i++) {

            Node x, y;
            Node z = new Node();

            z.left = x = extractMin(pq);
            z.right = y = extractMin(pq);
            z.freq = x.freq + y.freq;

            insert(pq, z);
        }

        huffmanTree = extractMin(pq);
    }

    public Node getHuffmanTree() {
        if(huffmanTree == null) {
            setHuffmanTree();
        }
        return huffmanTree;
    }
}