我正在从事一个学校项目,该项目涉及实现带有插入和删除功能的AVL树。但是我的程序在通过预遍历查看avl树时没有给出正确的输出。
avl树的变量:
current_binding = binding
until (line = gets.chomp!) == 'q'
value, current_binding = current_binding.eval("[#{line}, binding]")
puts value unless value.nil?
end
这是我的带有newnode函数调用的插入函数:
typedef struct Node{
int data;
int height;
struct Node *right;
struct Node *left;
}Node;
使用minvaluenode函数调用删除函数:
Node *newNode(int data){
Node *node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = NULL;
node->right = NULL;
node->height = 1;
return(node);
}
Node *insertNode(Node *node, int data){
if(node == NULL)
return(newNode(data));
if(data < node->data)
node->left = insertNode(node->left, data);
if(data > node->data)
node->right = insertNode(node->right, data);
if(data == node->data){
printf("No duplicate allowed.");
return node;
}
node->height = getHeight(node);
return(balancing(node, data));
}
具有rotate,getbalancefactor,max和getheight函数调用的平衡函数:
Node * minValueNode(Node* node){
Node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
Node *deleteNode(Node *root, int data){
if(root == NULL){
printf("Value not found.");
return root;
}
else if ( data < root->data )
root->left = deleteNode(root->left, data);
else if( data > root->data )
root->right = deleteNode(root->right, data);
else if(data == root->data){
if( (root->left == NULL) || (root->right == NULL) ){
Node *temp = root->left ? root->left : root->right;
if (temp == NULL){
temp = root;
root = NULL;
}
else
*root = *temp;
free(temp);
}
else{
Node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = deleteNode(root->right,temp->data);
}
}
if(root == NULL)
return root;
root->height = getHeight(root);
return(balancing(root, data));
}
我的查看功能:
int max(int number1, int number2){
return (number1 > number2) ? number1 : number2;
}
int getHeight(Node* n) {
return max(height(n->left), height(n->right)) + 1;
}
int getBalanceFactor(Node *currentNode){
if(currentNode == NULL)
return 0;
return height(currentNode->left) - height(currentNode->right);
}
Node *rightRotate(Node *currentNode){
Node *temp = currentNode->left;
currentNode->left = temp->right;
temp->right = currentNode;
temp->height = getHeight(temp);
currentNode->height = getHeight(currentNode);
return temp;
}
Node *leftRotate(Node *currentNode){
Node *temp = currentNode->right;
currentNode->right = temp->left;
temp->left = currentNode;
temp->height = getHeight(temp);
currentNode->height = getHeight(currentNode);
return temp;
}
Node *balancing(Node *node, int data){
if(getBalanceFactor(node) >= 2 && data < node->left->data)
return rightRotate(node);
if(getBalanceFactor(node) <= -2 && data > node->right->data)
return leftRotate(node);
if(getBalanceFactor(node) >= 2 && data > node->left->data){
node->left = leftRotate(node->left);
return(rightRotate(node));
}
if(getBalanceFactor(node) <= -2 && data < node->right->data){
node->right = rightRotate(node->right);
return(leftRotate(node));
}
return node;
}
我使用随机插入输入进行了测试:
//print AVL Tree in preorder traversal
void printAVLTree(Node *root){
if(root != NULL)
{
printf("%d ", root->data);
printAVLTree(root->left);
printAVLTree(root->right);
}
}
调用view函数时的输出为:
9 5 10 6 0 11 -1 1 2
正确的输出应为:
5 1 0 -1 2 9 6 10 11
答案 0 :(得分:0)
您的错误是您以错误的顺序(从上到下)更新了高度值:
temp->height = getHeight(temp);
currentNode->height = getHeight(currentNode);
旋转后,下层节点为currentNode
,因此您必须先对其进行更新。
否则,如果在更新temp
之前更新currentNode
,则temp
将使用先前的currentNode
height值,并且高度会错误