15,605,875 members
1.00/5 (1 vote)
See more:
I am trying to implement Priority queue functions PQ_DeleteMin(), PQ_GetMinKey() for Priority Queue with Binary Search Tree. The binary search tree contains additional information to get the k nearest nodes. Can any one help me how to achieve that in C? Thanks.

What I have tried:

The code for inserting a node is written as:

C++
```Node *insert(Node *Tree, void *Object, float (*Distance)(void *, void *))
{
if(Tree == NULL){
if((Tree=(Node *)malloc(sizeof(Node)))==NULL){
printf("Insufficient memory");
exit(1);
}

Tree->p1 = Object;
Tree->p2 = NULL;
Tree->left = Tree->right =NULL;
}

else if (Tree->p2 == NULL)
Tree->p2 = Object;

else if ((*Distance)(Tree->p1, Object) < (*Distance)(Tree->p2, Object))
insert(Tree->left, Object, Distance);

else insert(Tree->right, Object, Distance);
return(Tree);
}```
Posted
Updated 22-Sep-17 13:25pm
v2
Hello, have you found or been abel to find a solution for the asked topic?

## Solution 1

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

Member 13420508 22-Sep-17 18:53pm
This is not my homework or any assessment. In fact, I am learning by myself to get skill in Machine Learning. This idea of problem I got from going through one of the article on google. I don't want anyone to do coding for me but I really wanted to get some direction as where to begin with. Thank you though.

## Solution 2

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

[Update]
C++
```Node *insert(Node *Tree, void *Object, float (*Distance)(void *, void *))
{
if(Tree == NULL){
if((Tree=(Node *)malloc(sizeof(Node)))==NULL){
printf("Insufficient memory");
exit(1);
}

Tree->p1 = Object;
Tree->p2 = NULL;
Tree->left = Tree->right =NULL;
}

else if (Tree->p2 == NULL)
Tree->p2 = Object;

else if ((*Distance)(Tree->p1, Object) < (*Distance)(Tree->p2, Object))
insert(Tree->left, Object, Distance);

else insert(Tree->right, Object, Distance);
return(Tree);
}```

Professional programmer's editors have this feature.
UltraEdit | The Original Text Editor[^]

Advice: do not pack more code than needed in 1 line, it saves nothing and is not faster, it is just more difficult to read.
C++
```Node *insert(Node *Tree, void *Object, float (*Distance)(void *, void *))
{
if(Tree == NULL){
Tree=(Node *)malloc(sizeof(Node));
if((Tree == NULL){
printf("Insufficient memory");
exit(1);
}
Tree->p1 = Object;
Tree->p2 = NULL;
Tree->left = Tree->right =NULL;
}
else if (Tree->p2 == NULL)
Tree->p2 = Object;
else if ((*Distance)(Tree->p1, Object) < (*Distance)(Tree->p2, Object))
insert(Tree->left, Object, Distance);
else
insert(Tree->right, Object, Distance);
return(Tree);
}```

Advice: do not learn programming with C language. With C language, you are responsible of every details like memory management, array boundaries, memory leak and so on. Handling those details makes learning more difficult.
Use instead real high level language like BASIC, Java or C#, they are marshaled and tell you when something go wrong like reading after the end of an array.

v2