资讯专栏INFORMATION COLUMN

数据结构之二叉树(java版)

JayChen / 2038人阅读

摘要:二叉树是数据结构中很重要的结构类型,学习数据结构也是深入学习编程的必由之路,这里我们简单介绍下我对于二叉树的理解,水平有限,如有错误还请不吝赐教。

二叉树是数据结构中很重要的结构类型,学习数据结构也是深入学习编程的必由之路,这里我们简单介绍下我对于二叉树的理解,水平有限,如有错误还请不吝赐教。

首先照例定义一个二叉树的节点类
class Node {
     private int value;//二叉树的值
     private Node leftChild;//左孩子节点
     private Node rightChild;//右孩子节点

     public Node(int value, Node leftChild, Node rightChild) {
         this.value = value;
         this.leftChild = leftChild;
         this.rightChild = rightChild;
     }
}
插入节点

本次用到的二叉树是顺序二叉树,即判断当前插入的节点值与二叉树中父节点比较,如果value值大于父节点,插入父节点的右子树种,反之,插入左子树种,以此类推,直到找到相应的字节点插入。

public void insert(int value) {
        if (parent == null) {//父节点为空判断
            Node node = new Node(value, null, null);
            parent = node;
            length++;
        } else {
            currentPoint = parent;//将当前结点指向父节点
            while(true){//循环遍历节点,查找适合的插入位置
                if(currentPoint.value>value){
                    if(currentPoint.leftChild!=null){
                        currentPoint=currentPoint.leftChild;
                    }else{
                        currentPoint.leftChild=new Node(value,null,null);
                        length++;
                        break;
                    }
                }else{
                    if(currentPoint.rightChild!=null){
                        currentPoint=currentPoint.rightChild;
                    }else{
                        currentPoint.rightChild=new Node(value,null,null);
                        length++;
                        break;
                    }
                }
            }
        }
    }
中序遍历二叉树节点
    public String visit(Node node) {
        sb.append(node.value+"(");
        if (node.leftChild != null) visit(node.leftChild);//递归遍历左子树
        if (node.rightChild != null) visit(node.rightChild);//递归遍历右子树
        sb.append(")");
        return sb.toString();
    }
整体代码
public class BinaryTree {
    private Node parent;
    private Node currentPoint;
    private StringBuffer sb;
    private int length=0;

    class Node {
        private int value;
        private Node leftChild;
        private Node rightChild;

        public Node(int value, Node leftChild, Node rightChild) {
            this.value = value;
            this.leftChild = leftChild;
            this.rightChild = rightChild;
        }
    }
    public Node getParent(){
        return parent;
    }

    public BinaryTree() {
        sb=new StringBuffer();
    }

    public void insert(int value) {
        if (parent == null) {
            Node node = new Node(value, null, null);
            parent = node;
            length++;
        } else {
            currentPoint = parent;
            while(true){
                if(currentPoint.value>value){
                    if(currentPoint.leftChild!=null){
                        currentPoint=currentPoint.leftChild;
                    }else{
                        currentPoint.leftChild=new Node(value,null,null);
                        length++;
                        break;
                    }
                }else{
                    if(currentPoint.rightChild!=null){
                        currentPoint=currentPoint.rightChild;
                    }else{
                        currentPoint.rightChild=new Node(value,null,null);
                        length++;
                        break;
                    }
                }
            }
        }
    }

    public String visit(Node node) {
        sb.append(node.value+"(");
        if (node.leftChild != null) visit(node.leftChild);
        if (node.rightChild != null) visit(node.rightChild);
        sb.append(")");
        return sb.toString();
    }
    public int getLength(){
        return length;
    }

    @Override
    public String toString() {
        return visit(parent);
    }
}
main函数测试
    public static void main(String[] args) {
        BinaryTree bt = new BinaryTree();
        bt.insert(1);
        bt.insert(3);
        bt.insert(2);
        bt.insert(5);
        bt.insert(6);
        bt.insert(7);
        bt.insert(8);
        bt.insert(9);
        bt.insert(10);
        bt.insert(11);
        bt.insert(12);
        bt.insert(13);
        bt.insert(14);
        bt.insert(15);
        System.out.println(bt.getLength());
        System.out.println(bt.toString());
    }
结果输出

其中括号表示层级包含关系,

更多关于java的文章请戳这里:(您的留言意见是对我最大的支持)

我的文章列表
Email:sxh13208803520@gmail.com

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66960.html

相关文章

  • 数据结构之二叉树

    摘要:数据结构之二叉树本文讲解二叉树的基本操作查找节点计算树的高度清空树递归遍历先序遍历中序遍历后序遍历按层遍历来看一下树的结构首先,为了方便后面看到效果,先手动初始化一个有个节点的二叉树查找节点查找节点递归左子树递归右子树计算树的深度计算树的深 数据结构之二叉树 本文讲解二叉树的基本操作: 查找节点 计算树的高度 清空树 递归遍历:先序遍历、中序遍历、后序遍历 按层遍历 来看一下树的结...

    lansheng228 评论0 收藏0
  • PHPer面试必看:分门别类带你撸《剑指Offer》之二叉树

    摘要:例如输入前序遍历序列和中序遍历序列,则重建二叉树并返回。操作给定的二叉树,将其变换为源二叉树的镜像。剑指中还有一道类似的变种题目,就是下面的这道,之字形遍历二叉树。最后下面的两道题目分别运用了二叉树先序中序遍历算法。 开篇 以下内容可能偏应试但很好理解,所以大家一定要坚持看下去,因为我们变强的过程注定孤独的,坚持下来就会看到明天的太阳。 回顾 showImg(https://user-...

    li21 评论0 收藏0
  • 利用PHP实现常用的数据结构之二叉树(小白系列文章五)

    摘要:回来更新一波,最近刷剑指,才又发现树真是一个大头,二叉树的题目和变化运用好多啊二叉树算法引子很多人说二叉树没什么卵用,我觉得是他的工资和公司让他跨不过这个坎还有很多人学了一些树的知识,发现也用不上,我想说的是,读一本书体现不了这本书 回来更新一波,最近刷《剑指offer》,才又发现树真是一个大头,二叉树的题目和变化运用好多啊~ /** * PHP二叉树算法 * Create...

    developerworks 评论0 收藏0
  • 利用PHP实现常用的数据结构之二叉树(小白系列文章六)

    摘要:回来更新一波,最近刷剑指,才又发现树真是一个大头,二叉树的题目和变化运用好多啊二叉树算法引子很多人说二叉树没什么卵用,我觉得是他的工资和公司让他跨不过这个坎还有很多人学了一些树的知识,发现也用不上,我想说的是,读一本书体现不了这本书 回来更新一波,最近刷《剑指offer》,才又发现树真是一个大头,二叉树的题目和变化运用好多啊~ /** * PHP二叉树算法 * Create...

    Cympros 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<