Solved

DeleteMin Data Structure in C

Posted on 2004-03-21
8
246 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
8 Comments
 
LVL 45

Expert Comment

by:Kdo
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:Kdo
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now