public class LC {
Node[] nodes = null;
public void init(String names) {
String[] split = names.split(",");
nodes = new Node[split.length+1];
for (int i = 0; i < split.length; i++) {
Node node = new Node(split[i]);;
nodes[i+1] = node;
}
}
public void request(){
System.out.println("before:"+Arrays.toString(nodes));
Node node = nodes[1];
node.num.incrementAndGet();
int i = 1;
while (i << 1 < nodes.length) {
int flag = i;
int left = 1<<i;
int right = left+1;
if (nodes[left].num.get() < nodes[flag].num.get()) {
flag = left;
}
if (right < nodes.length && nodes[right].num.get() < nodes[flag].num.get()) {
flag = right;
}
if (i != flag) {
Node temp = nodes[i];
nodes[i] = nodes[flag];
nodes[flag] = temp;
i = flag;
}else {
break;
}
}
System.out.println("after:"+ Arrays.toString(nodes));
System.out.println("==========================");
}
public static void main(String[] args) {
LC lc = new LC();
lc.init("a,b,c,d,e");
for (int i = 0; i < 10; i++) {
lc.request();
}
}
class Node{
String name;
AtomicInteger num = new AtomicInteger();
public Node(String name) {
this.name = name;
}
@Override
public String toString() {
return "Node{" +
"name='" + name + '\'' +
", num=" + num +
'}';
}
}
}