二叉树相关知识总结

二叉树

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package com.shizhong.tree;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        //先创建二叉树
        BinaryTree binaryTree = new BinaryTree();
        HeroNode root = new HeroNode(1,"宋");
        HeroNode node2 = new HeroNode(2,"吴");
        HeroNode node3 = new HeroNode(3,"卢");
        HeroNode node4 = new HeroNode(4,"林");
        HeroNode node5 = new HeroNode(5,"关");
        root.setLeft(node2);
        root.setRight(node3);
        node3.setLeft(node5);
        node3.setRight(node4);
        binaryTree.setRoot(root);
        System.out.println("前序遍历");//12354
        binaryTree.preOrder();
        System.out.println("中序遍历");//21534
        binaryTree.infixOrder();
        System.out.println("后序遍历");//25431
        binaryTree.postOrder();

        //前序遍历查找节点
        HeroNode resNode = binaryTree.preOrderSearch(5);
        if (resNode != null) {
            System.out.printf("找到了,信息为no=%d, name = %s\n", resNode.getNo(), resNode.getName());
        }else {
            System.out.printf("没有找到no= %d",5);
        }
        //中序遍历查找
        HeroNode resNode1 = binaryTree.infixOrderSearch(5);
        if (resNode1 != null) {
            System.out.printf("找到了,信息为no=%d, name = %s\n", resNode1.getNo(), resNode1.getName());
        }else {
            System.out.printf("没有找到no= %d",5);
        }
        //后序遍历查找
        HeroNode resNode2 = binaryTree.postOrderSearch(5);
        if (resNode2 != null) {
            System.out.printf("找到了,信息为no=%d, name = %s\n", resNode2.getNo(), resNode2.getName());
        }else {
            System.out.printf("没有找到no= %d",5);
        }
        //删除节点
        System.out.println("=======================");
        System.out.println("删除前");
        binaryTree.preOrder();
        binaryTree.delNode(5);
        System.out.println("删除后");
        binaryTree.preOrder();
    }
}
//定义Binary Tree 二叉树
class BinaryTree{
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }
    //删除节点
    public void delNode(int no) {
        if (root != null) {
            if(root.getNo() == no) {
                root = null;
            }else {
                root.delNode(no);
            }
        }else {
            System.out.println("树为空,不能删除");
        }
    }
    //二叉树前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        }else {
            System.out.println("二叉树为空,无法遍历");
        }
    }
    //二叉树中序遍历
    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        }else {
            System.out.println("二叉树为空,无法遍历");
        }
    }
    //二叉树后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        }else {
            System.out.println("二叉树为空,无法遍历");
        }
    }
    //二叉树前序遍历查找节点
    public HeroNode preOrderSearch(int no) {
        if (root != null) {
            return root.preOrderSearch(no);
        }else {
            return null;
        }
     }
     //二叉树中序遍历查找节点
    public HeroNode infixOrderSearch(int no) {
        if (root != null) {
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    //二叉树后序遍历查找节点
    public HeroNode postOrderSearch(int no) {
        if (root != null) {
            return root.postOrderSearch(no);
        }else {
            return null;
        }
    }
}


class HeroNode {
    private int no;
    private String name;
    private HeroNode left;//默认为null,不需要初始化
    private HeroNode right;//默认为null, 不需要初始化
    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
    //递归删除节点,这里注意两点:1, 如果要删除的节点是叶子节点,就直接删除该节点。 2,如果要删除的节点是非叶子节点,则删除该子树,这里不考虑要删除节点的子节点的补位
    public void delNode(int no) {
        if (this.left != null && this.left.no == no) {//如果当前左子节点不为空,并且左子节点就是要删除的节点,则返回null
            this.left = null;
            return ;
        }
        if (this.right != null && this.right.no == no) {//如果当前节点的右子节点不为空,并且右子节点就是要删除的节点,则返回null
            this.right = null;
            return ;
        }
        if (this.left != null) {//否则,左递归查找
            this.left.delNode(no);
        }
        if (this.right != null) {//否则,右递归进行查找
            this.right.delNode(no);
        }
    }




    //创建该节点前序遍历的方法
    //先输出当前父节点的,然后判断如果左子节点不为空,前序遍历左子节点, 在判断右子节点不为空,前序遍历右子节点
    public void preOrder() {
        System.out.println(this);//先输出父节点
        if (this.left != null){//递归向左子树前序遍历
            this.left.preOrder();
        }
        if (this.right != null) {//递归向右子树前序遍历
            this.right.preOrder();
        }
     }
     //创建该节点的中序遍历的方法
    public void infixOrder() {
        if (this.left != null) {//先递归向左子树中序遍历
            this.left.infixOrder();
        }
        System.out.println(this);//输出当前节点的值
        if (this.right != null) {//递归向右子树中序遍历
            this.right.infixOrder();
        }
    }
    public void postOrder(){
        //先递归向左子树递归后序遍历
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }
    //前序遍历查找节点

    /**
     *
     * @param no 要查找的节点编号
     * @return
     */
    public HeroNode preOrderSearch(int no) {
        if (this.no == no) {//判断当前节点的编号是否等于要查找的节点编号
            return this;
        }
        HeroNode resNode = null;//
        if (this.left != null) {//判断当前节点的左子节点是否为空,如果不为空,左递归遍历
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {//如果左递归找到节点,则返回节点,否通则进行右递归查找
            return resNode;
        }
        if (this.right != null) {//如果右子节点不为空,则进行右递归进行查找
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;//最终返回节点
    }
    //中序遍历查找节点

    /**
     *
     * @param no 要查找的节点编号
     * @return
     */
    public HeroNode infixOrderSearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {//判断当前节点的左子节点是否为空,如果不为空,则进行左递归判断
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null) {//判断左递归是否找到,如果找到,则返回节点,否则判断当前节点
            return resNode;
        }
        if (this.no == no) {//如果当前节点编号和要查找的节点编号相等,则返回该节点,否则进行右递归查找
            return this;
        }
        if (this.right != null) {//如果当前节点的右子节点不为空,则进行右递归查找
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }
    //后序遍历查找
    public HeroNode postOrderSearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {//判断当前节点左子节点是否为空,如果不为空,则递归进行查找
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null) {//如果左递归找到,则返回,
            return resNode;
        }
        if (this.right != null) {//如果左递归没有找到,则判断当前节点右子节点是否为空,如果不为空,则右递归进行查找
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null) {//如果右递归找到,则返回节点
            return resNode;
        }
        if (this.no == no) {//如果左右递归都没有查找到,则判断当前节点是否是要查找的节点,如果是,则返回
            return this;
        }
        return resNode;//否则返回定义的节点
    }


}

顺序存储二叉树

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

package com.shizhong.tree;

import java.lang.reflect.Array;
import java.util.Arrays;

public class ArrayBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
        arrBinaryTree.preOrder();
        System.out.println();
        arrBinaryTree.infixOrder(0);//4251637
        System.out.println();
        arrBinaryTree.postOrder(0);//4526731

    }

//堆排序是顺序存储二叉树的实际应用
}
class ArrBinaryTree{
    private int[] arr;//存储数据节点的二叉树
    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }
    //重载preOrder()方法
    public void preOrder(){
        this.preOrder(0);
    }
    //编写一个方法,完成顺序存储二叉树的前序遍历
    /*
    顺序存储二叉树的特点:n表示二叉树中的第几个元素,下标从0开始
    1、顺序存储二叉树通常值考虑完全二叉树
    2、第n个元素的左字节点为2*n + 1
    3、第n个元素的右子节点为2*n + 2
    4、第n个元素的父节点为(n-1) / 2
     */
    /**
     *
     * @param index 数组索引
     */
    public void preOrder(int index){
        if (arr == null || arr.length == 0) {
            System.out.println("数组为空,不能实现顺序存储二叉树的遍历");
        }
        System.out.print(arr[index]);//前序遍历,先输出当前节点
        if ((2 * index + 1) < arr.length) {//左递归遍历,因为第n个元素的左子节点为2 * n + 1
            preOrder(2 * index + 1);
        }
        if ((2 * index + 2) < arr.length) {//右递归遍历,第n个元素的右子节点为 2 * n + 2
            preOrder(2 * index + 2);
        }
    }
    //顺序存储二叉树的中序遍历
    public void infixOrder(int index) {
        if (arr == null || arr.length == 0) {
            System.out.println("数组为空,无法实现顺序存储二叉树的中序遍历");
        }
        if ((2 * index + 1) < arr.length) {
            infixOrder(2 * index + 1);
        }
        System.out.print(arr[index]);
        if ((2 * index + 2) < arr.length) {
            infixOrder((2 * index + 2));
        }
    }
    //顺序存储二叉树的后序遍历
    public void postOrder(int index) {
        if (arr == null || arr.length == 0)  {
            System.out.println("数组为空,无法实现顺序存储二叉树的后序遍历");
        }
        if ((2 * index + 1) < arr.length) {
            postOrder(2 * index + 1);
        }
        if ((2 * index + 2) < arr.length) {
            postOrder(2 * index + 2);
        }
        System.out.print(arr[index]);
    }

}

