树与二叉树

二叉排序树(BST树)

基本介绍

对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大

创建节点

class Node {
    int value;
    Node left;
    Node right;
    public Node(int value) {
        this.value = value;
    }
    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
    //查找节点
    public Node search(int value) {
        if (value == this.value) {
            return this;
        } else 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);
        }
    }
    //查找某节点的父节点
    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);
            }
        }
    }
    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }
}

创建二叉排序树

class BinarySortTree {
    private Node root;
    public Node getRoot() {
        return root;
    }
    //查找节点
    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 int delEightTreeMin(Node node) {
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }

    //删除节点
    public void delNode(int value) {
        if (root == null) {
            return;
        } else {
            Node targetNode = search(value);
            if (targetNode == null) {
                return;
            }
            if (root.left == null && root.right == null) {
                root = null;
                return;
            }
            Node parent = searchParent(value);
            //如果要删除的节点是叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) {
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) {
                //删除有两棵子树的节点
                int minVal = delEightTreeMin(targetNode.right);
                targetNode.value = minVal;
            } else {//删除只有一棵子树的节点
                //如果要删除的节点有左子节点
                if (targetNode.left != null) {
                    if (parent != null) {
                        //如果tartgetNode是parent的左子节点
                        if (parent.left.value == value) {
                            parent.left = targetNode.left;
                        } else {
                            parent.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }
                } else {//如果要删除的节点是右子节点
                        if (parent != null) {
                            //如果tartgetNode是parent的左子节点
                            if (parent.left.value == value) {
                                parent.left = targetNode.right;
                            } else {
                                parent.right = targetNode.right;
                            }
                        } else {
                            root = targetNode.right;
                        }
                }
            }
        }
    }

    //添加节点的方法
    public void add(Node node) {
        if (root == null) {
            //如果root为空 则直接让root指向node
            root = node;
        } else {
            root.add(node);
        }
    }
    //中序遍历
    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序树为空 不能遍历");
        }
    }
}

测试

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        //画图构建好这棵二叉排序树
        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
        BinarySortTree binarySortTree = new BinarySortTree();

        //创建
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }
        //中序遍历二叉排序树
        System.out.println("中序遍历二叉排序树");
        binarySortTree.infixOrder();
        
        Node search = binarySortTree.search(9);
    }
}

测试代码

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        //画图构建好这棵二叉排序树
        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
        BinarySortTree binarySortTree = new BinarySortTree();

        //创建
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }
        //中序遍历二叉排序树 验证创建成功
//        System.out.println("中序遍历二叉排序树");
//        binarySortTree.infixOrder();

        //查找 打印查找路径
        System.out.println("查找路径:");
        Node search = binarySortTree.search(9);
        System.out.println("查找成功:");
        System.out.println(search);

        //删除叶子节点
//        binarySortTree.delNode(2);
//        //验证删除是否成功
//        System.out.println("中序遍历二叉排序树");
//        binarySortTree.infixOrder();

        //删除的节点有一棵子树
//        binarySortTree.delNode(1);
//        //验证删除是否成功
//        System.out.println("中序遍历二叉排序树");
//        binarySortTree.infixOrder();

        //删除的节点有两棵子树
//        binarySortTree.delNode(10);
//        //验证删除是否成功
//        System.out.println("中序遍历二叉排序树");
//        binarySortTree.infixOrder();
    }
}

部分截图(查找某节点)
在这里插入图片描述

在这里插入图片描述

完整代码

package com.cpw.binary_sort_tree;

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        //画图构建好这棵二叉排序树
        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
        BinarySortTree binarySortTree = new BinarySortTree();

        //创建
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }
        //中序遍历二叉排序树 验证创建成功
//        System.out.println("中序遍历二叉排序树");
//        binarySortTree.infixOrder();

        //查找 打印查找路径
        System.out.println("查找路径:");
        Node search = binarySortTree.search(9);
        System.out.println("查找成功:");
        System.out.println(search);

        //删除叶子节点
//        binarySortTree.delNode(2);
//        //验证删除是否成功
//        System.out.println("中序遍历二叉排序树");
//        binarySortTree.infixOrder();

        //删除的节点有一棵子树
