Last active
December 31, 2015 17:39
-
-
Save cwndrws/8021518 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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