1. 程式人生 > 其它 >平衡二叉樹(AVL)詳解

平衡二叉樹(AVL)詳解

平衡二叉樹(AVL)詳解

說明

  1. 平衡二叉樹又稱平衡二叉排序樹,是二叉排序樹的一種特殊型別

  2. 平衡二叉樹主要為了解決二叉搜尋樹出現的一些問題,比如如果二叉搜尋樹的各個節點的值是按照順序的,那麼二叉排序樹的形式會形如單鏈表,但是它的查詢速度會比單鏈錶慢,因為二叉排序樹在遍歷時還要考慮左子樹或者右子樹,即使他們都是空的,因此引入平衡二叉樹

  3. 平衡二叉樹要求左右子樹的高度差的絕對值小於等於1,因此它是一顆平衡樹

  4. 平衡二叉樹在建立時,會不停的判斷左子樹和右子樹的高度關係,一旦左子樹於右子樹的高度差大於1,就需要左旋或者右旋甚至雙旋轉進行矯正

  5. 左旋是指左子樹的高度-右子樹的高度>1的情況,具體思路如下:

    • 建立一個新節點,將當前節點的值指向新節點

    • 然後新節點的左子樹指向當前節點的左子樹

    • 新節點的右子樹指向當前節點右子樹的左子樹

    • 當前節點的值用當前節點右子樹的值替換

    • 當前節點的右子樹指向右子樹的右子樹

    • 當前節點的左子樹指向新節點

  6. 左旋是右子樹的高度 - 左子樹的高度 > 1的情況,思路如同左旋,方向改變即可

  7. 雙旋轉是指當前節點的左子樹高度>右子樹的高度,需要右旋,但是當前節點左子樹的右子樹高度>左子樹的高度,如果不對左子樹進行處理直接右旋的話,會發現旋轉後的樹依舊不平衡,會出現右子樹的高度大於左子樹的高度,右需要左旋,但是依舊不成功,因此需要對當前節點的左子樹進行處理,先將左子樹進行左旋,再將當前樹進行右旋

  8. 另一種情況,及右子樹的高度>左子樹的高度,但右子樹的左子樹的高度大於右子樹的高度,這種情況也雷同,因此需要先對當前節點的右子樹進行右旋,再對當前節點進行左旋

  9. 原始碼及思路見下

原始碼及分析

左旋右旋
//右旋轉,思路和左旋轉類似
    public void rightRotate() {
        Node newNode = new Node(this.value);
        newNode.right = this.right;
        newNode.left = this.left.right;
        this.value = this.left.value;
        this.left = this.left.left;
        this.right = newNode;
    }

    //左旋轉
    public void leftRotate() {
        //建立新樹的根節點
        Node newNode = new Node(this.value);
        //新樹左子樹連線當前樹的左子樹
        newNode.left = this.left;
        //新樹的右子樹連線當前樹的右子樹的左子樹
        newNode.right = this.right.left;
        //當前根節點的值用右節點的值替換
        this.value = this.right.value;
        //當前樹的右子樹連線右子樹的右子樹
        this.right = this.right.right;
        //將新樹作為當前樹的左子樹
        this.left = newNode;
    }

    //左子樹高度
    public int leftHeight() {
        if (left == null) {
            return 0;
        }
        return left.height();
    }

    //右子樹高度
    public int rightHeight() {
        if (right == null) {
            return 0;
        }
        return right.height();
    }

    //返回以當前節點為根節點的樹的高度
    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }
平衡二叉樹程式碼實現
package algorithm.tree.avl;

/**
 * @author AIMX_INFO
 * @version 1.0
 */
public class AVLTree {
    public static void main(String[] args) {
        AVL avl = new AVL();
        //int[] arr = {4, 3, 6, 5, 7, 8};
        //int[] arr = {10, 12, 8, 9, 7, 6};
        int[] arr = {10, 11, 7, 6, 8, 9};
        for (int i = 0; i < arr.length; i++) {
            avl.add(new Node(arr[i]));
        }
        //中序遍歷
        avl.infixOrder();
        //樹的高度
        System.out.println("樹高度: " + avl.getRoot().height());
        System.out.println("左子樹高度: " + avl.getRoot().leftHeight());
        System.out.println("右子樹高度: " + avl.getRoot().rightHeight());

    }
}

//二叉排序樹
class AVL {
    private Node root;

