-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCountTree.java
More file actions
140 lines (115 loc) · 3.25 KB
/
CountTree.java
File metadata and controls
140 lines (115 loc) · 3.25 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
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
import java.util.ArrayList;
import java.util.List;
public class CountTree<K> extends AbstractHierarchicalStatisticTree<K> implements IHierarchicalStatisticTree<K> {
/**
* maintain all children belongs to this SumTree node
*/
private ArrayList<CountTree<K>> children;
//Constructors
public CountTree(){
super();
children = new ArrayList<CountTree<K>>();
}
public CountTree(K key) {
this();
this.key = key;
}
public CountTree(K[] kArray){
this();
add(kArray);
}
public CountTree(List<K[]> list){
this();
buildTree(list);
}
//Build methods
@Override
public void buildTree(List<K[]> list) {
for(K[] kArray: list){
incrCount();
buildByCount(kArray, 0);
}
}
/**
* Build a CountTree by counting value recursively.
* @param kArray
* @param index
*/
private void buildByCount(K[] kArray, int index) {
if(index >= (kArray.length))
return;
if(kArray[index]==null)
throw new NullPointerException("key value can not be null!");
//Search for the tempHST in children list
CountTree<K> tempCountTree = new CountTree<K>(kArray[index]);
int indexOfChildren = children.indexOf(tempCountTree);
if(indexOfChildren >= 0){
tempCountTree = children.get(indexOfChildren);
tempCountTree.incrCount();
tempCountTree.buildByCount(kArray, ++index);
}else{
tempCountTree.incrCount();
tempCountTree.buildByCount(kArray, ++index);
children.add(tempCountTree);
}
}
@Override
public void add(K[] kArray) {
incrCount();
buildByCount(kArray, 0);
}
@Override
public Integer getResult(K[] kArray) {
if(kArray==null || kArray.length<=0)
return getCount();
CountTree<K> tempCountTree = new CountTree<K>(kArray[0]);
for(CountTree<K> countTree: children){ //Searching tempHST
if(tempCountTree.equals(countTree)){
return countTree.getCount(kArray, 0); //Start to searching accumulated value from this SumTree
}
}
return 0; //If not exist
}
private Integer getCount(K[] kArray, int index){
if(index > kArray.length || kArray[index]==null)
return 0;
CountTree<K> tempCountTree = new CountTree<K>(kArray[index]);
if(tempCountTree.equals(this)){
index++; //looking for next key value
if(index >= kArray.length){
return count; //match all key value, return result
}else if(kArray[index]==null){
return 0; //if next key value is null, then this key doesn't exist
}else{
//searching next key value in children
tempCountTree = new CountTree<K>(kArray[index]);
int indexOfChildren = children.indexOf(tempCountTree);
return (indexOfChildren >= 0) ? children.get(indexOfChildren).getCount(kArray, index) : 0;
}
}
return 0;
}
/**
* Starting point to print whole tree
*/
@Override
public void printTree() {
System.out.println(toString());
for(CountTree<K> hst: children){
hst.print(toString());
}
}
/**
* Printing whole tree recursively.
* @param parent
*/
private void print(String parent){
System.out.println(parent + toString());
for(CountTree<K> hst: children){
hst.print(parent + toString());
}
}
private void incrCount(){
this.count+=1;
}
}