I have an assignment with a slightly strange implementation of priority queues using a binary tree. I'm really having trouble trying to understand how to make my insert function work when the parameters are as follows

void insert(tree t, void* item)

where item is the data going to be inserted. The user of the library is supposed to be able to pass the data to the insert function with something like this:

insert(tree, (char*) "information");

The problem I'm having is that I don't understand how to deal with inserting (void*) item without it already being a malloc'd node (with pointers to the left and right already present). I can't throw a statement at the top of my insert function that automatically converts (void*) item into a node, because the insert function will have to recursively call itself until the node get put in the correct place. I wouldn't want to make a node out of something that's already a node.

Is there some kind of check i could do that determines if the void pointer that gets passed is already a node, or just another primitive type?

oh and this is how I have my tree struct setup, which could change if anyone has suggestions on how I could do this a little better so I could eliminate the problem all together.

struct tree{
void* left,right,data;
TreeType type;
};

Anyone that runs into a similar problem might want to know how I fixed it. I changed the insert function to take that void* item and create a new node out of it. It then calls a separate helper function that recursively calls itself to place the new node into the correct place within the priority queue. I still have a lot of other problems with this program, but there's the answer to this particular one :-/


I have another problem though, I seem to have gotten the insert function to work correctly except for one thing. If I insert something that's a higher priority than the root node (higher priority goes at the top of the tree), than everything is merged correctly but it seems like the root of the tree is still considered to be the old root and not the new root.

How can I keep track of the root node without adding a pointer to the root in every node (which would force me to update the pointer to the root in every single node each time the root changes)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.