1. 程式人生 > >AVL樹(Java實現)

AVL樹(Java實現)

height sea postorder int void brush node 情況 裏的

AVL樹基本操作

未完....待續....

AVL樹代碼

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);
    }

    
    //插入
    
    //刪除
}

  

AVL樹(Java實現)