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

二叉搜索樹遞歸插入不顯示任何內容

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

問題描述

我在二叉搜索樹上做一個小的 Java 工作,但是當我在樹中實現一個節點的遞歸插入并顯示它時,我什么也沒得到.我已經研究了一段時間了,我不確定,但我認為這是一個通過引用的問題.

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;

    }

}

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

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.

任何幫助都會很棒.

推薦答案

BSTNodes 的左右元素為空.您需要在插入之前創建它們.否則,他們會創建一個空的懸掛 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 );
    }

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

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

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 久久精品国产久精国产 | 国产高清视频 | 91视频91| 久久日韩粉嫩一区二区三区 | avav在线看 | 国产精品美女久久久久久久网站 | 成人在线观看免费爱爱 | 一区二区激情 | 欧美一区二区免费电影 | 国产精品国产馆在线真实露脸 | 欧美精品在线一区 | 欧美性乱 | 免费av电影网站 | 一区二区三区高清在线观看 | 看黄在线 | 九九九视频| 米奇成人网 | 欧美一级片 | 欧美日韩电影一区 | 免费高潮视频95在线观看网站 | 欧美精品在线一区二区三区 | 亚洲av毛片 | 亚洲激情一区二区三区 | 国产欧美日韩一区二区三区在线 | 91精品国产91久久久久久吃药 | 五月天婷婷综合 | 免费av手机在线观看 | 国产一区二区三区四区在线观看 | 成人综合视频在线观看 | 国产精品一二区 | 亚洲精品不卡 | 精品在线观看一区二区 | 免费视频一区二区 | 91精品国产综合久久香蕉922 | 中文亚洲视频 | 欧美三级视频 | 密桃av| 9久9久9久女女女九九九一九 | 免费亚洲一区二区 | 激情久久网 | 国产在线网站 |