gpt4 book ai didi

c - 插入功能无法旋转具有不平衡因子的节点

转载 作者:行者123 更新时间:2023-11-30 17:37:15 25 4
gpt4 key购买 nike

为什么我的插入函数在 AVL 树的 C 实现中无法旋转具有不平衡因子的节点?

这是我的代码:

// AVL Tree Implementation

typedef struct Node{
int key;
struct Node *left;
struct Node *right;
int height;
int balanceFactor;
}node;

int max(int a, int b);
int getHeight(node *n);
int getBalanceFactor(node *n);
void insert(node **n, int key);
void rotateLeft(node **n);
void rotateRight(node **n);

// Traversal
// In-Order Traversal
// Pre-Order Traversal
InOrder(node *n){
if (n != NULL){
InOrder(n->left);
printf("%d ", n->key);
InOrder(n->right);
}
}

define MAX_SIZE 6

int main(){
node *root = NULL;

int a[MAX_SIZE] = {10,9,8,7,6,5};

int i;
printf("\n\nInserting items in the Tree...\n\n");
for(i = 0; i < MAX_SIZE; i++){
printf("\nInserted: %d",a[i]);
insert(&root, a[i]);
}

printf("\n\nElements in the Tree: ");
InOrder(root);
printf("\n\n");
}

int max(int a, int b){
return (a > b)? a : b;
}

/* Get the height of a Node */
int getHeight(node *n){
if(n == NULL)
return 0;
return n->height;
}

/* Get the Balance Factor of a Node */
int getBalanceFactor(node *n){
if(n == NULL)
return 0;
return (getHeight(n->left)-getHeight(n->right));
}

插入函数、向左旋转和向右旋转

void insert(node **n, int key){
if(*n == NULL){
/* Create a New Node */
*n = (node*)malloc(sizeof(node));
if(*n != NULL){
(*n)->key = key;
(*n)->left = (*n)->right = NULL;
(*n)->height = 0;
return;
} else {
printf("\nError: Memory Allocation Fail");
return;
}
}

if (key < (*n)->key){
insert(&((*n)->left), key);
}else{
insert(&((*n)->right), key);
}

(*n)->height = max(getHeight((*n)->left), getHeight((*n)->right)) + 1;

printf("\nHeight of %d is updated to %d",(*n)->key, (*n)->height);

if(getBalanceFactor(*n) > 1){

if(getBalanceFactor((*n)->left) < 0)
rotateLeft(&(*n));

rotateRight(&(*n));
}
else if(getBalanceFactor(*n) < -1){

if(getBalanceFactor((*n)->right) > 0)
rotateRight(&(*n));

rotateLeft(&(*n));
}
}

void rotateRight(node **n){
node *lChild, *subtree;

lChild = (*n)->left;
subtree = lChild->right;

// Rotate Right
printf("\nRotating Right");
lChild->right = *n;
(*n)->left = subtree;

// Update Height
lChild->height = max(getHeight(lChild->left), getHeight(lChild->right)) + 1;
(*n)->height = max(getHeight((*n)->left), getHeight((*n)->right)) + 1;
}

void rotateLeft(node **n){
node *rChild, *subtree;

rChild = (*n)->right;
subtree = rChild->left;

// Rotate Left
printf("\nRotating Left");
rChild->left = *n;
(*n)->right = subtree;

// Update Height
rChild->height = max(getHeight(rChild->left), getHeight(rChild->right)) + 1;
(*n)->height = max(getHeight((*n)->left), getHeight((*n)->right)) + 1;
}

我已经修复了此代码。

这是链接:Pastebin

最佳答案

在调试图形算法时,勾勒出它们的作用通常会有所帮助。例如,在右旋转步骤中:

START         -->    lChild->right = *n;  -->    (*n)->left = subtree;
----------------------------------------------------------------------

*n *n *n
lChild --> lChild --> subtree
subtree *n

你失去了 lChild..

关于c - 插入功能无法旋转具有不平衡因子的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22453851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com