Java二叉排序树和平衡二叉树

二叉排序树

一、概述

二叉排序树:BST(Binary Sort Tree),对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大。

特别说明:如果有相同的值,可以将该叶子节点放在左子节点或右子节点
在这里插入图片描述

二、创建和遍历

/**
 * @author DELL
 * @Date 2020/2/13 11:23
 **/
public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9};
        BinarySortTree sortTree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            sortTree.add(new Node(arr[i]));
        }
        sortTree.infixOrder();
    }
}
//二叉排序树
class BinarySortTree {
    private Node root;

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("当前二叉树为空!");
        }
    }
}
//节点
class Node {
    int val;
    Node left;
    Node right;

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

    /**
     * 添加元素
     *
     * @param node
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.val < this.val) {
            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);
            }
        }
    }

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

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

三、删除

  • 删除叶子节点
  • 删除只有一棵子树的节点
  • 删除有两棵子树的节点

删除节点的核心代码及注解

class BinarySortTree {
    private Node root;

    public Node searchParent(int val) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(val);
        }
    }

    /**
     * 返回以node为根节点的二叉排序树的最小值
     * 删除node为根节点的二叉排序树的最小节点
     *
     * @param node 传入的节点(当成根节点)
     * @return 返回以node为根节点的二叉排序树的最小值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        //循环左子节点
        while (target.left != null) {
            target = target.left;
        }
        //删除该节点
        delNode(target.val);
        return target.val;
    }

    /**
     * 删除节点
     *
     * @param val 需要删除的节点
     */
    public void delNode(int val) {
        if (root == null) {//如果根节点为空则直接返回
            return;
        } else {
            //需要删除的节点
            Node targetNode = search(val);
            if (targetNode == null) {//如果此节点为空则说明没有此节点
                return;
            }
            if (root.left == null && root.right == null) {//需要删除的节点为根节点
                root = null;
                return;
            }
            //需要删除节点的父节点
            Node parent = searchParent(val);
            if (targetNode.left == null && targetNode.right == null) {//删除的节点为叶子节点
                if (parent.left != null && parent.left.val == val) {//左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.val == val) {//右子节点
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) {//删除只两棵子树的节点
                targetNode.val = delRightTreeMin(targetNode.right);
            } else {//删除有一棵子树的节点
                //删除的节点有左子节点
                if (targetNode.left != null) {
                    if (parent != null) {
                        //如果targetNode是parent的左子节点
                        if (parent.left.val == val) {
                            parent.left = targetNode.left;
                        } else {//targetNode是parent的左子节点
                            parent.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }
                } else {//删除的节点有右子节点
                    if (parent != null) {
                        if (parent.left.val == val) {//targetNode是parent的左子节点
                            parent.left = targetNode.right;
                        } else {//targetNode是parent的右子节点
                            parent.right = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }
            }
        }
    }
}

class Node {
    int val;
    Node left;
    Node right;

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

    /**
     * 查找需要删除的节点
     *
     * @param value 需要删除的节点
     * @return 找到返回此节点,否则返回null
     */
    public Node search(int value) {
        if (value == this.val) {//要查找的值等于当前节点的值
            return this;
        } else if (value < this.val) {//查找的值小于当前节点的值
            if (this.left == null) {//左子节点为空,说明没有该节点
                return null;
            } else {//向左递归查找
                return this.left.search(value);
            }
        } else {//查找的值大于等于当前节点的值
            if (this.right == null) {//右子节点为空,说明没有该值
                return null;
            } else {//向右递归
                return this.right.search(value);
            }
        }
    }

    /**
     * 查找需要删除节点的父节点
     *
     * @param val 要找到的节点值
     * @return 返回父节点
     */
    public Node searchParent(int val) {
        if ((this.left != null && this.left.val == val) ||
                (this.right != null && this.right.val == val)) {
            return this;
        } else {
            //如果查找的值小于当前节点的值且当前节点的左子节点不为空
            if (val < this.val && this.left != null) {
                return this.left.searchParent(val);
            } else if (val >= this.val && this.right != null) {
                return this.right.searchParent(val);
            } else {//没有父节点
                return null;
            }
        }
    }
}

平衡二叉树(AVL树)

一、概述

举例:一个数列{ 1, 2, 3, 4, 5, 6 },创建的二叉排序树为:

在这里插入图片描述

基本介绍

  1. 平衡二叉树也叫平衡二叉搜索树,又称为AVL树
  2. 特点:它是一棵空树或它的左右两个子树的高度差的绝对值不能超过1,并且左右两棵子树都是一棵平衡二叉树。平衡二叉树的常用实现方法有红黑树、AVL、替罪羊树、Treap、伸展树

二、左旋转

1. 步骤

  • 创建一个新的节点newNode,让此节点的值等于根节点的值
  • 新节点newNode的左子树设置为当前节点的左子树
  • 新节点的右子树设置为当前节点的右子树左子树
  • 把当前节点的值换为右子节点的值
  • 把当前节点的右子树设置为右子树的右子树
  • 把当前节点的左子树设置为新节点

在这里插入图片描述

2. 代码演示

class Node{	
    /**
     * @return 返回左子树高度
     */
    public int leftHeight() {
        if (left == null) {
            return 0;
        } else {
            return left.height();
        }
    }

    /**
     * @return 返回右子树高度
     */
    public int rightHeight() {
        if (right == null) {
            return 0;
        } else {
            return right.height();
        }
    }

    /**
     * @return 返回以该节点为根节点的树的高度
     */
    public int height() {
        return Math.max(left == null ? 0 : left.height(),
                right == null ? 0 : right.height()) + 1;
    }
	/**
     * 左旋转
     */
    private void leftRotate(){
        //创建新的节点
        Node newNode = new Node(this.val);
        //新节点的左子树设置为当前节点的左子树
        newNode.left=this.left;
        //右子树设置为当前节点右子树的左子树
        newNode.right=this.right.left;
        //当前节点的值换为右子节点的值
        this.val=this.right.val;
        //当前节点的右子树设置为当前节点右子树的右子树
        this.right=this.right.right;
        //当前节点的左子节点设置为新的节点
        this.left=newNode;
    }
}

三、右旋转

1. 步骤

  • 创建一个新的节点newNode,值等于根节点的值
  • 新节点的右子树设置为当前节点的右子树
  • 新节点的左子树设置为当前节点左子树的右子树
  • 前节点的值换为左子节点的值
  • 当前节点的左子树设置为左子树的左子树
  • 当前节点的右子树设置为newNode
    在这里插入图片描述

2. 代码演示

Node{    
    public void rightRotate(){
        Node newNode = new Node(this.val);
        newNode.right=this.right;
        newNode.left=this.left.right;
        this.val=this.left.val;
        this.left=this.left.left;
        this.right=newNode;
    }
}

四、双旋转

		//当添加完节点后,如果(左子树的高度-右子树的高度)>1,左旋转
        if(rightHeight()-leftHeight()>1){
            //左旋转
            //它的右子树的左子树高度大于右子树高度
            if(this.right !=null&&this.right.leftHeight()>this.right.rightHeight()){
                //对当前节点的右子节点进行右旋转
                this.right.rightHeight();
            }
            //统一进行左旋转
            this.leftRotate();
            return;
        }
        if(leftHeight()-rightHeight()>1){
            //右旋转
            //它的左子树的右子树高度大于它的左子树高度
            if(this.left!=null&&this.left.rightHeight()>this.left.leftHeight()){
                //对当前节点的左节点进行左旋转
                this.left.leftRotate();
            }
            //最后进行右旋转
            this.rightRotate();
        }
发布了55 篇原创文章 · 获赞 20 · 访问量 3941

猜你喜欢

转载自blog.csdn.net/qq_40613029/article/details/104315491