LEIZHANG ' s GitHub

数据结构与算法-二叉树

二叉树

每个结点最多只有两个子结点的树称为二叉树,二叉树的子结点分为左结点和右结点

二叉树的所有叶子结点都在最后一层,且结点 = 2 ^ n - 1,则称之为满二叉树(n)为层数

二叉树的所有叶子结点都在最后一层或者倒数第二层,且最后一层的叶子结点左连续,倒数第二层叶子结点右连续,则称之为完全二叉树

1. 二叉树的遍历

1.1 思路分析

  • 前序遍历:父结点--左子树--右子树
    • 先输出当前结点(初始为根结点)
    • 若左子结点不为空,则递归进行前序遍历
    • 若右子结点不为空,则递归继续前序遍历
  • 中序遍历:左子树--父结点--右子树
    • 若当前结点的左子结点不为空,则递归中序遍历
    • 输出当前结点
    • 若当前结点的右子结点不为空,则递归中序遍历
  • 后序遍历:左子树--右子树--父结点
    • 若当前结点的左子结点不为空,则递归后序遍历
    • 若当前结点的右子结点不为空,则递归后序遍历
    • 输出当前结点

看输出父结点的顺序,确定 前中后

!!可以先将 父结点排好,再按顺序填入孩子

1.2 代码实现

package cn.imut;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        //创建二叉树
        BinaryTree binaryTree = new BinaryTree();

        //创建结点
        HeroNode node = new HeroNode(1, "张磊");
        HeroNode node1 = new HeroNode(2, "王雁明");
        HeroNode node2 = new HeroNode(3, "纪烁");
        HeroNode node3 = new HeroNode(4, "袁国伟");
        HeroNode node4 = new HeroNode(5, "王彬");
        HeroNode node5 = new HeroNode(6, "苏志辉");

        //先手动创建二叉树
        node.setLeft(node1);        //1的左孩子是2
        node.setRight(node2);       //1的右孩子是3
        node1.setLeft(node3);
        node1.setRight(node4);
        node2.setLeft(node5);
        binaryTree.setRoot(node);   //根结点

        //测试
        System.out.println("前序");
        binaryTree.preOrder();
        System.out.println("中序");
        binaryTree.infixOrder();
        System.out.println("后序");
        binaryTree.postOrder();
    }
}

//定义结点
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 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);
    }
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", 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;
    }

}
//定义二叉树
class BinaryTree {
    private HeroNode root;      //定义根结点

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

    //前序遍历
    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("二叉树为空");
        }
    }
}

2. 查找指定结点

2.1 思路分析

  • 前序查找
    • 先判断当前结点的no是否等于要查找的
    • 如果是相等,则返回当前结点
    • 如果不相等,则判断当前结点的左子树是否为空,若不为空,则前序查找
    • 若左递归查找,找到结点,则返回,否则继续判断当前结点的右结点是否为空,不为空便右递归查找
  • 中序查找
    • 判断当前结点的右子结点是否为空,若不为空,则递归中序查找
    • 找到便返回i,否则和当前结点比较,是则返回,不是继续右递归
    • 若右递归还没找到,返回null,找到则返回结点
  • 后序查找
    • 判断当前结点左子结点是否为空,若不为空,则递归后序查找
    • 若找到,则返回。否则判断当前结点的右子结点是否为空,不为空则右递归后序查找,找到则返回
    • 没找到返回null

2.2 代码实现

package cn.imut;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        //创建二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建结点
        HeroNode root = new HeroNode(1,"zl");
        HeroNode node1 = new HeroNode(2,"js");
        HeroNode node2 = new HeroNode(3,"wym");
        HeroNode node3 = new HeroNode(4,"wb");
        HeroNode node4 = new HeroNode(5,"szh");

        //创建二叉树
        root.setLeft(node1);
        root.setRight(node2);
        node2.setRight(node3);
        node2.setLeft(node4);
        binaryTree.setRoot(root);

        //测试
        System.out.println("前序遍历");     //1  2  3  5  4
        binaryTree.preOrder();
        System.out.println("中序遍历");     //2  1  5  3  4
        binaryTree.infixOrder();
        System.out.println("后序遍历");     //2  5  4  3  1
        binaryTree.postOrder();

        //前序遍历查找
        HeroNode resNode = binaryTree.preOrderSearch(5);
        if(resNode != null) {
            System.out.printf("找到了,信息为%d name=%s", resNode.getNo(), resNode.getName());
        }else {
            System.out.printf("没有找到 no = %d 的英雄", 5);
        }

        //中序遍历查找
        HeroNode resNode1 = binaryTree.infixOrderSearch(4);
        if(resNode != null) {
            System.out.printf("找到了,信息为%d name=%s", resNode1.getNo(), resNode1.getName());
        }else {
            System.out.printf("没有找到 no = %d 的英雄", 4);
        }

        //后序遍历查找
        HeroNode resNode2 = binaryTree.preOrderSearch(3);
        if(resNode != null) {
            System.out.printf("找到了,信息为%d name=%s", resNode2.getNo(), resNode2.getName());
        }else {
            System.out.printf("没有找到 no = %d 的英雄", 3);
        }
    }
}

//二叉树
class BinaryTree {
    private HeroNode root;

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

