Solved

DeleteMin Data Structure in C

Posted on 2004-03-21
8
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 45

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 45

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
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 switch statements in the C programming language.

752 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