Click here to Skip to main content
15,605,875 members
Please Sign up or sign in to vote.
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
Comments
hadihassan09 18-Jul-20 10:08am    
Hello, have you found or been abel to find a solution for the asked topic?

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!
 
Share this answer
 
Comments
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.
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 your teacher spot your weaknesses and set remedial actions.
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]
Advice: Indent your code properly, indentation shows the structure of your code and makes it easier to read.
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.
Notepad++ Home[^]
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.
 
Share this answer
 
v2
Comments
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900