数据结构之树----二叉树

数据结构——树

首先什么是树结构?

树是一种描述非线性层次关系的数据结构,树是n个数据结点的集合,这些集结点包含一个根节点,根节点下有着互相不交叉的子集合,这些子集合便是根节点的子树。

树的特点
  • 在一个树结构中,有且仅有一个结点没有直接前驱,它就是根节点。
  • 除了根节点,其他结点有且只有一个直接前驱
  • 每个结点可以有任意多个直接后继
树的名词解释
  • 结点的度:一个结点包含子树的数量。
  • 树的度:该树所有结点中最大的度。
  • 兄弟结点:具有同一父结点的结点称为兄弟结点。
  • 树的深度(高度):叶子结点的深度(高度)为1,根节点深度(高度)最高;
  • 层数:从树根开始算,树根是第一层,以此类推。
  • 森林:由多个树组成

只说干货,现在直接复习最重要的——二叉树

二叉树

二叉树是一种超级超级超级重要的数据结构!也是树表家族最为基础的结构
先看看定义:二叉树嘛,每个结点最多只能有二棵子树,二叉树的子树有左右之分,次序不能颠倒
再看看完全二叉树的性质:

  • 第i层至多有 2的i次方减一 个结点
  • 深度为k的二叉树至多有2k-1个结点
  • 任意二叉树,度为0的节点数=度为2的节点数+1;
  • 如果i为父亲的编号,则孩子的编号为2i和2i+1;
  • 如果孩子编号为n,父亲结点编号为k/2,向下取整

关于树的度:
二叉树中连接节点和节点的线就是度
上面说过——度为0的节点数为度为2的节点数加1,即n0=n2+1
这个公式的推理方法如下:
设:
k:总度数
k+1:总节点数
n0:度为0的节点
n1:度为1的节点
n2:度为二的节点
根据二叉树中度和节点的守衡原理,可列出以下一组方程:
k=n2*2+n1;
k+1=n2+n1+n0;
根据方程可以求出n0=n2+1

基本概念不说,研究一下二叉树的遍历

二叉树的遍历

首先,我们看看前序、中序、后序遍历的特性:
前序遍历: (根——左——右)
1.访问根节点
2.前序遍历左子树
3.前序遍历右子树
中序遍历: (左——根——右)
1.中序遍历左子树
2.访问根节点
3.中序遍历右子树
后序遍历: (左——右——根)
1.后序遍历左子树
2.后序遍历右子树
3.访问根节点

要知道:
中序遍历是很重要的一个判断参照!如果只给我们前序遍历和后序遍历的结果,我们将无法推导出唯一的树。
给我们前序遍历和中序遍历,我们可以推出中序
给我们后序遍历和中序遍历,我们可以推出中序


好了,已经写了很多但是并没有拿出来什么真干货,接下来用代码写一下关于树的操作,这里先讲好——对于“树”这个运行结构,最重要的概念是***“递归”***,要想把树的概念理解好,必须先培养递归的思想。我向这篇文章:写递归函数的正确思想学习了一下,真的写的很棒,条理清晰而且有详有略。在这之后我就希望通过自己亲手敲代码的方式去学习,但是研究了两天,依然进展比较一般,最后通过研究各方博客的C代码,从而找到了思路。不再说废话了,这东西,必须通过自己动手敲才能有收获,好的接下来就要拿代码出来了。这里感谢这片文章二叉树题目实现
以下则是用java代码去写的,注解已经很详细,没有分开写,时不时有新体会也会去添加或修改:

代码

结点类

package com.ma.tree;

public class Node {
    private int no;
    private String name;
    private Node left;

    private Node right;

    public Node(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 Node getLeft() {
        return left;
    }

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

    public Node getRight() {
        return right;
    }

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

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", left=" + left +
                ", right=" + right +
                '}';
    }

    //定义前序遍历
    public void preSelect(){
        System.out.println(this);

        if (this.left != null){
            this.left.preSelect();
        }

        if (this.right != null){
            this.right.preSelect();
        }
    }

    //定义中序遍历
    public void inSelect(){
        if (this.left != null){
            this.left.inSelect();
        }

        System.out.println(this);

        if (this.right != null){
            this.right.inSelect();
        }
    }

    //定义后序遍历
    public void postSelect(){
        if (this.left != null){
            this.left.postSelect();
        }

        if (this.right != null){
            this.right.postSelect();
        }

        System.out.println(this);
    }

