Skip to content

Instantly share code, notes, and snippets.

@tarlanahad
Created February 10, 2018 17:52
Show Gist options
  • Save tarlanahad/b59d2a16e99c08c7adb1bc407fc6b3eb to your computer and use it in GitHub Desktop.
Save tarlanahad/b59d2a16e99c08c7adb1bc407fc6b3eb to your computer and use it in GitHub Desktop.
private void ReBalance(Node node) {
if (height(node.Left) - height(node.Right) > 1) {//if the problem in the left subtree
if (height(node.Left.Left) > height(node.Left.Right)) //if the problem in the left subtree
node = RightRotate(node);//do right subtree
else
node = LeftRightRotate(node);
} else {
if (height(node.Right.Left) > height(node.Right.Right))
node = LeftRotate(node);
else
node = RightLeftRotate(node);
}
if (node.Parent == null)//if the current node is root
Root = node;//adjust the root
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment