代码如下:
function AvlTree() {
this.root = null;
}
/*二叉查找树节点/
function BstNode(key, value) {
this.key = key;
this.value = value;
//this.parent = parent || null;
this.left = null;
this.right = null;
}
//获得树的高度
function getAvlTreeHeight(node) {
var tempNode = node;
if (tempNode == null) {
return 0;
} else {
var leftHeight = getAvlTreeHeight(tempNode.left);
var rightHeight = getAvlTreeHeight(tempNode.right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
}
//判断树的平衡性
function balance(node) {
if (node == null) {
return node;
}
if (getAvlTreeHeight(node.left) - getAvlTreeHeight(node.right) > 1) {
if (getAvlTreeHeight(node.left.left) >= getAvlTreeHeight(node.left.right)) {
node = roateWithLeftChild(node);
} else {
node = doubleWithLeftChild(node);
}
} else if (getAvlTreeHeight(node.right) - getAvlTreeHeight(node.left) > 1) {
if (getAvlTreeHeight(node.right.right) >= getAvlTreeHeight(node.right.left)) {
node = roateWithRightChild(node);
} else {
node = doubleWithRightChild(node);
}
}
return node;
}
AvlTree.prototype._add = function (node, key, value) {
if (this.root == null) {
this.root = new BstNode(key, value);
return;
}
if (key < node.key) {
node.left == null ?
node.left = new BstNode(key, value, node) :
this._add(node.left, key, value);
node.left = balance(node.left);
}
else if (key > node.key) {
node.right == null ?
node.right = new BstNode(key, value, node) :
this._add(node.right, key, value);
node.right = balance(node.right);
}
return node;
};
//查找树中的最小key
function findMin(node) {
if (node == null) {
return;
}
while (node.left != null) {
node = node.left;
}
return node.key;
}
AvlTree.prototype._find = function (node, key) {
if (node == null || node.key == key) {
return node;
}
while (node != null) {
if (node.key < key) {
if (node.right == null || findMin(node.right) > key) {
return node;
} else {
node = node.right;
}
} else if (node.key > key) {
node = node.left;
} else {
return node;
}
}
};
AvlTree.prototype.add = function (key, value) {
return this._add(this.root, key, value);
};
AvlTree.prototype.find = function (key) {
return this._find(this.root, key);
};
//左子树单旋转
function roateWithLeftChild(AvlNode) {
var node = AvlNode.left;
AvlNode.left = node.right;
node.right = AvlNode;
return node;
}
//右子树单旋转
function roateWithRightChild(AvlNode) {
var node = AvlNode.right;
AvlNode.right = node.left;
node.left = AvlNode;
return node;
}
//左子树双旋转
function doubleWithLeftChild(AvlNode) {
AvlNode.left = roateWithRightChild(AvlNode.left);
return roateWithLeftChild(AvlNode);
}
//右子树双旋转
function doubleWithRightChild(AvlNode) {
AvlNode.right = roateWithLeftChild(AvlNode.right);
return roateWithRightChild(AvlNode);
}
//中序遍历
function inOrder(node) {
if (node != null) {
inOrder(node.left);
document.write(node.key + ” ” + “bstHeight= ” + getAvlTreeHeight(node) + “
”);
inOrder(node.right);
}
}
//前序遍历
function preOrder(node) {
if (node != null) {
document.write(node.key + ” ” + “bstHeight= ” + getAvlTreeHeight(node) + “
”);
preOrder(node.left);
preOrder(node.right);
}
}