Binary Search tree - In order sorting

I have a project where I have given data and I have to insert each using binary search tree technique.
My input data format is
dd mm yyyy code activity shares prices


My binary search tree insertion code(got from D.S.Malik book)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
template<class Cstock_data>

void binary_search_tree<Cstock_data>::insert(const Cstock_data& insert_item)
{
	node_type<Cstock_data> *current;
	node_type<Cstock_data> *trail_current;
	node_type<Cstock_data> *new_node;

	new_node=new node_type<Cstock_data>;
	new_node->info=insert_item;
	new_node->llink=NULL;
	new_node->rlink=NULL;

	if(root==NULL)
		root=new_node;
	else
	{
		current=root;
		while(current!=NULL)
		{
			trail_current=current;
			if(current->info==insert_item)
			{
				cout<<"Item already exists. Duplicate not allowed\n";
				return ;
			}
			else if(current->info>insert_item)
				current=current->llink;
			else 
				current=current->rlink;
		}
		if(trail_current->info>insert_item)
			trail_current->llink=new_node;
		else
			trail_current->rlink=new_node;
	}
}


Relational Operators Definition defined Cstock_data class
1
2
3
4
5
6
7
8
9
10
11
12
13
bool Cstock_data:: operator == (const Cstock_data& right)const
{
	return (right.date1.get_year() == date1.get_year() && right.date1.get_month() == date1.get_month() && right.date1.get_day() == date1.get_day());
}
bool Cstock_data:: operator <= (const Cstock_data& right)const
{
	return (right.date1.get_year() <= date1.get_year() && right.date1.get_month() <= date1.get_month() && right.date1.get_day() <= date1.get_day());
}

bool Cstock_data:: operator > (const Cstock_data& right)const
{
	return ( right.date1.get_year() >  date1.get_year() && right.date1.get_month() >  date1.get_month() && right.date1.get_day() > date1.get_day());
}


Data insertion code in main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
binary_search_tree<Cstock_data> stock_type;
Cstock_data new_stock;
	
	for(int i=0;i<28;i++)
	{		
		in>>day>>month>>year;
		new_stock.set_all(day,month,year,"","",0,0); // To make it simple, I initialize other values except date(dd,mm,yyy) with NULL.
		stock_type.insert(new_stock);		
	}

}

stock_type.inorder_traversal();


My input file

03 01 2012
03 01 2011
17 01 2011
05 02 2001
26 02 2010
03 03 2012
05 08 2007
06 08 2011
15 08 2011
19 10 2011
03 11 2011
04 11 2011
23 11 2011
08 12 2011
24 12 2011
15 03 2008
29 03 2008
04 04 2007
12 04 2004
19 05 2006
25 05 2006
27 05 2006
16 06 2006
17 06 2008
18 06 2010
07 07 2010
20 07 2010
02 08 2008




Now the Problem is when I call the function inorder traversal(which suppose to prints the data in sorted order)
the output sorts only half of the data, remaining are printed in different order.
This is the output I got

03 01 2012
03 01 2011
17 01 2011
06 08 2011
15 08 2011
19 10 2011
23 11 2011
08 12 2011
24 12 2011
18 06 2010
20 07 2010
15 03 2008
29 03 2008
19 05 2006
25 05 2006
27 05 2006
16 06 2006
17 06 2008
12 04 2004
07 07 2010 // from here the sorting goes off
05 02 2001
26 02 2010
05 02 2001
26 02 2010
03 03 2012
05 08 2007
03 11 2011
04 11 2011
04 04 2007
02 08 2008



If you need any other details, please let me know

Thank you
The problem is with operator overloading.
Finally I figured it out. As I said, the problem was with statements in the operator overloading.

Here is the updated code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool Cstock_data:: operator <= (const Cstock_data& right)const
{
	if (right.date1.get_year() <= date1.get_year())
	{
		if(right.date1.get_month()<date1.get_month())
				return true;		
		else if(right.date1.get_month()>date1.get_month())
			return false;
		else
		{
			if(right.date1.get_day()<date1.get_day())
				return true;
			else
				return false;
		}
	}
	else
		return false;
}
Topic archived. No new replies allowed.