function Node(key) {
this.key = key;
this.height = 0;
this.left = null;
this.right = null;
}
function AVLTree(head) {
this.root = head;
}
AVLTree.prototype.add = function(node, key) {
if(node == null) {
return new Node(key);
}
if (key < node.key) {
node.left = this.add(node.left, key);
} else {
node.right = this.add(node.right, key);
}
return this.balance(node);
}
Node.prototype._getDiffer = function(node) {
return (node ? node._getHeight(node.right) - node._getHeight(node.left) : 0);
}
Node.prototype._getHeight = function(node) {
return (node ? node.height : -1);
}
AVLTree.prototype._fixHeight = function(node) {
if(node) {
node.height = ( node._getHeight(node.left) > node._getHeight(node.right)
? node._getHeight(node.left) : node._getHeight(node.right) ) + 1;
}
}
AVLTree.prototype.balance = function(node) {
this._fixHeight(node);
if (node._getDiffer(node) == 2) {
if (node._getDiffer(node.right) < 0) {
node.right = this.rotateRight(node.right);
}
return this.rotateLeft(node);
}
if (node._getDiffer(node) == -2) {
if (node._getDiffer(node.left) > 0) {
node.left = this.rotateLeft(node.left);
}
return this.rotateRight(node);
}
return node;
}
AVLTree.prototype.rotateRight = function(node) {
let temp = node.left;
node.left = temp.right;
temp.right = node;
this._fixHeight(temp);
this._fixHeight(node);
return temp;
}
AVLTree.prototype.rotateLeft = function(node) {
let temp = node.right;
node.right = temp.left;
temp.left = node;
this._fixHeight(node);
this._fixHeight(temp);
return temp;
}
AVLTree.prototype.getMin = function(node) {
return (node.left ? this.getMin(node.left) : node);
}
AVLTree.prototype.getMax = function(node) {
return (node.right ? this.getMax(node.right) : node);
}
AVLTree.prototype.removeMin = function(node) {
if (!node.left) {
return node.right;
}
node.left = this.removeMin(node.left);
return this.balance(node);
}
AVLTree.prototype.removeNode = function(node, key) {
if (!node) {
return 0;
}
if (key < node.key) {
node.left = this.removeNode(node.left, key);
}
if (key > node.key) {
node.right = this.removeNode(node.right, key);
}
if (key == node.key) {
let temp = node.left;
let temp1 = node.right;
if (!temp1) {
return temp;
}
let min = this.getMin(temp1);
min.right = this.removeMin(temp1);
min.left = temp;
return this.balance(min);
}
return this.balance(node);
}
let head = new Node(1);
let tree = new AVLTree(head);
tree.root = tree.add(tree.root, 2);
tree.root = tree.add(tree.root, 3);
tree.root = tree.add(tree.root, 4);
tree.root = tree.add(tree.root, 5);
tree.root = tree.add(tree.root, 6);
tree.root = tree.add(tree.root, 7);
tree.root = tree.add(tree.root, 8);
tree.root = tree.add(tree.root, 9);
tree.root = tree.add(tree.root, 10);
console.log(tree.root);