1 Replies - 256 Views - Last Post: 26 October 2013 - 04:09 PM Rate Topic: -----

#1 saeed8321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-October 13

Build a BST each node has person age and a list of names

Posted 26 October 2013 - 03:10 PM

I have problems in this code, I'm trying to build a BST "Binary Search Tree" recursively. Each Node has age and list of names, The code still uncompleted, But I got stuck with this error which prevent me from continuing:
The error
is
error C2664: 'BinarySearchTree::BinaryNode::BinaryNode(int,std::list<_Ty>,BinarySearchTree::BinariyNode *,BinarySearchTree::BinaryNode *)' : cannot convert parameter 2 from 'void' to 'std::list<_Ty>'

The problem in inserting a new item to the list .

My code is:
   
bool  insert(BinaryNode* &p, int age, string person)
		{
			if (p==NULL)
			{
				BinaryNode *t;
				p=new BinaryNode(age,t->name.push_front(person),NULL,NULL);
			}
			else if(p->age==age)
				p->name.pop_back();
			else if( p->age <age)
				insert(p->left,age,person);
			else
				insert(p->right,age,person);

		}



and for the struct of BinaryNode the code is

private: 
 struct BinaryNode
 {
	 int age;
	 list<string> name;
	 BinaryNode *left;
	 BinaryNode *right;
 
 BinaryNode(int personAge ,list<string> personName,BinaryNode *LF,BinaryNode *RH)
 {
	 age=personAge;
	 name=personName;
	 left=LF;
	 right=RH;
 }

 };

		BinaryNode *root;





Can anyone help please?

Is This A Good Question/Topic? 0
  • +

Replies To: Build a BST each node has person age and a list of names

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Build a BST each node has person age and a list of names

Posted 26 October 2013 - 04:09 PM

http://www.dreaminco...sertion-at-bst/

Same class?

BinaryNode *t;
p=new BinaryNode(age,t->name.push_front(person),NULL,NULL);

Create a new pointer t, don't allocate memory for what it's pointing to, and then try to use it? This is also where the error is coming from. push_front returns void.

p->name.pop_back();

Why are you popping a name in an insert function?

Also, why are you using lists in a BST? If that's the required design, then it's a major flaw in my opinion. BSTs are easy to write if you just handle equality with a <=, > scheme.

This post has been edited by eker676: 26 October 2013 - 04:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1