for (int i = 0; i < tree.getHeaderTableCount(); i++) {
int currentAttribute = tree.getAttributeAtIndex(i);
int nextNode = tree.getHeaderNext(currentAttribute);
OpenIntIntHashMap prevNode = new OpenIntIntHashMap();
int justPrevNode = -1;
while (nextNode != -1) {
int parent = tree.parent(nextNode);
if (prevNode.containsKey(parent)) {
int prevNodeId = prevNode.get(parent);
if (tree.childCount(prevNodeId) <= 1 && tree.childCount(nextNode) <= 1) {
tree.addCount(prevNodeId, tree.count(nextNode));
tree.addCount(nextNode, -1 * tree.count(nextNode));
if (tree.childCount(nextNode) == 1) {
tree.addChild(prevNodeId, tree.childAtIndex(nextNode, 0));
tree.setParent(tree.childAtIndex(nextNode, 0), prevNodeId);
}
tree.setNext(justPrevNode, tree.next(nextNode));
}
} else {
prevNode.put(parent, nextNode);
}
justPrevNode = nextNode;
nextNode = tree.next(nextNode);
}
}