-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleTree.java
More file actions
62 lines (48 loc) · 1.18 KB
/
SimpleTree.java
File metadata and controls
62 lines (48 loc) · 1.18 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* TCSS 342 - Compressed Literature 2
* SimpleTree class
*/
/**
* A tree that is used to implement a Huffman tree.
*
* @author Rylie Nelson
* @version Autumn 2013
*/
public class SimpleTree implements Comparable {
/**
* The Node that is the head of this tree.
*/
public Node HeadOfTree;
/**
* The weight of the tree, which is the combined frequency of each node.
*/
public int WeightOfTree;
/**
* Constructs a new SimpleTree.
*
* @param newNode The head of the new tree.
*/
public SimpleTree(Node newNode) {
HeadOfTree = newNode;
WeightOfTree = newNode.frequency;
}
/**
* Combines two trees using Huffman tree invariants.
*
* @param other_tree The tree to be combined with this one.
*/
public void combineTree(SimpleTree other_tree) {
Node newHead = new Node(null, 0);
newHead.left_child = HeadOfTree;
newHead.right_child = other_tree.HeadOfTree;
WeightOfTree = WeightOfTree + other_tree.WeightOfTree;
HeadOfTree = newHead;
}
public String toString() {
return HeadOfTree + " " + WeightOfTree + "\n";
}
@Override
public int compareTo(Object o) {
SimpleTree st = (SimpleTree) o;
return WeightOfTree - st.WeightOfTree;
}
}