平衡二叉树

搜索二叉树是一种特殊的二叉树数据结构,它的特征是任意一个节点的值大于其左节点的值而小于等于其右节点的值。二叉树综合了数组和链表的特点,在数据的增删以及查询方面都有不俗的表现。但是一种特殊情况是如果我们向二叉树中插入的数据本身就是有序的,这是二叉树就会转换为"一边深"的线性结构,这时数据的查询就会演变为普通的线性查找,无疑是不能接受的,因此就引入的平衡二叉树的概念:当数据插入时通过旋转操作,保证树中任意节点的左右子树高度差不超过1,以此保持平衡。

package dataStructure;

/**
 * 平衡二叉树
 *
 * @author lyq
 * @create 5/6/19
 */
class Node {

    /**
     * 节点的值
     */
    int value;

    /**
     * 左子节点
     */
    Node leftNode;

    /**
     * 右子节点
     */
    Node rightNode;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Node node = (Node) o;

        return value == node.value;
    }

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

    /**
     * 添加节点
     * @param node
     */
    public void addNode(Node node) {
        //如果要插入节点的值小于当前节点,则交给它的左子节点去处理
        if (node.value < value) {
            if (this.leftNode == null) {
                this.leftNode = node;
            } else {
                this.leftNode.addNode(node);
            }
        //如果要插入节点的值大于等于当前节点,则交给它的右子节点去处理
        } else {
            if (this.rightNode == null) {
                this.rightNode = node;
            } else {
                this.rightNode.addNode(node);
            }
        }
        //树是否平衡
        //左子树高度较高
        if (leftHeight() - rightHeight() >= 2) {
            //如果左子树中的左子节点树高度小于其右子树,则先左旋再右旋
            if (leftNode != null && (leftNode.leftHeight() < leftNode.rightHeight())) {
                leftNode.leftRoll();
                rightRoll();
            } else {
                rightRoll();
            }
        }
        //右子树高度较高
        if (leftHeight() - rightHeight() <= -2) {
            //如果右节点的右子树高度小于其左子树的高度,则先右旋再整体左旋
            if (rightNode != null && rightNode.rightHeight() < rightNode.leftHeight()){
                rightNode.rightRoll();
                this.leftRoll();
            } else {
                this.leftRoll();
            }
        }
    }

    /**
     * 右旋
     */
    private void rightRoll() {
        //创建一个新节点,值等于当前节点的值
        Node newRight = new Node(value);
        //将当前节点的右子节点作为新节点的右子节点
        newRight.rightNode = rightNode;
        //将当前节点左子节点的右子节点设置为新节点的左子节点
        newRight.leftNode = leftNode.rightNode;
        //把当前节点的值改为其左子节点的值
        value = leftNode.value;
        //将当前节点的左子节点设置为其左子节点的左子节点
        leftNode = leftNode.leftNode;
        //将新节点设置为当前节点的右子节点
        rightNode = newRight;
    }

    /**
     * 左旋
     */
    private void leftRoll() {
        //创建一个新节点等于当前节点
        Node newLeft = new Node(value);
        //将当前节点的左子树设置为新节点的左子树
        newLeft.leftNode = leftNode;
        //将当前节点的右子节点的左子节点设置为新节点的右子节点
        newLeft.rightNode = rightNode.leftNode;
        //将当前节点的值替换为当前节点的右子节点的值
        value = rightNode.value;
        //将当前节点的右子节点设置为当前节点右子节点的右子节点
        rightNode = rightNode.rightNode;
        //将新节点设置为当前节点的左子节点
        leftNode = newLeft;
    }

    /**
     * 获取右子树高度
     * @return
     */
    private int rightHeight() {
        if (rightNode == null) {
            return 0;
        } else {
            return rightNode.height();
        }
    }

    /**
     * 获取左子树高度
     * @return
     */
    private int leftHeight() {
        if (leftNode == null) {
            return 0;
        } else {
            return leftNode.height();
        }
    }

    /**
     * 获取树的高度
     * @return
     */
    private int height() {
        return Math.max(leftHeight(),rightHeight()) + 1;
    }

    /**
     * 前序遍历
     */
    public void frontShow() {
        //打印当前节点的值
        System.out.println(value);
        //递归遍历左子树
        if (this.leftNode != null) {
            this.leftNode.frontShow();
        }
        //递归遍历右子树
        if (this.rightNode != null) {
            this.rightNode.frontShow();
        }
    }

    /**
     * 查找指定节点
     * @param i
     */
    public Node search(int i) {
        if (this.value == i) {
            return this;
        } else if (i < this.value) {
            if (this.leftNode == null) {
                return null;
            } else {
                return this.leftNode.search(i);
            }
        } else {
            if (this.rightNode == null) {
                return null;
            } else {
                return this.rightNode.search(i);
            }
        }
    }

    /**
     * 删除节点
     * @param i
     */
    public void delete(int i) {

    }

    /**
     * 寻找指定节点父节点
     * @param i
     * @return
     */
    public Node searchParent(int i) {
        if ((this.leftNode != null && this.leftNode.value == i) || (this.rightNode != null && this.rightNode.value == i)) {
            return this;
        } else if (this.value > i && this.leftNode != null) {
            return this.leftNode.searchParent(i);
        } else if (this.value < i && this.rightNode != null) {
            return this.rightNode.searchParent(i);
        } else {
            return null;
        }
    }
}

