+/EE360C

algorithms

git clone https://git.julia.blue/+/EE360C

Joseph RyanFinish lab 2381a8ab

master
1.9 KiB71 linesraw
1import java.util.HashMap;
2import java.util.Map;
3
4public class TreeNode implements Comparable<TreeNode> {
5    private TreeNode leftChild = null;
6    private TreeNode rightChild = null;
7    private TreeNode parent = null;
8    private String value = null;
9    private int frequency = 0;
10
11    TreeNode() {
12        this.leftChild = null;
13        this.rightChild = null;
14        this.parent = null;
15        this.value = null;
16        this.frequency = 0;
17    }
18
19    TreeNode(TreeNode a, TreeNode b) {
20        frequency = a.getFrequency() + b.getFrequency();
21        leftChild = a;
22        rightChild = b;
23        a.setParent(this);
24        b.setParent(this);
25    }
26
27    TreeNode(String v, int f) {
28        value = v;
29        frequency = f;
30    }
31
32    private void addToMap(String prefix, Map<String, String> h) {
33        if (value != null) {
34            h.put(value, prefix);
35        } else {
36            leftChild.addToMap(prefix + '0', h);
37            rightChild.addToMap(prefix + '1', h);
38        }
39    }
40
41    public Map<String, String> getMap() {
42        HashMap<String, String> h = new HashMap<>();
43        addToMap("", h);
44        return h;
45    }
46
47    public void setLeftChild(TreeNode child) { this.leftChild = child; }
48
49    public void setRightChild(TreeNode child) { this.rightChild = child; }
50
51    public void setParent(TreeNode parent) { this.parent = parent; }
52
53    public void setValue(String value) { this.value = value; }
54
55    public void setFrequency(int frequency) { this.frequency = frequency; }
56
57    public TreeNode getLeftChild() { return this.leftChild; }
58
59    public TreeNode getRightChild() { return this.rightChild; }
60
61    public TreeNode getParent() { return this.parent; }
62
63    public String getValue() { return this.value; }
64
65    public int getFrequency() { return this.frequency; }
66
67    @Override
68    public int compareTo(TreeNode o) {
69        return new Integer(frequency).compareTo(o.getFrequency());
70    }
71}