avl树的java实现代码_AVL树(Java实现)

AVL树基本操作

未完....待续....

AVL树代码

public class AVLTree, 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);

}

//插入

//删除

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值