可能重复:
斜堆的 C 实现
我有以下结构:
typedef struct Task_t {
float length;
int value;
} task_t;
我正在尝试使用倾斜堆来使用“值”来保存这些结构。因此,具有最低“值”的结构将是根。我的倾斜堆实现如下:
typedef struct node
{
int value;
struct node * root;
struct node * leftchild;
struct node * rightchild;
} Node;
typedef Node * skewHeap;
struct skewHeap
{
struct node * root;
};
void skewHeapInit (struct skewHeap *sk)
{
sk->root = 0;
}
void skewHeapAdd (struct skewHeap *sk)
{
struct node *n = (struct node *) malloc(sizeof(struct node));
struct node *s;
assert(n != 0);
n->value = 0;
n->leftchild = 0;
n->rightchild = 0;
s->root = skewHeapMerge(s->root,n);
}
void skewHeapRemoveFirst (struct skewHeap *sk)
{
struct node * n = sk->root;
sk->root = skewHeapMerge(n->leftchild, n->rightchild);
free(n);
}
struct node * skewHeapMerge(struct node *left, struct node *right)
{
struct node *temp;
if (left == NULL)
return right;
if (right == NULL)
return left;
if (left->value < right->value)
{
temp = left->leftchild;
left->leftchild = skewHeapMerge(left->rightchild, right);
left->rightchild = temp;
return left;
}
else
{
temp = right->rightchild;
right->rightchild = skewHeapMerge(right->leftchild, left);
right->leftchild = temp;
return right;
}
}
我的问题是我不知道如何从这个倾斜堆中正确插入和删除结构。提前致谢。