2017-03-03 22 views
1

bstsort.c結構本身正在改變值?

#include "header.h" 

/* 
    I had to write my own string functions. 
    checkString = returns 1 if the strings are the same, 0 otherwise 
    checkGreaterString = returns 1 if arg1 comes before arg2, 0 otherwise 
*/ 

//insert a node 
int insertNode(struct NODE *root, char *compare, int cFlag) 
{ 
    // root node 
    //printf("root -> input : %s\n", root -> input); 

    //strings are the same 
    if(checkString(root->input, compare, cFlag)) 
     { 
      //printf("same string\n"); 
      root->isDuplicate++; // count as duplicate 
      //printf("root input %s and its duplicates is : %d\n", root->input, root->isDuplicate); 
      return 0; 
    } 
    //strings are different 
    else 
    { 
     int check = checkGreaterString(root->input, compare, cFlag); 
     //printf("check: %d\n", check); 
     //compare comes before root 
     if (check == 1) 
     { 
      // store compare at left node for root 
      if(root -> left == NULL){ 
       struct NODE *tempNode = malloc(sizeof(*tempNode)); 
       tempNode ->left = NULL; 
       tempNode -> right = NULL; 
       tempNode -> isDuplicate = 0; 
       tempNode -> input = compare; //give node input 
       root -> left = tempNode;  //set left node for root 
       //printf("left node : %s\n", tempNode -> input); 
       //printf("left node duplicate : %d\n", tempNode->isDuplicate); 
       //printf("left nodes parent %s\n", root -> input); 
       return 0; 
      } 
      //left node is not empty, so insert node at that node 
      else 
      { 
       printf("root -> left -> input %s\n", root -> left -> input); 
       printf("compare : %s\n", compare); 
       insertNode(root -> left, compare, cFlag); 
       return 0; 
      } 
     } 
     //compare comes after root 
     else if(check == 0) 
     { 
      // store compare at right node for root 
      if(root -> right == NULL){ 
       printf("im in the right node\n"); 
       struct NODE *tempNode = malloc(sizeof(*tempNode)); 
       tempNode ->left = NULL; 
       tempNode -> right = NULL; 
       tempNode -> isDuplicate = 0; 
       tempNode -> input = compare; //give node input 
       root -> right = tempNode;  // set right node for root 
       //printf("right -> right -> input : %s\n", root -> right -> input); 
       //printf("root -> left -> input %s\n", root -> left -> input); 
       //printf("right node duplicate : %d\n", tempNode->isDuplicate); 
       //printf("right nodes parent %s\n", root -> input); 
       return 0; 

     } 
     ////right node is not empty, so insert node at that node 
     else 
      { 
        //printf("im in the else for right node \n"); 
        //printf("root->right->input%s\n", root -> right -> input); 
        insertNode(root -> right, compare, cFlag); 
      } 
     } 
    } 
} 

header.h

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <ctype.h> 
#include <string.h> 

struct NODE { 
    struct NODE *left; 
    struct NODE *right; 
    char *input; 
    int isDuplicate; 
}; 
struct NODE root; 

我試圖做一個二叉樹,使用由用戶提供的輸入。輸入函數在另一個文件中,但它們都正常工作。我的問題是結構,或者更精確地說,設置結構的左右節點。每次我用特定的輸入設置根的左/右節點時,輸入變化沒有我明確地改變它。我認爲問題出在我的tempNode指針上,但我不確定問題是什麼以及如何解決問題。

這裏是我談到上述問題的例子:

比方說,用戶輸入「的」。 「爲」成爲樹

然後用戶輸入的根「吃」。 「吃」將成爲根

的左子****到這裏工作的。如果我打印我的根,我的根的左節點的字符串,他們會給我正確的字符串*******

現在,用戶輸入「測試」,「測試」併成爲右子根,但現在當我打印左側的孩子時,它也是「測試」。根字符串仍然是正確的。

任何幫助將不勝感激。謝謝!

+0

爲什麼你需要兩個不同的字符串比較函數'checkGreaterString'和'checkString'? – babon

+0

@babon我的教授正在讓我們做我們自己的字符串函數。 – name

+0

但爲什麼兩個不同的功能來做同樣的工作?這兩種功能如何不同? – babon

回答

3

這個問題可能是在這裏:

tempNode -> input = compare; 

你讓你的節點指向輸入緩衝區,但是當你讀下輸入該被覆蓋。 (除非您採取額外的預防措施以避免這種情況)。不是複製指針,而是分配內存來創建字符串的副本,並讓tempNode -> input指向該副本。

+0

謝謝!這工作完美! – name