Problem 1:
I was wondering if it's possible to add for example the struct below into a binary tree:

struct Appoint
{
string place;
string title;
int hour, minute;
int durationHr, durationMin;
};

I want the above struct to be sorted in the binary tree by hour and minute.
From what i've been looking on the internet though it seems that only one kind of variable can be entered in a binary tree? Is it not possible to add after the int hour and minute into the node of a binary tree with the rest of the Appoint struct?

I have been trying to code the structs for the beginning of the program and it looks like this and I would like to know if i am going the right way about it.

struct Appoint
{
string place;
string title;
int hour, minute;
int durationHr, durationMin;
};
struct TreeNode {
Appoint app;  //The data in this node. Is this possible to put in here and use?
TreeNode *left;  //left subtree
TreeNode * right; //right subtree
TreeNode(Appoint temp) {
//Constructor defined. Do i need to do this to made a node containing the struct?
temp = app;
left = NULL;
right = NULL;
}
};

Problem 2:
If the above is at all possible could anyone show me a basic example of how i could insert all the struct into a node please?

struct Appoint
{
string place;
string title;
int hour, minute;
int durationHr, durationMin;
};
struct TreeNode {
Appoint app;  //The data in this node. Is this possible to put in here and use?
TreeNode *left;  //left subtree
TreeNode * right; //right subtree
TreeNode(Appoint temp) {
//Constructor defined. Do i need to do this to made a node containing the struct?
temp = app;
left = NULL;
right = NULL;
}
};

One, you had this marked as C code, not C++ code. I assume this is C++? It has to be if you are using strings.

Two, I assume line 14 is backwards. You want this perhaps?

app = temp;

Three, if this is a binary tree, how do you decide whether one Appointment object is less than, equal to, or greater than another Appointment object? You'll need to create some operators.

After that, it's like a binary tree with a primitive type like an integer. You'll have an insert function. You first insert at the root node, then depending on whether the new node is "less than" or greater than" the root node, you'll call the insert function function with the root node's left or right child, then its left or right child, etc., till the child is null. That's where your new node goes, just like with a primitive type of data.

Add in some error handling for == if you like too or just make it an assumption that that won't be attempted.

But you can't do anything until you write the < and > operators or some equivalent functions, which means you need to set up some criteria for what is less than and what is greater than.

>>I was wondering if it's possible to add for example the struct below into a binary tree:

Yes it is. All you have to do is create a relational operator that compares hours as
primary and minutes as secondary. Come to think of it. A priority_queue is the
correct data structure for this problem. You can use the time as the priority_queue.
Here is an example.

#include <iostream>
#include <queue>

using namespace std;

struct Action{
	int hours;
	int minutes;
	Action(): hours(),minutes(){}
	Action(int h, int m) : hours(h), minutes(m) {}
	virtual void perform()const{
		/* perform some action */
	}
};

bool operator <(const Action& lhs, const Action& rhs){
	if( lhs.hours < rhs.hours ) return false;
	else if(lhs.hours > rhs.hours) return true;
	else return lhs.minutes < rhs.minutes;
}
void print(const Action& a){
	cout << a.hours << ":" << a.minutes << endl;
}
int main(){
	std::priority_queue<Action> actions;
	actions.push( Action(1,15) );
	actions.push( Action(0,10) );
	actions.push( Action(0,15) );
	actions.push( Action(1,20) );

	while(!actions.empty()){
		print(actions.top() );
		actions.pop();
	}
}

Ok thanks i've managed to create the binary tree and created a successful insert function. The problem is i cannot use priority_queue in my program (it's an assignment with several guidelines i have to follow) so i need to find some other way of finding a way to check if the appointments are not overlapping with either the previous or next meeting.

Edited 6 Years Ago by Drakarus: n/a

Ok thanks i've managed to create the binary tree and created a successful insert function. The problem is i cannot use priority_queue in my program (it's an assignment with several guidelines i have to follow) so i need to find some other way of finding a way to check if the appointments are not overlapping with either the previous or next meeting.

FirstPerson wrote the < operator. Use it to create a > operator. You can write an == operator too. Throw out the queue stuff.

Presumably you can write a binary tree with a regular old primitive type like an int. Use that as your basis. Now create a struct with a single integer element as its sole data member and make THAT into a binary tree. Now substitute in the <, > and == operator functions and make that work.

When all that is working, make it work with a struct of just the hours and minutes. When THAT works, put all the data members into the struct and you are done.

Thanks VernonDozier. I've managed to do an overlap function to find out if the appointment is clashing with a previous or next meeting. Thanks

This question has already been answered. Start a new discussion instead.