久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

二叉搜索樹遞歸插入不顯示任何內(nèi)容

Binary Search Tree Recursive insert not displaying anything(二叉搜索樹遞歸插入不顯示任何內(nèi)容)
本文介紹了二叉搜索樹遞歸插入不顯示任何內(nèi)容的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我在二叉搜索樹上做一個(gè)小的 Java 工作,但是當(dāng)我在樹中實(shí)現(xiàn)一個(gè)節(jié)點(diǎn)的遞歸插入并顯示它時(shí),我什么也沒得到.我已經(jīng)研究了一段時(shí)間了,我不確定,但我認(rèn)為這是一個(gè)通過引用的問題.

I'm doing a small Java work on Binary Search Tree but when I'm implementing the recursive insert of a node into the tree and display it, I don't get anything. I've been on it for a while now, I don't know for sure but I think it's a pass-by-reference problem.

這是我的代碼:

public class BST {

    private BSTNode root; 

    public BST() {
        root = null;
    }

    public BSTNode getRoot() {
        return root;
    }

    public void insertR( BSTNode root, Comparable elem ) {

        if ( root == null ) {
            root = new BSTNode( elem );
        }
        else {
            if ( elem.compareTo( root.element ) < 0 ) {
                insertR( root.left, elem );
            } else {
                insertR( root.right, elem );
            }
        }

    }

    public void printInOrder (BSTNode root) {
        if (root != null) {

            printInOrder(root.left);
            System.out.println(root.element);
            printInOrder(root.right);

        }
    }
}

class BSTNode {

    protected Comparable element;
    protected BSTNode left;
    protected BSTNode right;

    protected BSTNode ( Comparable elem ) {

        element = elem;
        left = null;
        right = null;

    }

}

我執(zhí)行了一系列 insertR,其中 root 是要插入的節(jié)點(diǎn),elem 是一個(gè)字符串,但它沒有打印出任何內(nèi)容,就好像根本沒有填充樹一樣.我確定我的遞歸插入有問題,但我不確定在哪里,我需要使用遞歸插入方法,它不返回任何我認(rèn)為不可能的東西.

I executed a series of insertR with the root being the node to insert into and the elem is a string but it doesn't print anything out, as if the tree was not filled in at all. I'm sure it's problem with my recursive insert but I'm not sure where, I need to use a recursive insert method that returns nothing which I think is impossible.

任何幫助都會(huì)很棒.

推薦答案

BSTNodes 的左右元素為空.您需要在插入之前創(chuàng)建它們.否則,他們會(huì)創(chuàng)建一個(gè)空的懸掛 BSTNode() 并將其插入,而不連接到樹的其余部分.

Your left, right element of the BSTNodes are null. You need to create them before inserting into it. Otherwise they create an empty hanging BSTNode() and insert it, without connecting to the rest of the tree.

你可以換行,

            if ( elem.compareTo( root.element ) < 0 ) {
                insertR( root.left, elem );
            } else {
                insertR( root.right, elem );
            }

 if ( elem.compareTo( root.element ) < 0 ) {
        if ( root.left == null )
             root.left = new BSTNode( elem );
        else
            insertR( root.left, elem );
    } else {
        if ( root.right == null )
             root.right = new BSTNode( elem );
        else
             insertR( root.right, elem );
    }

這篇關(guān)于二叉搜索樹遞歸插入不顯示任何內(nèi)容的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产福利在线视频 | 99亚洲视频 | 91麻豆精品国产91久久久久久久久 | 99久久精品视频免费 | 国产午夜精品一区二区三区在线观看 | 欧美日韩精品免费观看 | 成年网站在线观看 | 亚洲欧美日韩在线一区二区 | 在线观看亚洲专区 | 亚洲欧美综合精品久久成人 | 日韩欧美精品一区 | 中文字幕亚洲一区二区va在线 | 四虎成人免费视频 | 日韩视频一区二区 | 成人国产精品免费观看视频 | 国产精品久久久久一区二区三区 | 五月激情婷婷网 | 久久久123| 国产精品久久久久久久久久久久久 | 艹逼网| 国产一区二区精华 | 久久久国产一区二区三区 | 成人片免费看 | 欧美性吧 | 国产精品久久九九 | 国产日韩精品一区二区三区 | 亚洲国产精品va在线看黑人 | 国产成人福利在线观看 | 91亚洲国产成人久久精品网站 | 久在线精品视频 | 91精品在线看 | 日日夜夜精品视频 | 国产亚洲精品久久19p | 国产精品久久久久久妇女 | 99精品福利视频 | 欧美日韩久久 | 国产精品一区二区在线播放 | 日韩在线一区视频 | 久久久www成人免费精品 | 嫩呦国产一区二区三区av | 久久这里有精品 |