class BinarySearchTree {

    private Node root;

    /**
     * 添加节点
     * @param node
     */
    public void add(Node node) {
        if (root == null) {
            root = node;
            return;
        } else {
            root.addNode(node);
        }
    }

    /**
     * 前序、中序、后续遍历都是针对父节点与其两个子节点的遍历先后顺序而演变出来的不同遍历方式
     * 这里只演示了前序遍历的代码,其他两种类似,只需改变父节点与子节点的遍历顺序即可
     */
    public void frontShow() {
        root.frontShow();
    }

    /**
     * 删除节点
     * @param i
     */
    public void delete(int i) {
        Node target = search(i);
        if (target != null) {
            if (target.equals(root)) {
                root = null;
                return;
            }
            Node parent = searchParent(i);
            //要删除的节点是叶子节点
            if (target.leftNode == null && target.rightNode == null) {
                if (target.equals(parent.leftNode)) {
                    parent.leftNode = null;
                    return;
                } else {
                    parent.rightNode = null;
                    return;
                }
            //要删除的节点有两个子节点时,将其中序后继结点的值附给当前节点,同时删除其后继节点
            } else if (target.leftNode != null && target.rightNode != null) {
                //寻找要删除节点的后继节点
                int nextMin = deleteNextMin(target.rightNode);
                target.value = nextMin;
            //只有一个叶子节点
            } else {
                if (target.leftNode != null) {
                    //要删除节点是父节点的左子节点
                    if (target.equals(parent.leftNode)) {
                        parent.leftNode = target.leftNode;
                    } else {
                        parent.rightNode = target.leftNode;
                    }
                } else {
                    if (target.equals(parent.leftNode)) {
                        parent.leftNode = target.rightNode;
                    } else {
                        parent.rightNode = target.rightNode;
                    }
                }
            }
        }
    }

    /**
     * 删除中序后继节点
     * @param rightNode
     * @return
     */
    public int deleteNextMin(Node rightNode) {
        Node nextMin = rightNode;
        while (nextMin.leftNode != null) {
            nextMin = nextMin.leftNode;
        }
        delete(nextMin.value);
        return nextMin.value;
    }

    /**
     * 寻找指定节点的父节点
     * @param i
     * @return
     */
    public Node searchParent(int i) {
        return root.searchParent(i);
    }

    /**
     * 查找节点
     * @param i 要查找节点的值
     * @return
     */
    public Node search(int i) {
        if (root == null) {
            return null;
        }
        return root.search(i);
    }
}

public class Test{

    public static void main(String[] args) {
        int[] arr = {5,1,6,2,3,4};
        BinarySearchTree tree = new BinarySearchTree();
        for (int i = 0;i < arr.length;i++) {
            tree.add(new Node(arr[i]));
        }
        //前序遍历
        tree.frontShow();
        //删除元素
//        tree.delete(1);
        System.out.println("============");
//        tree.frontShow();
    }

}

在通过旋转保持平衡的过程中,有时需要双旋转!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值