我在StackOverflow中遇到了一些線程,但沒有一個能清除我的疑惑。迭代插入到二叉樹
所以問題很簡單。我需要迭代地將元素插入到二叉樹中。這是我的代碼。
BST newNode(int x)
{
BSTNodePtr node = (BSTNodePtr) malloc(sizeof(struct TreeNode));
node->Element = x;
node->Left = NULL;
node->Right = NULL;
return node;
}
BST Insert(int x, BST T)
{
BST temp_node = T;
while(T != NULL) {
if (x < T->Element)
T = T->Left;
else if (x >= T->Element)
T = T->Right;
}
T = newNode(x);
return temp_node;
}
然而,當我發現這棵樹,我總是得到0高度代碼的高度
int Height(BST T)
{
if (T == NULL)
return 0;
return 1+(max(Height(T->Left), Height(T->Right)));
}
當我做插入遞歸這工作完全正常(使用函數具有完全相同的簽名)
我錯過了什麼?
「我在StackOverflow上遇到過一些*線程」 - Pun是否打算? :P – 2012-09-23 05:55:26
哈哈。也許。 :) – wrahool