    //前序遍历查询
    public Node preSearch(int no){
        //定义一个辅助结点
        Node node = null;

        //判断是否为当前结点
        if (this.no == no){
            return this;
        }

        //查询当前结点下的左子节点,如果不为空,则继续递归向前查找
        if (this.left != null){
            node = this.left.preSearch(no);
        }

        if (node != null){
            return node;
        }

        //查询当前结点下的右子节点,如果不为空,则继续递归向前查找
        if (this.right != null){
            node = this.right.preSearch(no);
        }
        return node;

    }

    //中序遍历查询
    public Node inSearch(int no){
        //定义一个辅助结点
        Node node = null;

        //查询当前结点下的左子节点,如果不为空,则继续递归向前查找
        if (this.left != null){
            node = this.left.preSearch(no);
        }

        if (node != null){
            return node;
        }

        //判断是否为当前结点
        if (this.no == no){
            return this;
        }

        //查询当前结点下的右子节点,如果不为空,则继续递归向前查找
        if (this.right != null){
            node = this.right.preSearch(no);
        }
        return node;

    }

    //后序遍历查询
    public Node postSearch(int no){
        //定义一个辅助结点
        Node node = null;

        //查询当前结点下的左子节点,如果不为空,则继续递归向前查找
        if (this.left != null){
            node = this.left.preSearch(no);
        }

        if (node != null){
            return node;
        }



        //查询当前结点下的右子节点,如果不为空,则继续递归向前查找
        if (this.right != null){
            node = this.right.preSearch(no);
        }
        if (node != null){
            return node;
        }

        //判断是否为当前结点
        if (this.no == no) {
            return this;
        }
        return node;
    }

    //删除结点
    //删除结点的情况有两种:1.删除的是叶子节点 2.删除的是子树
    public void delNode(int no){
        //当前结点左结点不为空。并且左子节点就是要删除的结点  this.left = null
        //当前结点右结点不为空。并且左子节点就是要删除的结点  this.right = null
        //如果第1、2步没有执行,那么需要向左子树进行递归删除
        //如果第3步没有执行,那么需要向右子树进行递归删除
        if (this.left != null && this.left.no == no){
            this.left = null;
            return;
        }
        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);
        }

    }

}

二叉树

package com.ma.tree;

public class BinaryTree {
    private Node root;

    public void setRoot(Node root) {
        this.root = root;
    }
    //前序遍历
    public void preSelect(){
        if (this.root != null){
            this.root.preSelect();
        }else {
            System.out.println("该二叉树为空");
        }
    }
    //中序遍历
    public void inSelect(){
        if (this.root != null){
            this.root.inSelect();
        }else {
            System.out.println("该二叉树为空");
        }
    }
    //后序遍历
    public void postSelect(){
        if (this.root != null){
            this.root.postSelect();
        }else {
            System.out.println("该二叉树为空");
        }
    }

    //根据结点编号前序查询
    public Node preSearch(int no){
        if (root != null){
            return root.preSearch(no);
        }else {
            System.out.println("该二叉树为空");
            return null;
        }
    }

    //根据结点编号中序查询
    public Node inSearch(int no){
        if (root != null){
            return root.inSearch(no);
        }else {
            System.out.println("该二叉树为空");
            return null;
        }
    }

    //根据结点编号后序查询
    public Node postSearch(int no){
        if (root != null){
            return root.postSearch(no);
        }else {
            System.out.println("该二叉树为空");
            return null;
        }
    }

    //删除结点
    public void delete(int no){
        if (root != null){
            if (root.getNo() == no){
                root = null;
            }else {
               root.delNode(no);
            }
        }else {
            System.out.println("该二叉树为空,无法删除");
        }
    }
}

测试

package com.ma.tree;

public class Test {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();

        Node node1 = new Node(1,"刘备");
        Node node2 = new Node(2,"张飞");
        Node node3 = new Node(3,"关羽");
        Node node4 = new Node(4,"张兴");
        Node node5 = new Node(5,"张苞");
        Node node6 = new Node(6,"关平");

        node1.setLeft(node2);
        node1.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);

        binaryTree.setRoot(node1);

        binaryTree.delete(6);

        binaryTree.preSelect();
        System.out.println("------------------------------------------");
        binaryTree.inSelect();
        System.out.println("------------------------------------------");
        binaryTree.postSelect();
        System.out.println("------------------------------------------");


        Node node = binaryTree.preSearch(5);
        if (node != null){
            System.out.println("id为:"+node.getNo()+"\t"+"name为:"+node.getName());
        }else {
            System.out.println("未找到该结点");
        }



    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值