1
我有以下結構:
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;
}
}
我的問題是我不知道如何正確插入和刪除這個歪斜堆的結構。提前致謝。
謝謝你的迅速反應和明確的解釋。 – Brkk