    //中序遍歷
    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("樹是空的");
        }
    }

    public Node getRoot() {
        return root;
    }

    //新增節點
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    //查詢某一節點
    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    //查詢某一節點的父節點
    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    //刪除節點
    public void delNode(int value) {
        //先判斷是否為空樹
        if (root == null) {
            return;
        } else {
            //如果樹不為空,再判斷樹是否只有一個空節點
            if (root.left == null && root.right == null && root.value == value) {
                root = null;
                return;
            }
            //否則先查詢要刪除的節點
            Node target = search(value);
            //判斷要刪除節點是否存在
            if (target == null) {
                return;
            }
            //如果存在則再找到要刪除節點的父節點
            Node parent = searchParent(value);
            //然後根據要刪除的節點分情況刪除
            //如果要刪除的節點是葉子節點
            if (target.left == null && target.right == null) {
                //判斷target是父節點的左子節點還是右子節點
                //如果是左子節點
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                }
                //如果是左子節點
                if (parent.right != null && parent.right.value == value) {
                    parent.right = null;
                }
                //如果要刪除的節點有兩個子節點
            } else if (target.left != null && target.right != null) {
                int minVal = delRightNodeMin(target.right);
                target.value = minVal;

                //否則要刪除的節點只有一個子節點
            } else {
                //判斷要刪除的節點有左子節點還是右子節點
                //target的左子節點不為空
                if (target.left != null) {
                    //判斷target是父節點的左子樹還是右子樹
                    //左子樹
                    if (parent != null) {
                        if (parent.left.value == value) {
                            parent.left = target.left;
                        } else {
                            //右子樹
                            parent.right = target.left;
                        }
                    } else {
                        root = target.left;
                    }
                    //target的右子節點不為空
                } else {
                    //同理
                    if (parent != null) {
                        if (parent.left.value == value) {
                            parent.left = target.right;
                        } else {
                            parent.right = target.right;
                        }
                    } else {
                        root = target.right;
                    }
                }
            }
        }
    }

    /**
     * 查詢當前二叉排序樹的最小節點並刪除
     *
     * @param node 當前二叉排序樹
     * @return 返回最小節點的值
     */
    public int delRightNodeMin(Node node) {
        //輔助變數用於遍歷二叉排序樹
        Node target = node;
        //迴圈查詢最小節點
        while (target.left != null) {
            target = target.left;
        }
        //迴圈結束時已經找到
        //刪除當前節點
        delNode(target.value);
        //返回當前節點的值
        return target.value;
    }

}

//節點
class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    //右旋轉,思路和左旋轉類似
    public void rightRotate() {
        Node newNode = new Node(this.value);
        newNode.right = this.right;
        newNode.left = this.left.right;
        this.value = this.left.value;
        this.left = this.left.left;
        this.right = newNode;
    }

    //左旋轉
    public void leftRotate() {
        //建立新樹的根節點
        Node newNode = new Node(this.value);
        //新樹左子樹連線當前樹的左子樹
        newNode.left = this.left;
        //新樹的右子樹連線當前樹的右子樹的左子樹
        newNode.right = this.right.left;
        //當前根節點的值用右節點的值替換
        this.value = this.right.value;
        //當前樹的右子樹連線右子樹的右子樹
        this.right = this.right.right;
        //將新樹作為當前樹的左子樹
        this.left = newNode;
    }

    //左子樹高度
    public int leftHeight() {
        if (left == null) {
            return 0;
        }
        return left.height();
    }

    //右子樹高度
    public int rightHeight() {
        if (right == null) {
            return 0;
        }
        return right.height();
    }

    //返回以當前節點為根節點的樹的高度
    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    //查詢某一節點

    /**
     * @param value 要查詢的節點的值
     * @return 返回查詢的結果
     */
    public Node search(int value) {
        //如果要查詢的節點就是當前節點,直接返回
        if (value == this.value) {
            return this;
        }
        //判斷要查詢的節點的value與當前節點的value的大小關係
        //向左遞迴查詢
        if (value < this.value) {
            //判斷左子樹是否為空
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            //向右遞迴查詢
            //判斷右子樹是否為空
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }
    }
    //查詢要刪除節點的父節點

    /**
     * @param value 要刪除的節點的值
     * @return 返回查詢的結果
     */
    public Node searchParent(int value) {
        //判斷當前節點是不是要查詢節點的父節點
        if ((this.left != null && this.left.value == value) ||
                (this.right != null && this.right.value == value)) {
            return this;
        } else {
            //如果不是則向左向右遞迴查詢
            //向左遞迴
            if (value < this.value && this.left != null) {
                return this.left.searchParent(value);
                //向右遞迴
            } else if (value >= this.value && this.right != null) {
                return this.right.searchParent(value);
            } else {
                //否則沒有找到
                return null;
            }
        }
    }

    //遞迴新增節點的方法
    public void add(Node node) {
        //資料校驗
        if (node == null) {
            return;
        }
        //根據要新增的節點的值和當前節點值的大小判斷節點要新增的位置
        if (node.value < this.value) {
            //如果當前左子節點為空,則直接新增
            if (this.left == null) {
                this.left = node;
            } else {
                //否則遞迴新增
                this.left.add(node);
            }
        } else {
            //同理
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
        //新增完節點後,如果 (右子樹的高度 - 左子樹的高度 ) > 1,則左旋
        if (rightHeight() - leftHeight() > 1) {
            //如果當前右子樹的左子樹高度大於右子樹高度,則先對當前右子樹右旋
            //再對當前數左旋
            if (right != null && right.leftHeight() > right.rightHeight()) {
                right.rightRotate();
                leftRotate();
            } else {
                //否則直接左旋
                leftRotate();
            }
            //開始下一次新增節點
            return;
        }
        //新增完節點後,如果 (左子樹的高度 - 右子樹的高度 ) > 1,則右旋
        if (leftHeight() - rightHeight() > 1) {
            //如果當前左子樹的右子樹高度大於左子樹高度,則先對左子樹進行左旋,
            //再對當前樹右旋
            if (left != null && left.rightHeight() > left.leftHeight()) {
                left.leftRotate();
                rightRotate();
            } else {
                //否則直接右旋
                rightRotate();
            }
        }
    }

    //中序遍歷
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

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