[英]AVL Tree Deletion using Recursion
Let x = the node to be deleted 令x =要删除的节点
How can I reassign the left or right pointer of x's parent to x's left or right sub tree during rotation without a parent pointer in node's struct declaration using recursion in an AVL Tree Implementation coded in C? 如何在旋转期间将x的父对象的左或右指针重新分配给x的左或右子树,而在C编码的AVL树实现中使用递归在节点的结构声明中没有父指针?
My Node's struct declaration: 我的节点的结构声明:
typedef struct Node{
int key;
struct Node *left;
struct Node *right;
int height;
}node;
Currently, these are my rotation codes: 目前,这些是我的轮换代码:
void rotateRight(node **n){
node *lChild = (*n)->left;
node *subtree = lChild->right;
// Rotate Right
lChild->right = *n;
(*n)->left = subtree;
*n = lChild;
// Update Height
lChild->right->height = max(getHeight(lChild->right->left), getHeight(lChild->right->right)) + 1;
(*n)->height = max(getHeight((*n)->left), getHeight((*n)->right)) + 1;
}
void rotateLeft(node **n){
node *rChild = (*n)->right;
node *subtree = rChild->left;
// Rotate Left
rChild->left = *n;
(*n)->right = subtree;
*n = rChild;
// Update Height
rChild->left->height = max(getHeight(rChild->left->left), getHeight(rChild->left->right)) + 1;
(*n)->height = max(getHeight((*n)->left), getHeight((*n)->right)) + 1;
}
When I execute these rotation codes, I lose some elements that should have not been deleted. 当我执行这些旋转代码时,我丢失了一些不应该删除的元素。
You can't. 你不能 Either add a pointer to parent to your structure, or write your recursion in a way that passes node and parent on each level, so you can pass the parent as a second parameter to your rotate functions. 您可以将指向父级的指针添加到您的结构,也可以通过在每个级别上传递节点和父级的方式编写递归,这样就可以将父级作为第二个参数传递给您的旋转函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.