Skip to content

Instantly share code, notes, and snippets.

@cwndrws
Last active December 31, 2015 17:39
Show Gist options
  • Save cwndrws/8021518 to your computer and use it in GitHub Desktop.
Save cwndrws/8021518 to your computer and use it in GitHub Desktop.
public void add() {
Node newNode = Node();
addChild(root, newNode);
}
public void addChild(Node parent, Node child) {
if (parent.leftChild == null) {
parent.leftChild = child;
} else if (parent.rightChild == null) {
parent.rightChild = child;
} else {
weightLeft = getWeight(parent.leftChild, 0)
weightRight = getWeight(parent.rightChild, 0)
if (weightLeft >= weightRight) {
addChild(parent.leftChild, child)
} else {
addChild(parent.rightChild, child)
}
}
}
public int getWeight(Node testNode, int len) {
if (testNode.leftChild == null && testNode.rightChild == null) {
return len + 1;
} else if (testNode.leftChild == null) {
return getWeight(testNode.rightChild, 1);
} else if (testNode.rightChild == null) {
return getWeight(testNode.leftChild, 1);
} else {
return getWeight(testNode.leftChild, len) + getWeight(testNode.rightChild, len)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment