10.1.4 二叉树遍历的说明

使用前序,中序和后序对下面的二叉树进行遍历。

  1. 前序遍历: 先输出父节点,再遍历左子树和右子树
  2. 中序遍历: 先遍历左子树,再输出父节点,再遍历右子树
  3. 后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点
  4. 小结: 看输出父节点的顺序,就确定是前序,中序还是后序

10.1.5 二叉树遍历应用实例(前序,中序,后序)

应用实例的说明和思路

java 嵌C java 前叉_java 嵌C

代码实现(Java实现二叉树的前序、中序和后序的遍历、查找、删除代码在最下方一块展示)

10.1.6 二叉树-查找指定节点

要求

  1. 请编写前序查找,中序查找和后序查找的方法。
  2. 并分别使用三种查找方式,查找 heroNO = 5 的节点
  3. 并分析各种查找方式,分别比较了多少次
  4. 思路分析图解

java 嵌C java 前叉_java 嵌C_02

  1. 代码实现(Java实现二叉树的前序、中序和后序的遍历、查找、删除代码在最下方一块展示)

10.1.7 二叉树-删除节点

要求

  1. 如果删除的节点是叶子节点,则删除该节点
  2. 如果删除的节点是非叶子节点,则删除该子树
  3. 测试,删除掉 5 号叶子节点 和 3 号子树
  4. 完成删除思路分析(注意:其实步骤1和2 是错误的,删除非叶子节点不能删除整个字数,但是这里先简化处理了,删除方法比较复杂,删除方法在二叉树排序的时候展示,想要看的话请在文章中找第11章节的代码)
  5. java 嵌C java 前叉_数据结构_03

  6. 代码实现(Java实现二叉树的前序、中序和后序的遍历、查找、删除代码在最下方一块展示)
/**
 * @author zk
 * @version 1.0.0
 * @ClassName BinaryTreeDemo.java
 * @Description TODO 二叉树遍历和查找应用实例(前序,中序,后序)
 * @createTime 2021年09月25日 15:15:00
 */
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.setRight(node4);
        node3.setLeft(node5);
        binaryTree.setRoot(root);
        binaryTree.preOrder();
        System.out.println("----------");
        binaryTree.inOrder();
        System.out.println("----------");
        binaryTree.postOrder();
        System.out.println("----------------------");
        System.out.println("前序、中序、后序遍历查找");
        HeroNode heroNode1 = binaryTree.preOrderSearch(5);
        System.out.println("前序查找结果:"+heroNode1); //比对次数4次
        HeroNode heroNode2 = binaryTree.inOrderSearch(5);
        System.out.println("中序查找结果:"+heroNode2); //比对次数3次
        HeroNode heroNode3 = binaryTree.postOrderSearch(5);
        System.out.println("后序查找结果:"+heroNode3); //比对次数2次
        /*System.out.println("----------删除5号节点------------");
        binaryTree.delNode(5);
        binaryTree.preOrder();*/
        /*System.out.println("----------删除3号节点------------");
        binaryTree.delNode(3);
        binaryTree.preOrder();*/



    }
}

class BinaryTree {
    private HeroNode root;

    public HeroNode getRoot() {
        return root;
    }

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    // 前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("root is null");
        }
    }

    // 中序遍历
    public void inOrder() {
        if (this.root != null) {
            this.root.inOrder();
        } else {
            System.out.println("root is null");
        }
    }

    // 后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("root is null");
        }
    }

    // 前序遍历查找
    public HeroNode preOrderSearch(int id){
        if (this.root != null){
            return this.root.preOrderSearch(id);
        }else {
            return null;
        }
    }

    // 中序遍历查找
    public HeroNode inOrderSearch(int id){
        if (this.root != null){
            return this.root.inOrderSearch(id);
        }else {
            return null;
        }
    }

    // 后序遍历查找
    public HeroNode postOrderSearch(int id){
        if (this.root != null){
            return this.root.postOrderSearch(id);
        }else {
            return null;
        }
    }

    // 根据id删除节点
    // this.root = null;是本方法的缺陷,删除一个节点不应该把整个树清空
    public void delNode(int id){
        if (this.root !=null){
            if (this.root.getId() == id){
                this.root = null;
            }else {
                this.root.delNode(id);
            }
        }else {
            System.out.println("root is null");
        }

    }

}

class HeroNode {
    private int id;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    // 前序遍历(preOrder traversal)
    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

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

    // 后序遍历(postOrder traversal)
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

    // 前序遍历查找
    public HeroNode preOrderSearch(int id) {
        System.out.println("进入前序遍历查找进行比对");
        if (this.id == id) {
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(id);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preOrderSearch(id);
        }
        return resNode;
    }

    // 中序遍历查找
    public HeroNode inOrderSearch(int id) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.inOrderSearch(id);
        }
        if (resNode != null) {
            return resNode;
        }
        System.out.println("进入中序遍历查找进行比对");
        if (this.id == id) {
            return this;
        }
        if (this.right != null) {
            resNode = this.right.inOrderSearch(id);
        }
        return resNode;
    }

    // 后序遍历查找
    public HeroNode postOrderSearch(int id) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.postOrderSearch(id);
        }
        if (resNode != null) {
            return resNode;
        }

        if (this.right != null) {
            resNode = this.right.postOrderSearch(id);
        }
        if (resNode !=null){
            return resNode;
        }
        System.out.println("进入后序遍历查找进行比对");
        if (this.id == id) {
            return this;
        }
        return null;
    }

    // 递归删除节点
    /**
     * 1) 如果删除的节点是叶子节点,则删除该节点
     * 2) 如果删除的节点是非叶子节点,则删除该子树.(本方法的缺陷,不应该删除子树)
     * 3)测试,删除掉 5 号叶子节点 和 3 号子树.
     * 4) 完成删除思路分
     **/

    // 思路
    /**
     * 1. 因为我们的二叉树是单向的,所以我们是判断当前结点的子结点是否需要删除结点,而不能去判断当前这个结点是不是需要删除结点.
     * 2. 如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将 this.left = null; 并且就返回(结束递归删除)
     * 3. 如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将 this.right= null ;并且就返回(结束递归删除)
     * 4. 如果第 2 和第 3 步没有删除结点,那么我们就需要向左子树进行递归删除
     * 5. 如果第 4 步也没有删除结点,则应当向右子树进行递归删除.
     **/
    public void delNode(int id){
        // 1.如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将 this.left = null; 并且就返回(结束递归删除)
        if (this.left != null&& this.left.getId() == id){
            this.left = null;
            return;
        }
        // 2.如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将 this.right= null ;并且就返回(结束递归删除)
        if (this.right != null&& this.right.getId() == id){
            this.right = null;
            return;
        }
        // 3.如果第 2 和第 3 步没有删除结点,那么我们就需要向左子树进行递归删除
        if (this.left != null){
            this.left.delNode(id);
        }
        // 4.如果第 4 步也没有删除结点,则应当向右子树进行递归删除.(正规点的话 要判断第三步是否删除成功,然后在确定下面是否执行)
        if (this.right != null){
            this.right.delNode(id);
        }

    }
}