Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8826547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:09:50+00:00 2026-06-14T07:09:50+00:00

Possible Duplicate: C implementation of skew heap I have the following struct: typedef struct

  • 0

Possible Duplicate:
C implementation of skew heap

I have the following struct:

typedef struct Task_t {
   float length;
   int value;
} task_t;

I’m trying to use a skew heap to hold bunch of these structs using the ‘value’. So the struct with the lowest ‘value’ will be the root. My skew heap implementation follows:

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;
    }
}

My problem is I don’t know how to correctly insert and delete structs from this skew heap. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:09:52+00:00Added an answer on June 14, 2026 at 7:09 am

    Instead of storing value directly in each node in your heap, store a pointer to a task instead. Your insertion method should take a pointer to the task you want to add. It will need to allocate memory for the task in the node, and then copy the relevant data in. You will need to free this in the removal method.

    E.g.

    typedef struct node
    {
        Task_t *task;
        struct node *root;
        struct node *left;
        struct node *right;
    } Node;
    
    // your add should have passed in a value, because the only thing your code adds to the heap is zeros
    void skewHeapAdd (struct skewHeap *sk, Task_t *task);
    

    To be honest, you could just store Task_t task; instead of using a pointer because it only contains an int and a float so copying it is not very expensive, but more generally you should use a pointer to a task. In your case, you would also be wasting even more memory by storing an actual struct instead of a pointer to a struct since you have an explicit root pointer in each node. I highly recommend getting rid of that regardless (it is inelegant as well as totally unnecessary), but that would require rewriting a fair bit of your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Does an open-ended interval implementation exist for Java? i have an int
Possible Duplicate: How to: Back button support “Ajax” I have a ASP.NET MVC implementation
Possible Duplicate: How do I use arrays in C++? I have the following C
Possible Duplicate: Event loop implementation for Python 3? I am trying to implement an
Possible Duplicate: Undefined, unspecified and implementation-defined behavior I'm trying to deepen my understanding of
Possible Duplicate: Linked list implementation of Binary Min Heap (Having trouble with manipulation…) Greetings,
Possible Duplicate: What do I use for a max-heap implementation in Python? I am
Possible Duplicate: Should I Use self Keyword (Properties) In The Implementation? Say I have
Possible Duplicate: Implementation of a web-server I am trying to make an http server
Possible Duplicate: RSA Encryption, getting bad length I am trying to encrypt a large

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.