线索化二叉树

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

package com.shizhong.tree.threadbinarytreee;

public class ThreadedBinaryTreeDemo {
    public static void main(String[] args) {
        HeroNode root = new HeroNode(1, "hell");
        HeroNode node2 = new HeroNode(3, "keh");
        HeroNode node3 = new HeroNode(6, "luioe");
        HeroNode node4 = new HeroNode(8, "wiejee");
        HeroNode node5 = new HeroNode(10, "qqjhgl");
        HeroNode node6 = new HeroNode(14, "czzljl");
        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);
        ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree();
        threadedBinaryTree.setRoot(root);
        threadedBinaryTree.threadedNodes();
        HeroNode nodeLeft = node5.getLeft();
        HeroNode nodeRight = node5.getRight();
        System.out.println("十号节点的前驱节点为"+nodeLeft);
        System.out.println("十号节点的后继节点=" + nodeRight);
        threadedBinaryTree.threadedList();
    }

}

//创建线索化二叉树threadedBinaryTree
class ThreadedBinaryTree{
    private HeroNode root;
    private HeroNode pre; //为了实现线索化,需要创建指向当前节点的前驱节点的指针,在递归线索化时,pre总是保留前一个节点

    public void setRoot(HeroNode root) {
        this.root = root;
    }
    public void threadedNodes(){
        this.threadedNodes(root);
    }
    //编写二叉树,进行中序线索化二叉树的操作

