forked from erichVK5/translate2geda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.java
122 lines (110 loc) · 3.56 KB
/
Node.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// BXLDecoder.java - a utility for converting Huffman encoded files
// into text, ported to Java from the vala code by Geert Jordaens
//
// Node.java - a Node object used for the Huffman decoding tree
//
// BXLDecoder.java v1.0
// Node.java v1.0
// Copyright (C) 2016 Erich S. Heinzle, [email protected]
// see LICENSE-gpl-v2.txt for software license
// see README.txt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// BXLDecoder Copyright (C) 2016 Erich S. Heinzle [email protected]
public class Node {
public int level = 0; // { get; private set; default = 0; }
public Node parent = null;// { get; set; default = null; }
public Node left = null; //{ get; set; default = null; }
public Node right = null; //{ get; set; default = null; }
public int symbol = -1; //{ get; private set; default = -1; }
public int weight = 0; //{ get; set; default = 0; }
public Node () {
//this.level = 0;
}
public Node (Node parent, int symbol) {
if (parent != null) {
this.parent = parent;
this.level = parent.level+1;
} else {
this.level = 0;
}
if (level > 7) {
this.symbol = symbol;
//System.out.println("Symbol allocated is: " + symbol);
}
}
public String toString() {
// System.out.println("toString called..." +
// symbol + " "
// + Character.toString((char)(symbol & 0xff)));
return Character.toString((char)(symbol & 0xff));
}
public Node add_child(int symbol){
Node ret = null;
if (level < 7) {
if (right != null) {
ret = right.add_child(symbol);
if (ret != null) return ret;
}
if (left != null) {
ret = left.add_child(symbol);
if (ret != null) return ret;
}
if (right == null) { // first fill right branch
right = new Node (this, -1);
return right;
}
if (left == null) {
left = new Node (this, -1);
if (left !=null) return left;
}
return null;
} else {
if (right == null) {
right = new Node (this, symbol);
return right;
} else if (left == null) {
left = new Node (this, symbol);
return left;
} else {
return null; // Leaves are filled
}
}
}
public boolean is_leaf() {
//System.out.println("Am I a leaf? Checking level:" + level);
return (level > 7);
}
public Node sibling(Node node){
if (node != right) {
return right;
} else {
return left;
}
}
public void incrementWeight() {
this.weight++;
}
public boolean need_swapping() {
if (parent != null &&
parent.parent != null && // root node
this.weight > parent.weight) {
//System.out.println("Hmm, need to swap during tree update.");
return true;
}
return false;
}
}