简体   繁体   English

C ++中字符串实现的二进制搜索树

[英]Binary search tree of strings implementation in c++

I have written a program that inserts integer values into binary search tree. 我编写了一个将整数值插入二进制搜索树的程序。 It seems to work fine but when I modify the same to accept a character array instead of an integer, I get unexpected results. 看来工作正常,但是当我修改它以接受字符数组而不是整数时,得到了意外的结果。 Here is my complete code: 这是我完整的代码:

struct Node{
char data[50];
struct Node* right;
struct Node* left;
};

typedef struct Node* NODE;

NODE createNode(char data[]){
    NODE newNode = (NODE) malloc (sizeof(struct Node));

if(!newNode){
    cout<<"Not enough memory"<<endl;
    exit(-1);
}
newNode->left = NULL;
newNode->right = NULL;
strcpy(newNode->data,data);
return (newNode);
}

void insertNode(NODE* head,char data[]){

    NODE newNode = createNode(data);
    NODE hold_the_head = *head;
    if(*head == NULL){
        *head = newNode;
        (*head)->right = NULL;
        (*head)->left = NULL;
        return;
    }

    while(1){
        if((newNode->data>(*head)->data)&&((*head)->right==       NULL)){
            (*head)->right = newNode;
            *head = hold_the_head;
            return;
        }
        else if( newNode->data > (*head)->data ){
            (*head) = (*head)->right;
        }

        else if( (newNode->data < (*head)->data) && ( (*head)->left ==   NULL ) ){
            (*head)->left = newNode;
            *head = hold_the_head;
            return;
        }
        else if( newNode->data < (*head)->data ){
            (*head) = (*head)->left;
        }
    }
}

void inOrderTraversal(NODE node){

    if(node == NULL)
       return;
    inOrderTraversal(node->left);
    cout<<node->data<<"\t";
    inOrderTraversal(node->right);
}

int main(){

    NODE head = NULL;
    insertNode(&head,"karan");
    insertNode(&head,"sameer");
    insertNode(&head,"palak");
    insertNode(&head,"jagdish");
    insertNode(&head,"naman");
    insertNode(&head,"umang");
    insertNode(&head,"chandu");

    inOrderTraversal(head);
    cout<<endl;
    return 0;
}

Output : 输出:

karan sameer palak jagdish naman umang chandu karan sameer palak jagdish naman umang chandu

Expected: 预期:

chandu jagdish karan naman palak sameer umang chandu jagdish karan naman palak sameer umang

A question like this has already been asked before but that had some compilation error. 之前已经有人问过这样的问题,但是有一些编译错误。 My code is not throwing any error but there seems to be some logical flaw! 我的代码没有抛出任何错误,但是似乎存在一些逻辑缺陷!

In addition to rachitmanit's answer, I felt like you are writing in C, not C++. 除了rachitmanit的答案之外,我还觉得您正在用C而不是C ++编写。

char data[50];

In case you are writing in C++, I recommend using std::string . 如果您使用C ++编写,建议使用std::string It can be compared conveniently with == , < , etc. 可以方便地与==<等进行比较。

NODE newNode = (NODE) malloc (sizeof(struct Node));

Old C's malloc only allocates memory, and doesn't construct object (ie doesn't call constructor). 旧C的malloc仅分配内存,并且不构造对象(即,不调用构造函数)。 It should be: NODE newNode = new Node; 应该是: NODE newNode = new Node;

(*head)->right = NULL;
(*head)->left = NULL;
/*etc...*/

NULL is usually 0 , which is an integer, not a pointer. NULL通常是0 ,它是一个整数,而不是指针。 I definitely recommend using nullptr . 我绝对建议使用nullptr

void insertNode(NODE* head,char data[]){

Usually, pointer arguments might be nullptr and you should check if it is. 通常,指针参数可能为nullptr ,应检查是否为nullptr I recommend using reference, where you don't have to: void insertNode(NODE &head, std::string data){ 我建议您使用参考,而您不必这样做: void insertNode(NODE &head, std::string data){

cout<<endl;

Should be std::cout<<std::endl . 应该是std::cout<<std::endl

And don't forget to deallocate the memory. 并且不要忘记释放内存。 Your program allocates memory and doesn't deallocate it, which will cause memory leak. 您的程序将分配内存,但不会取消分配内存,这将导致内存泄漏。 struct Node should deallocate the memory when destroyed: struct Node在销毁时应释放内存:

struct Node {
    std::string data;
    Node *right;
    Node *left;
    ~Node() {
        delete right;
        delete left;
    }
};
/* ... */
int main() {
    /* ... */
    delete head;
    return 0;
}

In case of integers, your "data" is actually a value. 如果是整数,则“数据”实际上是一个值。 While here "node->data" is the address of the first block of data[] array. 而“ node-> data”是data []数组的第一个块的地址。 Remember node->data[0] is a value. 记住node-> data [0]是一个值。 You are comparing addresses here not the actual "Value". 您在此处比较的地址不是实际的“值”。

Also, you have to follow this: How to compare string in an character array? 另外,您必须遵循以下步骤: 如何比较字符数组中的字符串?

This should help. 这应该有所帮助。

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

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