    /**
     *
     * @param node 就是当前需要线索化的二叉树节点
     */
    public void threadedNodes(HeroNode node){
        if(node == null) {//如果node为null,则不能进行线索化
            return ;
        }
        //中序遍历:(一) 先线索化左子树(二)在线索化当前节点(三)最后线索化右子树
        threadedNodes(node.getLeft());
        if (node.getLeft() == null) {//如果当前节点的左子节点为空,让当前节点的左指针指向前驱节点,
            // 同时让左指针的类型变为1
            node.setLeft(pre);
            node.setLeftType(1);
        }
        if (pre != null && pre.getRight() == null) {//如果前驱节点为null,则让前驱节点右指针指向后继节点,
            // 即就是当前的node节点,同时让pre的右指针类型变为1;
            pre.setRight(node);
            pre.setRightType(1);
        }
        //每处理一个节点,让当前节点是下一个节点的前去节点
        pre = node;

        threadedNodes(node.getRight());

    }
    //遍历中序线索化二叉树的方法
    public void threadedList(){
        HeroNode node = root;//定义一个变量,存储当前的节点
        while (node != null){
            while (node.getLeftType() == 0) {//循环找到leftType=1的节点,因为其实线索化二叉树的开始节点
                node = node.getLeft();
            }
            System.out.println(node);
            while (node.getRightType() == 1) {//如果当前节点的右指针指向的是后继节点,则一直输出
                node = node.getRight();
                System.out.println(node);
            }
            node = node.getRight();//替换遍历节点
        }
    }
    //删除节点
    public void delNode(int no) {
        if (root != null) {
            if(root.getNo() == no) {
                root = null;
            }else {
                root.delNode(no);
            }
        }else {
            System.out.println("树为空,不能删除");
        }
    }
    //二叉树前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        }else {
            System.out.println("二叉树为空,无法遍历");
        }
    }
    //二叉树中序遍历
    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        }else {
            System.out.println("二叉树为空,无法遍历");
        }
    }
    //二叉树后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        }else {
            System.out.println("二叉树为空,无法遍历");
        }
    }
    //二叉树前序遍历查找节点
    public HeroNode preOrderSearch(int no) {
        if (root != null) {
            return root.preOrderSearch(no);
        }else {
            return null;
        }
     }
     //二叉树中序遍历查找节点
    public HeroNode infixOrderSearch(int no) {
        if (root != null) {
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    //二叉树后序遍历查找节点
    public HeroNode postOrderSearch(int no) {
        if (root != null) {
            return root.postOrderSearch(no);
        }else {
            return null;
        }
    }
}

class HeroNode {
    private int no;
    private String name;
    private HeroNode left;//默认为null,不需要初始化
    private HeroNode right;//默认为null, 不需要初始化
    //如果leftType = 0,表示的是左子树,如果是1,则表示的是前驱节点
    //如果rightType = 0, 表示的是右子树,如果是1, 则表示的是后继节点
    private int leftType;
    private int rightType;
    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
    //递归删除节点,这里注意两点:1, 如果要删除的节点是叶子节点,就直接删除该节点。 2,如果要删除的节点是非叶子节点,则删除该子树,这里不考虑要删除节点的子节点的补位
    public void delNode(int no) {
        if (this.left != null && this.left.no == no) {//如果当前左子节点不为空,并且左子节点就是要删除的节点,则返回null
            this.left = null;
            return ;
        }
        if (this.right != null && this.right.no == no) {//如果当前节点的右子节点不为空,并且右子节点就是要删除的节点,则返回null
            this.right = null;
            return ;
        }
        if (this.left != null) {//否则,左递归查找
            this.left.delNode(no);
        }
        if (this.right != null) {//否则,右递归进行查找
            this.right.delNode(no);
        }
    }




    //创建该节点前序遍历的方法
    //先输出当前父节点的,然后判断如果左子节点不为空,前序遍历左子节点, 在判断右子节点不为空,前序遍历右子节点
    public void preOrder() {
        System.out.println(this);//先输出父节点
        if (this.left != null){//递归向左子树前序遍历
            this.left.preOrder();
        }
        if (this.right != null) {//递归向右子树前序遍历
            this.right.preOrder();
        }
     }
     //创建该节点的中序遍历的方法
    public void infixOrder() {
        if (this.left != null) {//先递归向左子树中序遍历
            this.left.infixOrder();
        }
        System.out.println(this);//输出当前节点的值
        if (this.right != null) {//递归向右子树中序遍历
            this.right.infixOrder();
        }
    }
    public void postOrder(){
        //先递归向左子树递归后序遍历
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }
    //前序遍历查找节点

    /**
     *
     * @param no 要查找的节点编号
     * @return
     */
    public HeroNode preOrderSearch(int no) {
        if (this.no == no) {//判断当前节点的编号是否等于要查找的节点编号
            return this;
        }
        HeroNode resNode = null;//
        if (this.left != null) {//判断当前节点的左子节点是否为空,如果不为空,左递归遍历
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {//如果左递归找到节点,则返回节点,否通则进行右递归查找
            return resNode;
        }
        if (this.right != null) {//如果右子节点不为空,则进行右递归进行查找
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;//最终返回节点
    }
    //中序遍历查找节点

    /**
     *
     * @param no 要查找的节点编号
     * @return
     */
    public HeroNode infixOrderSearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {//判断当前节点的左子节点是否为空,如果不为空,则进行左递归判断
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null) {//判断左递归是否找到,如果找到,则返回节点,否则判断当前节点
            return resNode;
        }
        if (this.no == no) {//如果当前节点编号和要查找的节点编号相等,则返回该节点,否则进行右递归查找
            return this;
        }
        if (this.right != null) {//如果当前节点的右子节点不为空,则进行右递归查找
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }
    //后序遍历查找
    public HeroNode postOrderSearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {//判断当前节点左子节点是否为空,如果不为空,则递归进行查找
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null) {//如果左递归找到,则返回,
            return resNode;
        }
        if (this.right != null) {//如果左递归没有找到,则判断当前节点右子节点是否为空,如果不为空,则右递归进行查找
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null) {//如果右递归找到,则返回节点
            return resNode;
        }
        if (this.no == no) {//如果左右递归都没有查找到,则判断当前节点是否是要查找的节点,如果是,则返回
            return this;
        }
        return resNode;//否则返回定义的节点
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值