-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.java
169 lines (156 loc) · 4.72 KB
/
BinaryTree.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package miniProject;
public class BinaryTree implements java.io.Serializable {
public Node root;
public TrainingImage[] avarageImages;
private static final long serialVersionUID = 1L;
public BinaryTree() {
this.root = new Node();
this.avarageImages = null;
}
// All nodes are visited in ascending order
// Recursion is used to go to one node and
// then go to its child nodes and so forth
public int size() {
return(size(root));
}
public Node findNode(int label) {
// Start at the top of the tree
Node focusNode = root;
// While we haven't found the Node
// keep looking
while (focusNode.label != label) {
// If we should search to the left
if (label < focusNode.label) {
// Shift the focus Node to the left child
focusNode = focusNode.leftChild;
} else {
// Shift the focus Node to the right child
focusNode = focusNode.rightChild;
}
// The node wasn't found
if (focusNode == null)
return null;
}
return focusNode;
}
private int size(Node node) {
if (node == null)
return(0);
else {
return(size(node.leftChild) + 1 + size(node.rightChild));
}
}
//change
@Override
public String toString(){
return toString (root);
}
private String toString(Node root){
String result = "";
if (root == null)
return "";
result += toString(root.leftChild);
result += toString(root.rightChild);
result += root.toString();
return result;
}//end change
}
class Node implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public int label;
public String name;
public int amountOfImgs;
public int[] eachLabelAmount;
public Node leftChild;
public Node rightChild;
public int choosenLabelLeft;
public int choosenLabelRight;
public double HL; //antropy of the node
public double HX; //antropy of both sons
public double IG; //improvement
public int amountOfImgsLeft;
public int amountOfImgsRight;
public int[] eachLabelAmountLeft;
public int[] eachLabelAmountRight;
public double HLa;//antropy of the left son
public double HLb;//antropy of the right son
public TrainingImage[] nodeImgs;
public TrainingImage[] leftNodeImgs;
public TrainingImage[] rightNodeImgs;
public double[] accScore;
public double[] accScoreLeft;
public double[] accScoreRight;
public int depth;
public int questionNumber;
public int pixi;
public int pixj;
public Node() {
this.label = -1;
this.name = null;
this.leftChild = null;//>128
this.rightChild = null;//<=128
this.amountOfImgs = 0;
this.eachLabelAmount = null;
this.choosenLabelLeft = -1;
this.choosenLabelRight = -1;
this.HL=0;//antropy of the node
this.HX=0;//antropy of both sons
this.IG=0;//improvement
this.amountOfImgsLeft=0;
this.amountOfImgsRight=0;
this.eachLabelAmountLeft = new int[10];
this.eachLabelAmountRight = new int[10];
this.HLa = 0;//antropy of the left son
this.HLb=0;//antropy of the right son
this.nodeImgs = null;
this.leftNodeImgs = null;
this.rightNodeImgs = null;
this.accScore = new double[10];
this.accScoreLeft = new double[10];
this.accScoreRight = new double[10];
this.depth = 1;
this.questionNumber = 0;
this.pixi=0;
this.pixj=0;
}
@SuppressWarnings("unchecked")
public Node copy(){
Node newNode = new Node();
newNode.label = this.label;
newNode.name = this.name;
newNode.amountOfImgs = this.amountOfImgs;
newNode.eachLabelAmount = this.eachLabelAmount ;
newNode.choosenLabelLeft = this.choosenLabelLeft;
newNode.choosenLabelRight = this.choosenLabelRight ;
newNode.HL = this.HL;//antropy of the node
newNode.HX = this.HX;//antropy of both sons
newNode.IG = this.IG;//improvement
newNode.amountOfImgsLeft = this.amountOfImgsLeft;
newNode.amountOfImgsRight = this.amountOfImgsRight;
newNode.eachLabelAmountLeft = this.eachLabelAmountLeft;
newNode.eachLabelAmountRight = this.eachLabelAmountRight;
newNode.HLa = this.HLa;//antropy of the left son
newNode.HLb = this.HLb;//antropy of the right son
newNode.nodeImgs = this.nodeImgs ;
newNode.leftNodeImgs = this.leftNodeImgs ;
newNode.rightNodeImgs = this.rightNodeImgs ;
newNode.accScore = this.accScore;
newNode.accScoreLeft = this.accScoreLeft ;
newNode.accScoreRight = this.accScoreRight;
newNode.depth = this.depth;
newNode.questionNumber = this.questionNumber;
newNode.pixi=this.pixi;
newNode.pixj = this.pixj;
if(this.leftChild != null){
newNode.leftChild = this.leftChild.copy();
}
if(this.rightChild != null){
newNode.rightChild = this.rightChild.copy();
}
return newNode;
}
@Override
public String toString() {
return name + " has the label " + label + " ";
}
}