简体   繁体   English

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

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

So I'm a little new to Binary search trees and I'm trying to make a binary tree where each node is a vector of strings.所以我对二叉搜索树有点陌生,我正在尝试制作一个二叉树,其中每个节点都是一个字符串向量。 then each insertion takes a string and only considers the first letters of that string.然后每次插入都需要一个字符串,并且只考虑该字符串的第一个字母。 Based off the first 2 letters it will either append that string to an existing node where all string share the same 2 first letters or create a new node which will hold a vector of strings with all the same 2 first letters.基于前 2 个字母,它将 append 该字符串连接到所有字符串共享相同的 2 个首字母的现有节点,或者创建一个新节点,该节点将保存具有所有相同的 2 个首字母的字符串向量。 Weird I know.我知道很奇怪。 It wasn't my idea.这不是我的主意。

I've tried narrowing down where the issue is by displaying the root at every insertion.我尝试通过在每次插入时显示根来缩小问题所在。 And the insertions all seem to be working fine, but as soon as I want to display the nodes in Inorder, the root just seems to disappear, BUT almost like it's invisible.并且插入似乎都工作正常,但是一旦我想按顺序显示节点,根似乎就消失了,但几乎就像它是不可见的。 It's very evident base on the output.基于 output 非常明显。 My guess is that it's null but I'm not sure.我的猜测是它是 null 但我不确定。 Sorry if this isn't the best way to ask.抱歉,如果这不是最好的提问方式。 this is my first question here.这是我在这里的第一个问题。

here's my code:这是我的代码:

#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; 
}

here was the output:这是 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

Problem:问题:

Your nodes are not NULL , but you're not printing the data of any of them.您的节点不是NULL ,但您没有打印其中任何一个的数据。 With the statement printVector(data);使用语句printVector(data); you're printing just the data of the object b .您只打印 object b的数据。

Solution:解决方案:

Change printVector(data);改变printVector(data); to printVector(root->data); printVector(root->data); . .

Additional information:附加信息:

  1. using namespace std; is considered a bad practice (More info here ).被认为是一种不好的做法(更多信息在这里)。
  2. Instead of creating an object b just to use the methods of the class BST , make the methods static and pass the node as an argument.不要创建 object b只是为了使用 class BST的方法,而是创建方法 static 并将节点作为参数传递。 It's cleaner and will help to avoid confusions as this case.它更干净,有助于避免这种情况下的混淆。
  3. Personally I would recommend you to use nullptr instead of NULL in C++.我个人建议您在 C++ 中使用nullptr而不是NULL
  4. Even if root is NULL , the Inorder method will execute "cout << "root is " << endl;"即使rootNULL ,Inorder 方法也会执行"cout << "root is " << endl;" before returning and therefore outputting unnecesary lines.在返回并因此输出不必要的行之前。
  5. You should use delete to free the data you store with new .您应该使用delete来释放您使用new存储的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM