Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DeleteMin Data Structure in C

Posted on 2004-03-21
8
Medium Priority
?
256 Views
Last Modified: 2010-04-15
Does anyone have any suggestions on how to make this cleaner?  It always leaves the last node.
Thanks,
Dan

int DeleteMin(BQ *head, int *data)
{
      BQ *delnode,*temp,*ptr,*ptr1,*headtemp=head;

      if(Empty(head)) return 0;

      if(!head->sib->sib && !head->sib->child)
      {
            free(head->sib);
            return 0;
      }

      for(ptr=head;!ptr->sib->child;ptr=ptr->sib);
      delnode=ptr;

    for(ptr->sib; ptr; ptr=ptr->sib)
      {
            if(ptr->child)
            {
                  if(ptr->child->data<delnode->child->data)
                  delnode=ptr;
            }
      }

      ptr1=delnode->child;
      delnode->child=NULL;
      (*data)=ptr1->data;

      if(temp = ptr1->child)
      {
            while(temp)
            {
                  ptr = temp->sib;
                  temp->sib = NULL;
                  Combine(head->sib, temp);
                  temp = ptr;
                  head = head->sib;
            }
      }
      free(ptr1);

      for(headtemp;headtemp->sib->sib;headtemp=headtemp->sib);
      if(!headtemp->sib->child)
      {
            free(headtemp->sib);
            headtemp->sib=NULL;
      }

      return 1;
}
0
Comment
Question by:rockybalboa
7 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 10645078
Hi rockybalboa,

I see several things that need cleaning up.  If you'll tell me a bit more about what the function is supposed to do I'll be glad to offer some suggestions.


Kent
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10646712
what is BQ data type and what is this function delmin supposed to do ?
0
 

Author Comment

by:rockybalboa
ID: 10647170
Sorry,
DeleteMin is supposed to recursively delete nodes of a Binomial Queue until all are gone.  The original is an array operation that I am converting to a dynamic.  Wish I could post the original code but it's about twice as big as what I've already posted.
Thanks for taking the time to look.
Dan
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 10647191
>Wish I could post the original code but it's about twice as big as what I've already posted.
that would not be a problem ... post it

Also post the definition for BQ
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 10648452
Hi Rocky,

Like Sunny, I don't have much experience with Binomial Queues.  A google search did find a lot of things to study.  :)  Here's a pretty good pictoral:  http://lcm.csa.iisc.ernet.in/dsa/node141.html.  The thing that I don't understand is "child management".  I would expect the nodes of a structure named "binomial" to have, at most, 2 children.  The pictoral shows quite a few nodes with 3.

Just to see if I understand the concept, each node in the queue (tree) contains the depth of the queue at that node.  This implies that nodes are always added to or delete from the top of the structure so that the depth need not be recomputed every time the structure is changed.  Merging of structures can occur only when the two structures are the same depth.  (An interesting concept.)  Intuitively, I would expect that the merge occurs such that the head of both of the structures are peers (at the same level) in the new structure.  However, the pictoral suggests differently.

On to the questions.....


It would seem that the DeleteMin() function would only need to delete the head of the structure and adjust the children as required.  (The trees all appear to be "right handed" with the head being the smalled element.)  If this is so, deletion should be nearly trivial, and restructuring the tree should be "complicated" only when rebalancing the remaining nodes.

1)  How many children can a node have?  The pictoral often shows three links from the head.  How are they "managed"?

2)  Why a recursive deletion?  The only application for this that I can see is to move the children of the deleted node to another node.  If this is the case, I would expect the recursive process to be searching for the appropriate node to insert the abandoned children.  That is, search for all nodes with a "depth" of the deleted node and move the children to the most appropriate node.

3)  You state that your function always leaves "the last node".  Can you explain what you mean by this?


Kent


0
 
LVL 4

Expert Comment

by:booki
ID: 10675757
rockybalboa,

This seems to be similar to a recent post.  See my post for a sol'n.

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20930975.html

b.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11367994
PAQed - no points refunded (of 500)

modulo
Community Support Moderator
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question