标签:-- print 插入 tor private font search root turn
未完....待续....
public class AVLTree<Key extends Comparable<? super Key>, Value> {
private class Node {
Key key;//键,相当于词典里的单词
Value value;//值,相当于词典里的单词解释
int height;//结点的高度
Node left;
Node right;
public Node(Key key, Value value) {
this.key = key;
this.value = value;
this.left = null;
this.right = null;
int height = 0;
}
}
private Node root;
public AVLTree() {
root = null;
}
private int height(Node node) {
if (node != null) {
return node.height;
}
return 0;
}
public int height() {
return height(root);
}
private int max(int a, int b) {
return a > b ? a : b;
}
private void preOrder(Node node) {
if (node != null) {
System.out.println(node.key);
preOrder(node.left);
preOrder(node.right);
}
}
public void preOrder() {
preOrder(root);
}
private void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
System.out.println(node.key);
inOrder(node.right);
}
}
public void inOrder() {
inOrder(root);
}
public void postOrder(Node node) {
if (node != null) {
postOrder(node.left);
postOrder(node.right);
System.out.println(node.key);
}
}
public void postOrder() {
postOrder(root);
}
private Node search(Node node, Key key) {
if (node == null) {
return null;
} else if (key.compareTo(node.key) == 0) {
return node;
} else if (key.compareTo(node.key) < 0) {
return search(node.left, key);
} else {//key.compareTo(node.key) > 0
return search(node.right, key);
}
}
public Node search(Key key) {
return search(root, key);
}
private Node minNode(Node node) {
if (node == null) {
return null;
} else if (node.left == null) {
return node;
} else {
return minNode(node.left);
}
}
public Node minNode() {
return minNode(root);
}
private Node maxNode(Node node) {
if (node == null) {
return null;
} else if (node.right == null) {
return node;
} else {
return maxNode(node.right);
}
}
public Node maxNode() {
return maxNode(root);
}
// 对如下的LL情况
//
// k1 k2
// / \ / // k2 z LL转 x k1
// / \ ----\ / / // x y ----/ o y z
// /
// o
//
// 或
//
// k1 k2
// / \ / // k2 z LL转 x k1
// / \ ----\ \ / // x y ----/ o y z
// // o
//
private Node leftLeftRotation(Node k1) {
Node k2 = k1.left; //k2是k1的左子树
k1.left = k2.right;//k2的右子树 变为 k1 的左子树
k2.right = k1; //k1变为k2的右子树
k1.height = max(height(k1.left), height(k1.right)) + 1;//计算k1的高度
k2.height = max(height(k2.left), k1.height) + 1;//计算k2的高度
return k2;//返回新的根k2
}
// 对如下的RR情况
//
// k1 k2
// / \ / // x k2 RR转 k1 k3
// / \ ----\ / \ // y k3 ----/ x y z
// // z
//
// 或
//
// k1 k2
// / \ / // x k2 RR转 k1 k3
// / \ ----\ / \ /
// y k3 ----/ x y z
// /
// z
//
public Node rightRightRotation(Node k1) {
Node k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
k1.height = max(height(k1.left), height(k1.right)) + 1;
k2.height = max(k1.height, height(k2.right)) + 1;
return k2;
}
// 对如下的LR情况
// k1 k1 k3
// / \ / \ / // k2 z k2左旋 k3 z k1右旋 k2 k1
// / \ -----\ / \ -----\ / \ / // w k3 -----/ k2 y -----/ w x y z
// / \ RR单转 / \ LL单转
// x y w x
//
public Node leftRightRotation(Node k1) {
k1.left = rightRightRotation(k1.left);
return leftLeftRotation(k1);
}
// 对如下的RL情况
// k1 k1 k3
// / \ k2右旋 / \ k1左旋 / // w k2 -----\ w k3 -----\ k1 k2
// / \ -----/ / \ -----/ / \ / // k3 z LL单转 x k2 RR单旋 w x y z
// / \ / // x y y z
//
public Node rightLeftRotation(Node k1) {
k1.right = leftLeftRotation(k1.right);
return rightRightRotation(k1);
}
//插入
//删除
}
标签:-- print 插入 tor private font search root turn
原文地址:http://www.cnblogs.com/noKing/p/8001608.html