im试图获取一棵红黑树的高度,该树中插入了一个3000+单词的文件,它给了我高度= 1968我不知道插入部分的错误还是计算高度的算法< / p>
高度功能
#define MAX(a,b) (((a)>(b))?(a):(b))
int height(Node* Root)
{
int h = 0;
if (Root != NULL) {
if (Root == T_Nil)
h = 1;
else
{
int leftHeight = height(Root->left);
int rightHeight = height(Root->right);
h = MAX(leftHeight, rightHeight) + 1;
}
}
return h;
}
具有:
typedef struct Node {
char word[128];
char color;
struct Node * left;
struct Node *right;
struct Node *parent;
} Node;
struct Node T_Nil_Node;
Node * T_Nil = &T_Nil_Node;
插入功能
void redBlackInsert(Node** T, char word[128])
{
Node* z = newNode(word);
Node* y = T_Nil;
Node* x = *T;
while (x != T_Nil)
{
y = x;
if (strcmp(z->word,x->word)<0)
x = x->left;
else
x = x->right;
}
z->parent = y;
if (y == T_Nil)
*T = z;
else if (strcmp(z->word,y->word)<0)
y->left = z;
else
y->right = z;
z->left = T_Nil;
z->right = T_Nil;
z->color = RED;
redBlackInsertFixup(T, z);
}
修正功能
void redBlackInsertFixup(Node** Root, Node* New)
{
Node* temp;
while (New->parent->color == RED)
{
if (New->parent == New->parent->parent->left)
{
temp = New->parent->parent->right;
if (temp->color == RED)
{
New->parent->color = BLACK;
temp->color = BLACK;
New->parent->parent->color = RED;
New = New->parent->parent;
}
else
{
if (New == New->parent->right)
{
New = New->parent;
rotateLeft(Root, New);
}
New->parent->color = BLACK;
New->parent->parent->color = RED;
rotateRight(Root, New->parent->parent);
}
}
else
{
temp = New->parent->parent->left;
if (temp->color == RED)
{
New->parent->color = BLACK;
New->color = BLACK;
New->parent->parent->color = RED;
New = New->parent->parent;
}
else
{
if (New == New->parent->left)
{
New = New->parent;
rotateRight(Root, New);
}
New->parent->color = BLACK;
New->parent->parent->color = RED;
rotateLeft(Root, New->parent->parent);
}
}
}
Root[0]->color = BLACK;
}
向左旋转
void rotateLeft( Node** T, Node* x)
{
Node *y = x->right;
x->right = y->left;
if (y->left != T_Nil)
y->left->parent = x;
y->parent = x->parent;
if (x->parent == T_Nil)
*T = y;
else if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
y->left = x;
x->parent = y;
}
向右旋转(我在修正时使用了它)
void rotateRight(Node** T, Node* y)
{
Node *x = y->left;
y->left = x->right;
if (x->right != T_Nil)
x->right->parent = y;
x->parent = y->parent;
if (y->parent == T_Nil)
*T = x;
else if (y == y->parent->right)
y->parent->right = x;
else
y->parent->left = x;
x->right = y;
y->parent = x;
}
答案 0 :(得分:0)
这行看起来不对
if (Root == T_Nil)
h = 1;
我希望它看起来像这样
if (Root->right == NULL && Root->left == NULL)
h = 1;
这是一个没有子节点的节点,因此高度为1。
该函数看起来像没有if语句就可以工作,因为该函数的null值返回0。由于它在MAX之后已经为+1,所以我猜这是更正确的解决方案。
答案 1 :(得分:0)
似乎您在height
函数中混合了NULL和前哨NIL节点。您可以使用NULL表示“无子代”,也可以使用前哨NIL对象。两者同时出现似乎是错误的。
此外,到达哨兵时返回1似乎是错误的。
这就是我期望的代码...
使用前哨NIL (看来您想这样做)
int height(Node* Root)
{
assert(Root != NULL); // NULL is never allowed
if (Root == T_Nil) return 0; // No data here so return 0
int leftHeight = height(Root->left);
int rightHeight = height(Root->right);
int h = MAX(leftHeight, rightHeight) + 1;
return h;
}
使用NULL (似乎您不想这样做)
int height(Node* Root)
{
if (Root == NULL) return 0; // No data here so return 0
int leftHeight = height(Root->left);
int rightHeight = height(Root->right);
int h = MAX(leftHeight, rightHeight) + 1;
return h;
}