AVL树基本操作

public class AVLTree {

    public static class Node {
        public int data;
        public int height;
        public Node left;
        public Node right;

        public Node(int data) {
            this.data = data;
            this.height = 1;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "data=" + data +
                    ", height=" + height +
                    '}';
        }
    }

    public int heightOf(Node node) {
        return node == null ? 0 : node.height;
    }

    public int balanceOf(Node node) {
        return node == null ? 0 : heightOf(node.left) - heightOf(node.right);
    }

    public Node rotateLeft(Node node) {
        Node rightNode = node.right;
        node.right = rightNode.left;
        rightNode.left = node;

        node.height = Math.max(heightOf(node.left), heightOf(node.right)) + 1;
        rightNode.height = Math.max(heightOf(rightNode.left), heightOf(rightNode.right)) + 1;

        return rightNode;
    }

    public Node rotateRight(Node node) {
        Node leftNode = node.left;
        node.left = leftNode.right;
        leftNode.right = node;

        node.height = Math.max(heightOf(node.left), heightOf(node.right)) + 1;
        leftNode.height = Math.max(heightOf(leftNode.left), heightOf(leftNode.right)) + 1;

        return leftNode;
    }

    public Node insert(Node root, int data) {
        if (root == null) {
            return new Node(data);
        }

        if (data < root.data) {
            root.left = insert(root.left, data);
        } else if (data > root.data) {
            root.right = insert(root.right, data);
        } else {
            return root;
        }

        root.height = Math.max(heightOf(root.left), heightOf(root.right)) + 1;

        int balance = balanceOf(root);

        //check balance

        //LL
        if (balance > 1 && data < root.left.data) {
            return rotateRight(root);
        }

        //RR
        if (balance < -1 && data > root.right.data) {
            return rotateLeft(root);
        }

        //LR
        if (balance > 1 && data > root.left.data) {
            root.left = rotateLeft(root.left);
            return rotateRight(root);
        }

        //RL
        if (balance < -1 && data < root.right.data) {
            root.right = rotateRight(root.right);
            return rotateLeft(root);
        }

        return root;
    }

    public Node delete(Node root, int data) {
        if (root == null) {
            return root;
        }

        if (data < root.data) {
            root.left = delete(root.left, data);
        } else if (data > root.data) {
            root.right = delete(root.right, data);
        } else {
            if (root.left == null || root.right == null) {

                Node temp = null;
                if (root.left != null) {
                    temp = root.left;
                } else if (root.right != null) {
                    temp = root.right;
                }

                root = temp;
            } else {
                Node minNode = minValueNode(root.right);
                root.data = minNode.data;
                root.right = delete(root.right, minNode.data);
            }
        }

        if (root == null) {
            return root;
        }

        root.height = Math.max(heightOf(root.left), heightOf(root.right)) + 1;
        int rootBalance = balanceOf(root);
        int leftBalance = balanceOf(root.left);
        int rightBalance = balanceOf(root.right);

        //check balance of root
        if (rootBalance > 1 && leftBalance >= 0) {
            root = rotateRight(root);
        }

        if (rootBalance > 1 && leftBalance < 0) {
            root.left = rotateLeft(root.left);
            root = rotateRight(root);
        }

        if (rootBalance < -1 && rightBalance <= 0) {
            root = rotateLeft(root);
        }

        if (rootBalance < -1 && rightBalance > 0) {
            root.right = rotateRight(root.right);
            root = rotateLeft(root);
        }

        return root;
    }

    public Node minValueNode(Node root) {
        while (root.left != null) {
            root = root.left;
        }
        return root;
    }

    public void printTreeInOrder(Node root) {
        if (root != null) {
            printTreeInOrder(root.left);
            System.out.println(root.data);
            printTreeInOrder(root.right);
        }
    }

    public Node search(Node root, int data) {
        if (root == null) {
            return null;
        }

        if (data < root.data) {
            return search(root.left, data);
        } else if (data > root.data) {
            return search(root.right, data);
        } else {
            return root;
        }
    }

    public static void main(String[] args) {
        AVLTree tree = new AVLTree();

        Node root = null;

        //create
        for (int i = 1; i <= 100; i++) {
            root = tree.insert(root, i);
        }

        //search
        Node targetNode = tree.search(root, 50);
        System.out.println("search result:" + targetNode);

        //delete
        tree.delete(root, 50);
        tree.delete(root, 5);

        tree.printTreeInOrder(root);

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值