//        binarySortTree.delNode(1);
//        //验证删除是否成功
//        System.out.println("中序遍历二叉排序树");
//        binarySortTree.infixOrder();

        //删除的节点有两棵子树
//        binarySortTree.delNode(10);
//        //验证删除是否成功
//        System.out.println("中序遍历二叉排序树");
//        binarySortTree.infixOrder();



    }
}



class BinarySortTree {
    private Node root;
    public Node getRoot() {
        return root;
    }
    //查找节点
    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 int delEightTreeMin(Node node) {
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }

    //删除节点
    public void delNode(int value) {
        if (root == null) {
            return;
        } else {
            Node targetNode = search(value);
            if (targetNode == null) {
                return;
            }
            if (root.left == null && root.right == null) {
                root = null;
                return;
            }
            Node parent = searchParent(value);
            //如果要删除的节点是叶子节点
            if (targetNode.left == null && targetNode.right == null) {
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) {
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) {
                //删除有两棵子树的节点
                int minVal = delEightTreeMin(targetNode.right);
                targetNode.value = minVal;
            } else {//删除只有一棵子树的节点
                //如果要删除的节点有左子节点
                if (targetNode.left != null) {
                    if (parent != null) {
                        //如果tartgetNode是parent的左子节点
                        if (parent.left.value == value) {
                            parent.left = targetNode.left;
                        } else {
                            parent.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }
                } else {//如果要删除的节点是右子节点
                        if (parent != null) {
                            //如果tartgetNode是parent的左子节点
                            if (parent.left.value == value) {
                                parent.left = targetNode.right;
                            } else {
                                parent.right = targetNode.right;
                            }
                        } else {
                            root = targetNode.right;
                        }
                }
            }
        }
    }

    //添加节点的方法
    public void add(Node node) {
        if (root == null) {
            //如果root为空 则直接让root指向node
            root = node;
        } else {
            root.add(node);
        }
    }
    //中序遍历
    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序树为空 不能遍历");
        }
    }
}

class Node {
    int value;
    Node left;
    Node right;
    public Node(int value) {
        this.value = value;
    }
    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
    //查找节点
    public Node search(int value) {
        System.out.println(this);
        if (value == this.value) {
//            System.out.println(this);
            return this;
        } else 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);
        }
    }
    //查找某节点的父节点
    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);
            }
        }
    }
    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }
}

平衡二叉树(AVL树)

特点:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1.并且左右两个子树都是一棵平衡二叉树。

左旋代码

private void leftRotate() {
	//创建新的结点 以当前根节点的值
	Node newNode = new Node(value);
	//把新的结点的左子树设置成当前节点的左子树 
	newNode.left = left;
	//把新的节点的右子树设置成当前节点的右子树的左子树
	newNode.right = right.left;
	把当前节点的值替换成右子节点的值
	value = right.value;
	//把当前节点的右子树设置成当前节点右子树的右子树
	right = right.right;
	//把当前节点的左子树(左子节点)设置成新的节点
	left = newNode; 
}

右旋代码

	Node newNode = new Node(value);
	//把新节点的右子树设置成当前节点的右子树
	newNode.right = right;
	//把新节点的左子树设置成当前节点的左子树的右子树
	newNode.left = left.right;
	//把当前节点的值换成左子节点的值
	value = left.value;
	//把当前节点的左子树设置成左子树的左子树
	left = left.left;
	//把当前节点的右子树设置成新节点
	right = newNode

红黑树

是一个自平衡(不是绝对的平衡的二叉查找树),树上的每个节点都遵循下面的规则:
1.每个节点要么是黑色,要么是红色
2.根节点是黑色
3.每个叶子节点(NULL)是黑色
4.每个红色节点的两个子节点一定都是黑色
5.任意一节点到每个叶子节点的路径都包含数量相同的黑节点。

红黑树能自平衡,靠的是三种操作:左旋,右旋 变色。
左旋:以某个节点作为支点(旋转节点),其右子节点变为旋转节点的父节点,右子节点的左子节点变为旋转节点的右子节点,左子节点保持不变。
右旋:以某个节点作为支点(旋转节点),其左子节点变为旋转节点的父节点,左子节点的右子节点变为旋转节点的左子节点,右子节点保持不变。
变色:节点的颜色由红变黑或者由黑变红。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值