    //前序遍历
    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;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", 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;
    }

    //前序遍历
    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 查找no
     * @return 找到则返回 Node,否则返回 null
     */
    public HeroNode preOrderSearch(int no) {
        System.out.println("进入前序查找");

        //比较当前结点
        if(this.no == no) {
            return this;
        }

        //左
        //判断当前结点的左子结点是否为空,若不为空,则递归前序查找
        //若左递归前序查找,找到结点,则返回
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if(resNode != null) {   //说明在左子树找到
            return resNode;
        }

        //右
        //1.右递归查找
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    /**
     * 中序遍历查找
     * @param no 查找no
     * @return 找到则返回 Node,否则返回 null
     */
    public HeroNode infixOrderSearch(int no) {

        //左
        //判断当前结点的左子结点是否为空,若不为空,则递归中序查找
        //若左递归前序查找,找到结点,则返回
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixOrderSearch(no);
        }
        if(resNode != null) {   //说明在左子树找到
            return resNode;
        }

        System.out.println("进入中序遍历");
        if(this.no == no) {
            return this;
        }

        //右
        //1.右递归查找
        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }

    /**
     * 后序遍历查找
     * @param no 查找no
     * @return 找到则返回 Node,否则返回 null
     */
    public HeroNode postOrderSearch(int no) {

        //左
        //判断当前结点的左子结点是否为空,若不为空,则递归后序查找
        //若左递归前序查找,找到结点,则返回
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.postOrderSearch(no);
        }
        if(resNode != null) {   //说明在左子树找到
            return resNode;
        }

        //右
        //1.右递归查找
        if (this.right != null) {
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }

        System.out.println("进入后序查找");
        //比较当前结点
        if(this.no == no) {
            return this;
        }
        return resNode;
    }
}

3. 删除结点

若删除的结点是叶子结点,则删除该结点

若删除的结点是非叶子结点,则删除其子树

3.1 思路分析

  • 若树为空树,则只有一个root结点,等价于将二叉树置空
  • 因为本二叉树是单向的,所以我们是判断当前结点的子结点是需要删除结点,而不能判断这个结点是不是需要删除的结点
  • 若当前结点的左子结点不为空,且左子结点就是要删除的结点,则this.left = null,并返回(结束递归)
  • 若当前结点的右子结点不为空,且右子结点就是要删除的结点,则this.right = null,并返回(结束递归)
  • 若上述两步没有删除结点,那么我们就需要向左子树进行递归删除
  • 若还没有删除结点,则向右子树进行递归删除

3.2 代码实现

HeroNode类增加方法

/**
 * 递归删除结点,若删除的是叶子结点,则删除。若非叶子结点,则删除该子树
 * @param no 编号
 */
public void delNode(int no) {
    //若结点的左子结点不为空,且左结点就是要删除的结点,则将 this.left = null,且退出递归
    if(this.left != null && this.left.no == no) {
        this.left = null;
        return;
    }
    //若结点的右子结点不为空,且右结点就是要删除的结点,则将 this.left = null,且退出递归
    if(this.right != null && this.right.no == no) {
        this.right = null;
        return;
    }
    //向左子树进行递归删除
    if(this.left != null) {
        this.left.delNode(no);
    }
    //向右进行递归删除
    if(this.right != null) {
        this.right.delNode(no);
    }
}

BinaryTree类增加方法

//删除结点
public void delNode(int no) {
    if(root != null) {
        //若只有一个root结点,这里直接判断root是不是要删除的结点
        if(root.getNo() == no) {
            root = null;
        }else {
            root.delNode(no);   //递归删除
        }
    }else {
        System.out.println("空树,不能删除");
    }
}

测试

System.out.println("测试删除");
binaryTree.delNode(3);
System.out.println("测试前序遍历");
binaryTree.preOrder();

4. 二叉树的顺序存储

从数据存储来看,数组存储方式和树的存储方式可以相互转换,即数据可以转换为树,树也可以转换成数组

顺序存储二叉树的特点

  • 顺序二叉树通常只考虑完全二叉树
  • 第n个元素的左子结点为 2 * n + 1
  • 第n个元素的右子结点为 2 * n + 2
  • 第n个元素的父结点为 (n - 1) / 2
  • n:表示二叉树中的第几个元素

4.1 顺序存储二叉树遍历

package cn.imut;

import java.sql.SQLOutput;

public class ArrBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};

        //创建二叉树
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
        arrBinaryTree.preOrder();
    }
}

class ArrBinaryTree {
    private int[] arr;  //存储数据结点的数组

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }

    //方法重载
    public void preOrder() {
        this.preOrder(0);
    }

    /**
     * 前序遍历
     * @param index 数组下标
     */
    public void preOrder(int index) {
        //若数组为空,或者arr.length = 0
        if(arr == null || arr.length == 0) {
            System.out.println("数组为空,无法遍历");
        }
        //输出当前元素
        System.out.println(arr[index]);
        //左递归
        if((index * 2 + 1) < arr.length) {
            preOrder(2 * index + 1);
        }
        //右递归
        if((index * 2 + 2) < arr.length) {
            preOrder(2 * index + 2);
        }
    }
}
posted @ 2020-04-24 19:08  LEI_Z  阅读(221)  评论(0)    收藏  举报