簡體   English   中英

二叉搜索樹:根在中序期間保持 null (C++)

[英]Binary Search Tree: root remains null during Inorder (C++)

所以我對二叉搜索樹有點陌生,我正在嘗試制作一個二叉樹,其中每個節點都是一個字符串向量。 然后每次插入都需要一個字符串,並且只考慮該字符串的第一個字母。 基於前 2 個字母,它將 append 該字符串連接到所有字符串共享相同的 2 個首字母的現有節點,或者創建一個新節點,該節點將保存具有所有相同的 2 個首字母的字符串向量。 我知道很奇怪。 這不是我的主意。

我嘗試通過在每次插入時顯示根來縮小問題所在。 並且插入似乎都工作正常,但是一旦我想按順序顯示節點,根似乎就消失了,但幾乎就像它是不可見的。 基於 output 非常明顯。 我的猜測是它是 null 但我不確定。 抱歉,如果這不是最好的提問方式。 這是我在這里的第一個問題。

這是我的代碼:

#include <iostream> 
#include <string>
#include <vector>
//#include "stringSlicer.h"
using namespace std; 
  
class BST  
{ 
    vector<string> data; 
    BST *left, *right; 
  
public: 
    // Default constructor. 
    BST(); 
  
    // Parameterized constructor. 
    BST(string); 
  
    // Insert function. 
    BST* Insert(BST*, string); 
  
    // Inorder traversal. 
    void Inorder(BST*); 

    // PreOrder Traversal.
    void PreOrder(BST*);

    // PostOrder Traversal
    void PostOrder(BST*);

    // string slicer
    string strSlice(string);

    // print vector
    void printVector(vector<string>);
}; 
  
// Default Constructor definition. 
BST ::BST() 
    : data(0) 
    , left(NULL) 
    , right(NULL) 
{ 
} 
  
// Parameterized Constructor definition. 
BST ::BST(string value) 
{ 
    if(data.empty()){
        data.push_back(strSlice(value));
    }
        data.push_back(value); 
    
    left = right = NULL; 
} 
  
// String slicing function definition
string BST ::strSlice(string word){
    string word2 = "";
    word2 += word[0];
    word2 += word[1];
    return word2;
}


// print vector function definition
void BST ::printVector(vector<string> dataVector){
    for(int i = 0; i < dataVector.size(); i ++){
        cout << dataVector.at(i) << " ";
        cout << "end of this node";
    }
}

// Insert function definition. 
BST* BST ::Insert(BST* root, string value) 
{ 
    if (!root)  
    { 
        // Insert the first node, if root is NULL. 

        return new BST(value); 
    } 
  
    // Insert data. 
    if (strSlice(value).compare(root->data.at(0)) > 0)  
    { 
        // Insert right node data, if the 'value' 
        // to be inserted is greater than 'root' node data. 
        cout << value << " is being put in the right node " << value << " > " << root->data.at(0) << endl;
        // Process right nodes. 
        root->right = Insert(root->right, value); 
        
    } else if (strSlice(value).compare(root->data.at(0)) == 0) {

        cout << value << " is being put in the same node " << value << " = " << root->data.at(0)  << endl;
        root->data.push_back(value);
    }
    else 
    { 
        // Insert left node data, if the 'value' 
        // to be inserted is greater than 'root' node data. 
        cout << value << " is being put in the left node " << value << " < " << root->data.at(0) << endl;
        // Process left nodes. 
        root->left = Insert(root->left, value); 
    } 
  
    // Return 'root' node, after insertion. 
    cout << "after insert root is " << root << endl;
    return root; 
} 
  
// Inorder traversal function. 
// This gives data in sorted order. 
void BST ::Inorder(BST* root) 
{ 
    cout << "root is " << endl;
    if (!root) { 
        return; 
    } 
    Inorder(root->left); 
    printVector(data);
    cout << endl; 
    Inorder(root->right); 
} 

int main() {
    const int size = 5;
    string array [size] = {"hi","hillo","bye","chao","elo"};


     BST b, *root = NULL;
     cout << "root is " << root << endl;
     root = b.Insert(root, array[0]); 
     for (int i = 1; i < size; i ++){
         b.Insert(root, array[i]);
     }
     
     
  
    b.Inorder(root); 
    return 0; 
}

這是 output:

root is 0
hillo is being put in the same node hillo = hi
after insert root is 0xeb7f10
bye is being put in the left node bye < hi
after insert root is 0xeb7f10
chao is being put in the left node chao < hi
chao is being put in the right node chao > by
after insert root is 0xeb7f30
after insert root is 0xeb7f10
elo is being put in the left node elo < hi
elo is being put in the right node elo > by
elo is being put in the right node elo > ch
after insert root is 0xeb7f88
after insert root is 0xeb7f30
after insert root is 0xeb7f10
root is
root is
root is

root is
root is

root is
root is

root is

root is

問題:

您的節點不是NULL ,但您沒有打印其中任何一個的數據。 使用語句printVector(data); 您只打印 object b的數據。

解決方案:

改變printVector(data); printVector(root->data); .

附加信息:

  1. using namespace std; 被認為是一種不好的做法(更多信息在這里)。
  2. 不要創建 object b只是為了使用 class BST的方法,而是創建方法 static 並將節點作為參數傳遞。 它更干凈,有助於避免這種情況下的混淆。
  3. 我個人建議您在 C++ 中使用nullptr而不是NULL
  4. 即使rootNULL ,Inorder 方法也會執行"cout << "root is " << endl;" 在返回並因此輸出不必要的行之前。
  5. 您應該使用delete來釋放您使用new存儲的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM