Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Sorting in binary search tree

$
0
0
Hello this one part of my project for binary tree...for some reason I can sort my list of books by year in the binary tree....however the title I can't sort by abc order.....any recommendations?
#include "title.h"
#include <string>
#include<string.h>
using namespace std;
tree1::tree1()
{
	root=NULL;
}
void tree1::insertR(string d, Node1 * &node,Node1 *&newnode)
{

if(node==NULL)
{
node = newnode;
//node->author=newnode->author;
//node->id=newnode->id;
//node->title=newnode->title;
//node->year=newnode->year;
node->left=NULL;
node->right=NULL;
return;
}
else if(node->title.compare(d)>0)
insertR(d,node->left,newnode);
else
insertR(d,node->right,newnode);
}


void tree1::search_by_titleR(string d,Node1* node)
{
	bool found=false;
	if (node)
	{
	if (node->title==d)
	{
		printtwo(node);
		found=true;
	}
	else if (node->title.compare(d)<0)
		return search_by_titleR(d,node->right);
	else
		return search_by_titleR(d,node->left);
	}
	if(!found)
	cout << "\n\tSorry no books listed by that title.\n";
	
}
void tree1::insert(string d,Node1 *&node)	
{
	if (!root)
	{
		root=new Node1;
		root->id=node->id;
		root->author=node->author;
		root->title=node->title;
		root->year=node->year;
		root->left=NULL;
		root->right=NULL;
	}
	if (root->title.compare(d)<0)
		insertR(d,root->left,node);
	else if (root->title.compare(d)>0)
		insertR(d,root->right,node);
}

Node1 *tree1::get_root()
{
	return root;
}

void tree1::search_by_title(string d,tree1 T)
{
	Node1 *curr=T.get_root();
	T.search_by_titleR(d,curr);

}
void tree1::printtwo(Node1* node)
{
			// display the Book data
	cout << "\n\tTitle: ";
	int n=node->title.length();
	for (int i=0;i<n;i++)
		cout<< node->title[i];
	cout << "\n\tID: " << node->id;
	cout << "\n\tYear: " << node->year;
	cout<<  "\n\tAuthor: ";
	 n=node->author.length();
	for (int i=0;i<n;i++)
		cout<< node->author[i];
}
void tree1::printtwoR(Node1 *node)
{
	if (node)
	{	
		 printtwoR(node->right);
		 printtwo(node);
		 printtwoR(node->left);
		 
	}
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>