Java学习——数据结构——二叉树的遍历

学习尚硅谷韩顺平老师的Java数据结构笔记,详情请移步网站
1.为什么需要树这种数据结构
(1) 数组存储方式的分析
优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。
缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低。
在这里插入图片描述
(2) 链式存储方式的分析
优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可, 删除效率也很好)。
缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历) 。
在这里插入图片描述
(3) 树存储方式的分析
能提高数据存储,读取的效率, 比如利用 二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。
在这里插入图片描述
2.树示意图
在这里插入图片描述
3.二叉树的概念
(1) 树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。
(2) 二叉树的子节点分为左节点和右节点
(3) 示意图
在这里插入图片描述
(4) 如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2^n -1 , n 为层数,则我们称为满二叉树。
在这里插入图片描述
(5) 如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树
在这里插入图片描述
4.二叉树遍历的说明
使用前序,中序和后序对下面的二叉树进行遍历.
(1) 前序遍历: 先输出父节点,再遍历左子树和右子树
(2) 中序遍历: 先遍历左子树,再输出父节点,再遍历右子树
(3) 后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点
(4) 小结: 看输出父节点的顺序,就确定是前序,中序还是后序
在这里插入图片描述

package com.Tree;

/**
 * 二叉树的遍历
 */
//定义结点
class Node{
    private int no;
    private String name;
    private Node leftNode;
    private Node rightNode;

    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 getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(Node leftNode) {
        this.leftNode = leftNode;
    }

    public Node getRightNode() {
        return rightNode;
    }

    public void setRightNode(Node rightNode) {
        this.rightNode = rightNode;
    }

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

    //前序遍历
    public void preOrder(){
        System.out.println(this);
        //向左递归
        if (this.leftNode != null){
            this.leftNode.preOrder();
        }
        //向右递归
        if (this.rightNode != null){
            this.rightNode.preOrder();
        }
    }

    //中序遍历
    public void infixOrder(){
        //向左递归
        if (this.leftNode != null){
            this.leftNode.infixOrder();
        }
        System.out.println(this);
        //向右递归
        if (this.rightNode != null){
            this.rightNode.infixOrder();
        }
    }

    //后序遍历
    public void postOrder(){
        //向左递归
        if (this.leftNode != null){
            this.leftNode.postOrder();
        }
        //向右递归
        if (this.rightNode != null){
            this.rightNode.postOrder();
        }
        System.out.println(this);
    }
}

//定义一个二叉树
class BinaryTree{
    private Node rootNode;

    public void setRootNode(Node rootNode) {
        this.rootNode = rootNode;
    }

    //前序遍历
    public void preOrder(){
        if (this.rootNode != null){
            this.rootNode.preOrder();
        }else {
            System.out.println("根节点为空");
        }
    }

    //中序遍历
    public void infixOrder(){
        if (this.rootNode != null){
            this.rootNode.infixOrder();
        }else {
            System.out.println("根节点为空");
        }
    }

    //后序遍历
    public void postOrder(){
        if (this.rootNode != null) {
            this.rootNode.postOrder();
        }else {
            System.out.println("根节点为空");
        }
    }
}

public class BinaryTreeDemo {
    public static void main(String[] args) {
        //创建一棵二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建结点
        Node root = new Node(1, "宋江");
        Node node1 = new Node(2, "吴用");
        Node node2 = new Node(3, "卢俊义");
        Node node3 = new Node(4, "林冲");
        Node node4 = new Node(5, "关胜");

        //手动创建二叉树
        root.setLeftNode(node1);
        root.setRightNode(node2);
        node2.setRightNode(node3);
        node2.setLeftNode(node4);
        binaryTree.setRootNode(root);

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值