• Advertisement
Sign in to follow this  

why not insert data into tree and out is empty.

This topic is 4728 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#define NULL 0
#include <iostream>
using namespace std;

// Ê÷½ÚµãÀà
class treeNode 
{
	friend class tree;
public:
	treeNode(int item, treeNode *left=NULL, treeNode *right=NULL)
		:data(item), leftChild(left), rightChild(right) {}
	treeNode() :leftChild(NULL) ,rightChild(NULL){}
private:
	treeNode * leftChild, *rightChild;
	char data;
};

//Ê÷Àà
class tree
{
public:
	tree():root(NULL),refValue('c'){}
	tree(int value):refValue(value),root(NULL){}
	virtual ~tree(){destroy(root);}
	virtual treeNode * parent(treeNode * current)
	{return root==NULL||root==current?NULL:parent(root,current);
	}
	virtual treeNode *leftChild(treeNode * current)
	{return root!=NULL?current->leftChild:NULL;
	}
	virtual treeNode *rightChild(treeNode *current)
	{return root!=NULL?current->rightChild:NULL;
	}

	virtual int insert(const int & item);
	//virtual int find(const int & item) const;
	friend istream &operator>>(istream & in,tree &tree);
	friend ostream &operator<<(ostream & out,tree & tree);

private:
	treeNode * root;
	char refValue;
	treeNode *parent(treeNode *start,treeNode *current);
	void traverse(treeNode *current,ostream &out)const;
	virtual int insert(treeNode * & current,const int & item);
//virtual	int find(treeNode * &current,const int & item)const;
	void destroy(treeNode *current);

};

// Ê÷µÄʵÏÖ

void tree::destroy(treeNode *current)
{
	if (current !=NULL)
	{
		destroy(current->leftChild);
		destroy(current->rightChild);
		delete current;
	}
}

treeNode * tree::parent(treeNode * start,treeNode * current)
{
	if (start==NULL)return NULL;
	if (start->leftChild==current||start->rightChild==current)return start;
	treeNode *p;
	if((p=parent(start->leftChild,current))!=NULL)return p;
	else return parent (start->rightChild,current);
}
void tree::traverse(treeNode *current,ostream &out)const
{
	if(current!=NULL)
	{
		out<<current->data<<' ';
		traverse(current->leftChild,out);
		traverse(current->rightChild,out);
	}
}
istream & operator >>(istream & in,tree & tree)
{
	char item;
	cout<<"¶þ²æÊ÷\n";
	cout<<"ÓÃ" <<tree.refValue<<" ½áÊøÊäÈë";
	in>>item;

	while(item!=tree.refValue)
	{
		tree.insert(item-'0');
		cout<<"ok²åÈëÁËÒ»¸ö£¡"<<endl;
		in>>item;
	}
	return in;
}

ostream &operator<<(ostream &out,tree &tree)
{
	out<<"±éÀú£¡\n";
	tree.traverse(tree.root,out);
	out<<endl;
	return out;
}

  int tree::insert(const int & item)
{
	if (root==NULL)return 0;
	insert(root,item);
	return 1;
}
  int tree::insert(treeNode * &tree,const int & item)
{
	treeNode * newNode=new treeNode(item);

	if(root==NULL)
	{root=newNode;
	return 1;
	}
	if(tree->leftChild ==NULL)
		tree->leftChild=newNode;
	if(tree->rightChild==NULL&&tree->leftChild!=NULL)
		tree->rightChild=newNode;
	if(tree->leftChild!=NULL&&tree->rightChild!=NULL)
		cout<<"no space ";
	return 1;
}



main()
{
	tree t1;
	 cin>>t1;
	 cout<<t1;
	//treeNode *t=t1.root();
	 
	
}


Share this post


Link to post
Share on other sites
Advertisement
You really do need to work on posting a question with explanations if you want to get help...You have used up your 'get out of jail for free card' now, so be warned [wink] Anyways, all you need to do is add:


istream &operator>>(istream & in,tree &tree);
ostream &operator<<(ostream & out,tree & tree);


Above the
//Ê÷Àà
class tree
{
public:
...


It should now compile correctly.

- Drew

Share this post


Link to post
Share on other sites
my question is:

when I create node with cin>>t1;and then traverse the tree with cout<<out;

it seem no node created.

I think maybe problem existed in operater << or operatro >> section. but I am not sure.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement