简体   繁体   English

在C中循环将Node插入BST

[英]Insert Node into BST on a loop in C

I'm trying to insert into my BST but I'm struggling with creating a loop out of it. 我正在尝试插入我的BST,但正在努力创建一个循环。 The code works when I insert one by one, but when I try to put it into a loop it doesn't insert correctly. 当我一一插入时,该代码有效,但是当我尝试将其放入循环中时,则无法正确插入。

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h> // for strcmp()
#include <ctype.h> // for toupper()

typedef struct BstNode{
  //char name[20];
  // int data;
  struct BstNode* left;
  struct BstNode* right;
  char* name;
}BstNode;

typedef int (*Compare)(const char*, const char*); // makes comparisons easier

/* Returns pointer address to newly created node */
BstNode* createNode(char* name){ 
  BstNode* newNode = (BstNode*)malloc(sizeof(BstNode)); // Allocates memory for the newNode
  newNode->name = name; // newNode->data is like newNode.data
  newNode->left= NULL;
  newNode->right = NULL;
  return newNode;
}

//insert node into Tree recursively
BstNode* insertNode(BstNode* node, char* name, Compare cmp){
  int i;
  /* char *s1 = node->name;
     char *s2 = name;
     printf("s1: %s, s2: %s\n", s1,s2);
     i = strcmp(s1, s2); // if =1, s1 is greater
     printf("i: %d\n", i); */

  if(node == NULL){// if tree is empty
    // printf("inside NULL\n");
    node = createNode(name);
    //return node;
  }
  else{
    i = cmp (name, node->name); // sweet
    if(i == -1){
      // printf("inside left\n");
      node->left = insertNode(node->left, name, cmp);
      //return node;
    }
    else if(i == 1){
      // printf("inside right\n");
      node->right =  insertNode(node->right, name, cmp);
      //return node;
    }
    else if(i == 0 ){ //avoid duplicates for now
      // printf("inside 0\n");
      printf("Name is in BST\n");
      return NULL;
    }
  }
  return node;
}

BstNode* printTree(BstNode* node){
  if(node == NULL){
    return NULL;
  }
  printTree(node->left);
  printf("%s\n",node->name);
  printTree(node->right);
}

int CmpStr(const char* a, const char* b){
  return (strcmp (a, b));     // string comparison instead of pointer comparison
}
//void Insert(Person *root, char name[20]);

int main(){
  BstNode* root = NULL; // pointer to the root of the tree
  char buf[100];
  char option = 'a';

  while(1) { 
    printf("Enter employee name");
    scanf("%s",buf); 
    printf ("Inserting %s\n", buf);
    root =  insertNode(root, buf, (Compare)CmpStr);
    printTree(root);
  }
}

I can do root = insertNode(root, name, (Compare)CmpStr) several times in code, but if I try to loop it with user input it won't insert correctly. 我可以在代码中多次执行root = insertNode(root,name,(Compare)CmpStr),但是如果我尝试用用户输入使其循环,则无法正确插入。 I'm not sure if it has to do with the fact that I'm using scanf() or root not being set correctly. 我不确定是否与我使用scanf()或root设置不正确有关。 I've tried using fgets() as well but I'm not too sure how to use it and keep messing that up. 我也尝试过使用fgets(),但是我不太确定如何使用它,并不断弄乱它。 Any help is appreciated. 任何帮助表示赞赏。

In your loop, you always pass the same buffer to your insert function; 在循环中,您总是将相同的缓冲区传递给insert函数; Your createNode does not copy the content of the buffer but rather stores a reference to the (always) same buffer; 您的createNode不会复制缓冲区的内容,而是存储对(始终)相同缓冲区的引用; Hence, changing the buffer content after insert will also change the "content" of previously inserted nodes. 因此,在插入后更改缓冲区内容也将更改先前插入的节点的“内容”。

I'd suggest to replace newNode->name = name in createNode with newNode->name = strdup(name) . 我建议更换newNode->name = namecreateNodenewNode->name = strdup(name) This will actually copy the passed "contents" and gives your BST control over the memory to be kept. 这实际上将复制传递的“内容”,并让您的BST控制要保留的内存。 Thereby don't forget to free this memory when deleting nodes later on. 因此,以后删除节点时,不要忘记释放此内存。

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

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