AVL樹(Java實現)

來源:互聯網
上載者:User

標籤:--   print   插入   tor   private   font   search   root   turn   

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實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.