我知道這可能看起來很簡單,但我一直在摸索着我的頭幾個小時,試圖弄清楚爲什麼我會這樣做,thisNode總是NULL。因爲這是空的,這意味着沒有任何事情最終會被添加到樹中。有沒有任何想法? Arghhh二進制搜索樹節點總是NULL
struct node *tra(struct node * start, Type input)
{
struct node * thisNode = start;
if (thisNode == NULL)
return thisNode;
else
{
Type current = thisNode -> el;
if (strcmp(input, current) > 0)
return tra(thisNode -> right, input);
else if (strcmp(input, current) < 0)
return tra(thisNode -> left, input);
else
return thisNode;
}
}
Ta insert(Type input, Ta ta)
{
if ((find(input, ta)) == FALSE)
{
struct node *newEl = tra(ta -> head, input);
newEl = (struct node*)malloc(sizeof(struct node));
newEl -> el = input;
newEl -> left = NULL;
newEl -> right = NULL;
}
return ta;
}
Boolean find(Type input, Ta ta)
{
if (tra(ta -> head, input) == NULL)
return FALSE;
else
return TRUE;
}
要求人們發現代碼中的錯誤並不是特別有效。您應該使用調試器(或添加打印語句)來隔離問題,然後構造一個[最小測試用例](http://sscce.org)。 – 2013-02-17 22:43:24
@OliCharlesworth抱歉,我應該改寫這個問題。我的意思是你有什麼想法,我可以改正這一點?在過去的幾個小時裏,我一直在使用一個調試器來解決這個問題。我知道它是NULL,因爲它總是進入第一個if語句,但我根本無法弄清楚爲什麼會出現這種情況。 – user1899174 2013-02-17 22:49:27
http://stackoverflow.com/q/14926949/905902是否有任何理由重新發布相同的問題? – wildplasser 2013-02-17 23:12:14