为什么我的insert_节点函数正在擦除我的根?(C)

为什么我的insert_节点函数正在擦除我的根?(C),c,binary-search-tree,inorder,C,Binary Search Tree,Inorder,我正在尝试编写一个程序,将带有成员字符串的节点插入BST,然后打印有关该BST的信息,如高度、顺序遍历、叶数等 现在,当我进行顺序遍历时,它会打印作为根输入的最后一个字符串,即使它应该位于树的底部 代码如下: 插入函数: void insert_node(Node* root, char *nextString) { int newLessThanRoot = strcmp( root->Word, nextString ) > 0 ? 1 : 0; // test if n

我正在尝试编写一个程序,将带有成员字符串的节点插入BST,然后打印有关该BST的信息,如高度、顺序遍历、叶数等

现在,当我进行顺序遍历时,它会打印作为根输入的最后一个字符串,即使它应该位于树的底部

代码如下:

插入函数:

void insert_node(Node* root, char *nextString) {
    int newLessThanRoot = strcmp( root->Word, nextString ) > 0 ? 1 : 0; // test if nextString is left or right from root

    if (newLessThanRoot && root->Left != NULL) {
      return insert_node(root->Left, nextString);
    }
    if (!newLessThanRoot && root->Right != NULL) {
      return insert_node(root->Right, nextString);
    }

    Node* freshNode = newNode();
    freshNode->Word = malloc(strlen(nextString) +1);
    strcpy(freshNode->Word, nextString);
    freshNode->Left = NULL;
    freshNode->Right = NULL;

    if (newLessThanRoot) {
      root->Left = freshNode;
    }
    else {
      root->Right = freshNode;
    }
}
void inorder(Node *temp) {
  if (temp != NULL) {
    inorder(temp->Left);
    printf("%s ",temp->Word);
    inorder(temp->Right);
  }
}
按顺序遍历函数:

void insert_node(Node* root, char *nextString) {
    int newLessThanRoot = strcmp( root->Word, nextString ) > 0 ? 1 : 0; // test if nextString is left or right from root

    if (newLessThanRoot && root->Left != NULL) {
      return insert_node(root->Left, nextString);
    }
    if (!newLessThanRoot && root->Right != NULL) {
      return insert_node(root->Right, nextString);
    }

    Node* freshNode = newNode();
    freshNode->Word = malloc(strlen(nextString) +1);
    strcpy(freshNode->Word, nextString);
    freshNode->Left = NULL;
    freshNode->Right = NULL;

    if (newLessThanRoot) {
      root->Left = freshNode;
    }
    else {
      root->Right = freshNode;
    }
}
void inorder(Node *temp) {
  if (temp != NULL) {
    inorder(temp->Left);
    printf("%s ",temp->Word);
    inorder(temp->Right);
  }
}
它们的使用方式:

 char inputString[15];
  char *inputStringPtr = &inputString[0];
  Node* root;
  root = newNode();
  fscanf(infile,"%s",inputStringPtr);
  root->Word = inputString;
  //printf("Root's word: %s\n",root->Word);

  while (fscanf(infile,"%s",inputStringPtr) == 1) {
      insert_node(root,inputStringPtr);
      printf("%s\n",inputString);
  }

  int numberOfStrings = num_of_strings(root);
  int heightOfBST = height_of_tree(root);
  int numberOfLeaves = num_of_leaves(root);

  inorder(root);
输入如下所示:

b a c e d l m n o p z
因此,输出(进行顺序遍历时)应为:

a b c d e l m n o p z 
但事实是:

z a c d e l m n o p z

在这里,您可以读取根节点的值:

root = newNode();
fscanf(infile,"%s",inputStringPtr);
root->Word = inputString;
在这里,使用第二个节点的值再次覆盖它:

while (fscanf(infile,"%s",inputStringPtr) == 1) {
您可以使用strdup()创建根值的副本:

root->Word = strdup(inputString);
这会解决你的问题


insert_node()
中,正确复制每个新节点的值。您可能会考虑使用<代码> StrudUp()/代码>,也就是说,代替Malc(StRelf())/SrcPy*(./P>< P>),您的InStand函数和EndotSnObl函数没有问题。用法有一些问题。排队

 root->Word = inputString;

您正在将本地存储地址分配给root用户。随着本地商店的不断变化,词根也会发生变化。

Ahhh,非常感谢,修复该行很有效,非常感谢。