前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >二叉查找树代码java+运行结果

二叉查找树代码java+运行结果

作者头像
发布2020-10-23 10:36:21
6060
发布2020-10-23 10:36:21
举报
文章被收录于专栏:后端JavaEE

1.代码

代码语言:javascript
复制
package TestTree;/*
 * zt
 * 2020/8/3
 * 14:41
 *二叉查找树:

 */

public class BinarySortTree {

    //根节点
    private Node root;
    //元素个数
    private int size;

    public int getSize() {
        return size;
    }

    //添加元素
    public void add(int v){
        if(root==null){//当前树是空的
            root = new Node(v);
            size++;
            System.out.println("添加了根元素"+v);
        }else{//不是空
            //给根添加子节点
            if(root.addChild(v)){
                size++;
            }
        }
    }

    //中序遍历
    public void middleleList(){
        root.printNode();
    }

    /*
    节点类型
     */
    static class Node {
        //元素值
        private int item;
        //左节点
        private Node left;
        //右节点
        private Node right;

        public Node(int item) {
            this.item = item;
        }

        //添加孩子节点方法
        public boolean addChild(int v) {
            if (this.item > v) {//v放在左边
                if (this.left == null) {//左边没有元素
                    this.left = new Node(v);
                    System.out.println(v+"放在"+this.item+"的左边");
                } else {
                    return this.left.addChild(v);//递归(可能出现重复元素false的情况)
                }

            }else if(this.item<v){
                if(this.right==null){
                    this.right=new Node(v);
                    System.out.println(v+"放在"+this.item+"的右边");
                }else{
                    return this.right.addChild(v);
                }
            }else{
                System.out.println(v+"重复元素");
                return false;
            }
            return true;
        }

        //打印元素
        public void printNode(){
            //判断左边的,如果有元素就打印
            if(this.left!=null){
                this.left.printNode();
            }
            //打印中间的
            System.out.println(this.item);

            //判断右边的,如果有元素就打印
            if(this.right!=null){
                this.right.printNode();
            }
        }
    }
}

将main方法写在了另一个测试类里面

代码语言:javascript
复制
package TestTree;/*
 * zt
 * 2020/8/3
 * 15:03
 *
 */

public class TestBinarySortTree {
    public static void main(String[] args) {
        BinarySortTree tree = new BinarySortTree();
        tree.add(10);
        tree.add(30);
        tree.add(30);
        tree.add(40);
        tree.add(50);
        System.out.println("元素个数:"+tree.getSize());
        tree.middleleList();

    }
}

2.运行结果

代码语言:javascript
复制
添加了根元素10
30放在10的右边
30重复元素
40放在30的右边
50放在40的右边
元素个数:4
10
30
40
50
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/08/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.代码
  • 2.运行结果
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档