Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

have issue returning pointer data

This topic is 2782 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

I have link list class and binary search tree class. In the Binary search tree I have a pointer to link list nodes. I am using templates and my issue is return the data from that pointer.

How do I get the data instead of the address?

Here is my link list class



#ifndef NODE_H
#define NODE_H
#include <cstdlib>
#include<string>
#include"Struct_header.h"

using namespace std;

template <class LLItem>

class node
{
public:
typedef LLItem value_type;

node(const LLItem& init_data = LLItem(),node* init_link=NULL)
{data_field = init_data; next = init_link;}

void set_data(const LLItem& new_data){data_field = new_data;}
void set_next(node* new_link) {next = new_link;}

const LLItem& data() const{return data_field;}
const node* link() const{return next;}
node* link() {return next;}
LLItem& data() {return data_field;}
private:
LLItem data_field;
node *next;
};




#endif // NODE_H



and here is my binary search tree code



#ifndef BINTREE_H
#define BINTREE_H
#include <cstdlib>
#include<string>




namespace main_tree
{

template <class Item,class Item2>
class TreeNode
{
public:
typedef Item value_type;

TreeNode(const Item& init_data = Item(),
TreeNode* init_Left_link=NULL,
TreeNode* init_Right_link=NULL,
Item2* init_Data_link=NULL)
{
data_field = init_data;
Data = init_Data_link;
Left = init_Left_link;
Right = init_Right_link;
}

void set_data(const Item& new_data){data_field = new_data;}
void set_Data_Link(Item2* new_link) {Data = new_link;}

void set_Left_Link(TreeNode* new_left) {Left = new_left;}
void set_Right_Link(TreeNode* new_right) {Right = new_right;}

const Item2* Data_link() const{return Data;}
const TreeNode* Left_link() const{return Left;}
const TreeNode* Right_link() const{return Right;}
const Item& data() const{return data_field;}

bool is_leaf() const{return (Left == NULL)&&(Right == NULL);}

Item2*& Data_link() {return Data;} <----- right here points to the link list node
TreeNode*& Left_link() {return Left;}
TreeNode*& Right_link() {return Right;}
Item& data() {return data_field;}

private:
Item data_field;
TreeNode *Left;
TreeNode *Right;
Item2 *Data;
};

template <class Item,class Item1,class Item2>
void insert_Tree_node(TreeNode<Item,Item2>*& node_ptr,Item1);

template <class Item,class Item2>
void print_Tree_nodes(const TreeNode<Item,Item2>* node_ptr);

template <class Item, class Item2>
void insert(TreeNode<Item,Item2>*& node_ptr,TreeNode<Item,Item2>*& node_ptr_2 );

template <class Item,class Item2>
void Search_Tree(TreeNode<Item,Item2> *root,string name);
}


#include "Binary_Search.h"
#endif

#include <iostream>
namespace main_tree
{
template <class Item,class Item2>
void print_Tree_nodes(const TreeNode<Item,Item2>* root)
{
if(root != NULL)
{
print_Tree_nodes(root->Left_link());
std::cout<<root->data()<<std::endl;
print_Tree_nodes(root->Right_link());
}

}

template <class Item,class Item1,class Item2>
void insert_Tree_node(TreeNode<Item,Item2> *&Root,Item1 name)
{
TreeNode<string,Item2>* newNode;

newNode = new TreeNode<string,Item2>(name);
newNode->set_data(name);

insert(Root, newNode);
}

template <class Item,class Item2>
void insert(TreeNode<Item,Item2> *&root, TreeNode<Item,Item2> *&newNode)
{
if(root == NULL)
root = newNode;
else if(newNode->data() < root->data())
insert(root->Left_link(),newNode);
else
insert(root->Right_link(),newNode);
}
bool found = false;
template <class Item,class Item2>
void Search_Tree(TreeNode<Item,Item2> *root,string name)
{

while(found == false)
{
cout<<"1"<<endl;
if(root->data() == name)
{
cout<<"2"<<endl;
cout<<root->Data_link(); <------ Here I can get the address but not the data what do I have to change?
found = true;
}
else if(name < root->data() && found == false)
{
cout<<"3"<<endl;
cin.get();
Search_Tree(root->Left_link(),name);
}
else if( found == false)
{
cout<<"4"<<endl;
cin.get();
Search_Tree(root->Right_link(),name);
}
}

}

Share this post


Link to post
Share on other sites
Advertisement
////////////your code
if(root->data() == name)
{
cout<<"2"<<endl;
cout<<root->Data_link(); <------ Here I can get the address but not the data what do I have to change?
found = true;
}

///////////changed code
if(root->data() == name)
{
cout<<"2"<<endl;
cout<< *(root->Data_link()); <------ Here I can get the address but not the data what do I have to change?
found = true;
}

Share this post


Link to post
Share on other sites

////////////your code
if(root->data() == name)
{
cout<<"2"<<endl;
cout<<root->Data_link(); <------ Here I can get the address but not the data what do I have to change?
found = true;
}

///////////changed code
if(root->data() == name)
{
cout<<"2"<<endl;
cout<< *(root->Data_link()); <------ Here I can get the address but not the data what do I have to change?
found = true;
}




I tried that and I all I get is

Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'node<LLItem>' (or there is no acceptable conversion) c:\users\shauna\documents\visual studio 2010\projects\bintree2\bintree2\binary_search.h 48

Share this post


Link to post
Share on other sites
It means exactly what it says. Your tree contains Node<LLItem>s. You have retrieved one of them from the tree, and are now attempting to print it. You have not defined how to do so.

Share this post


Link to post
Share on other sites
why is my code creating a blank node? I cant find the issue.

by default the data_link is set to NULL



template <class Item>
class node;

class TreeNode
{
public:

TreeNode(const string init_data = "",
TreeNode* init_Left_link=NULL,
TreeNode* init_Right_link=NULL,
int* data_link=NULL
)
{
data_field = init_data;
Left = init_Left_link;
Right = init_Right_link;
Data = data_link;
}

void set_data(const string& new_data){data_field = new_data;}
void set_Data_Link(int* new_link) {Data = new_link;}

void set_Left_Link(TreeNode* new_left) {Left = new_left;}
void set_Right_Link(TreeNode* new_right) {Right = new_right;}

const int* Data_link() const{return Data;}
const TreeNode* Left_link() const{return Left;}
const TreeNode* Right_link() const{return Right;}
const string& data() const{return data_field;}

bool is_leaf() const{return (Left == NULL)&&(Right == NULL);}

// int Data_link() {return *Data;}
TreeNode*& Left_link() {return Left;}
TreeNode*& Right_link() {return Right;}
string& data() {return data_field;}

private:
string data_field;
TreeNode *Left;
TreeNode *Right;
int *Data;
};


template <class Item>
void insert_Tree_node(TreeNode*& root,node<Item>* head, string name);


void print_Tree_nodes(const TreeNode* node_ptr);


void insert(TreeNode*& node_ptr,TreeNode*& node_ptr_2 );



Here is where I link the id from the link list to the binary search tree

template <class Item>
void insert_Tree_node(TreeNode *&Root,node<Item>* cursor, string name)
{
TreeNode* newNode;
int *point;

point = &(cursor->data().id);
newNode = new TreeNode(name);
newNode->set_data(name);
newNode->set_Data_Link(point);
insert(Root, newNode);
}


void insert(TreeNode *&root, TreeNode *&newNode)
{
if(root == NULL)
root = newNode;
else if(newNode->data() < root->data())
insert(root->Left_link(),newNode);
else
insert(root->Right_link(),newNode);
}


first I create the nodes then I call the read function to read the data in from a file to fill out the binary search tree and link list.


int main()
{
string name = "";
user_data.name = "";
user_data.id = -1;
node<user>* head ; //1
head = new node<user>(user_data);
TreeNode* Root;

Root = new TreeNode;

if(!Read_Data_File(head,Root))
{
cout<<"No data file found"<<endl;
cout<<"Program exiting"<<endl;
}
else
{
print_Tree_nodes(Root);
system("PAUSE");
menu(head,Root);
}

Pause_Pro();
return 0;
}



this is where I read the data in and fill out the link list and the binary search tree but for some reason the very fist node is blank and not the first node from the file. What could be the issue?


bool Read_Data_File(node<user> *head_ptr,TreeNode*& Root)
{
string users = "";
fstream infile;
bool found = false;
infile.open("USERS.dat");

node<user>* cur_ptr ; //2A
node<user>* new_node;
node<user>* prev_ptr;
node<user>* temp_node;

string name = "";

if(infile.is_open())
{
while(infile.good())
{
infile>>user_data.name>>skipws>>user_data.id;
name = user_data.name;
new_node = new node<user>(user_data);

found = false;
while(!found)
{

if(head_ptr->data().name == "" &&head_ptr->data().id == -1)//3
{
head_ptr->set_data(new_node->data());
cur_ptr = head_ptr;
prev_ptr = cur_ptr;
insert_Tree_node(Root,new_node,name);
found = true;
}
else
{
if(new_node->data().name > cur_ptr->data().name)
{
if(cur_ptr->link() == NULL )
{
cur_ptr->set_next(new_node);
cur_ptr = head_ptr;
//insert_Tree_node(Root,cur_ptr,name);
found = true;
}
prev_ptr = cur_ptr;
cur_ptr = cur_ptr->link();
}
else if(new_node->data().name > prev_ptr->data().name && new_node->data().name < cur_ptr->data().name)
{
prev_ptr->set_next(new_node);
new_node->set_next(cur_ptr);
//insert_Tree_node(Root,cur_ptr,name);
cur_ptr = head_ptr;
found = true;
}
else if(new_node->data().name < head_ptr->data().name)
{
temp_node = new node<user>(head_ptr->data(),head_ptr->link());
head_ptr->set_data(new_node->data());
head_ptr->set_next(temp_node);
cur_ptr = head_ptr;
insert_Tree_node(Root,new_node,name);
prev_ptr = cur_ptr;
found = true;
}
}

}

}
return true;
infile.close();
}
else
{
return false;
}
}

Share this post


Link to post
Share on other sites
here is a simplified version of my tree

header

void print_Tree_nodes(const TreeNode* root)
{
if(root)
{
print_Tree_nodes(root->Left_link());
std::cout<<root->data()<<" ";
std::cout<<*(root->Data_link())<<std::endl;
print_Tree_nodes(root->Right_link());
}

}


void insert_Tree_node(TreeNode *Root,int* temp,string name)
{
TreeNode* newNode;

newNode = new TreeNode(name);

newNode->set_data(name);
//newNode->set_Data_Link(temp);
newNode->set_Data_Link(NULL);
newNode->set_Left_Link(NULL);
newNode->set_Right_Link(NULL);
insert(Root, newNode);
linker(Root,temp);
}


void insert(TreeNode *&root, TreeNode *newNode)
{
if(root == NULL)
root = newNode;
else if(newNode->data() < root->data())
insert(root->Left_link(),newNode);
else
insert(root->Right_link(),newNode);
}

void linker(TreeNode *root, int* i)
{
if(root)
{
if(root->Data_link() == NULL)
{
root->set_Data_Link(i);
}
else
linker(root->Left_link(),i);
linker(root->Right_link(),i);
}
}


the class


class TreeNode
{
public:


TreeNode(const string init_data = " fred",
TreeNode* init_Left_link=NULL,
TreeNode* init_Right_link=NULL,
int* data_link=NULL
)
{
data_field = init_data;
Left = init_Left_link;
Right = init_Right_link;
Data = data_link;
}

void set_Data_Link(int* new_link) {Data = new_link;}
void set_data(const string& new_data){data_field = new_data;}


void set_Left_Link(TreeNode* new_left) {Left = new_left;}
void set_Right_Link(TreeNode* new_right) {Right = new_right;}

const int* Data_link() const{return Data;}
const TreeNode* Left_link() const{return Left;}
const TreeNode* Right_link() const{return Right;}
const string& data() const{return data_field;}

bool is_leaf() const{return (Left == NULL)&&(Right == NULL);}

int*& Data_link() {return Data;}
TreeNode*& Left_link() {return Left;}
TreeNode*& Right_link() {return Right;}
string& data() {return data_field;}

private:
string data_field;
TreeNode *Left;
TreeNode *Right;
int *Data;
};


void insert_Tree_node(TreeNode* node_ptr,int*,string);


void print_Tree_nodes(const TreeNode* node_ptr);


void insert(TreeNode*& node_ptr,TreeNode* node_ptr_2 );
void linker(TreeNode *root, int* i);



Main

TreeNode* Root;

int* num;

bool Read_Data_File( TreeNode *&Root);


void main()
{
string name = "";

Root = new TreeNode(name);
if(!Read_Data_File(Root))
{
cout<<"No data file found"<<endl;
cout<<"Program exiting"<<endl;
}

print_Tree_nodes(Root);
system("PAUSE");
}




bool Read_Data_File( TreeNode *&Root)
{

string users,id;
fstream infile;
bool found = false;
infile.open("USERS.dat");

string name = "";
num = new int(NULL) ;
*num = 9;
if(infile.is_open())
{
while(infile.good())
{
infile>>name>>skipws>>id;

insert_Tree_node(Root,num,name);
}

return true;
infile.close();
}
else
{
return false;
}
}




the output



9 <<<<<<<<<<<<<<<<<<<WHY THE LEADING 9?
ALBERT 9
ALBERTA 9
BORIS 9
CANUS 9
CHUCKIE 9
COMMODORE 9
EDGAR 9
ELF 9
ELMIRA 9
ELMO 9
GRISLEE 9
IGOR 9
MCINTOSH 9
PIPPIN 9
POLARO 9
ROMEO 9
TERESA 9
ZELDA 9
Press any key to continue . . .




the data file



IGOR 12345
ELMIRA 16421
BORIS 42116
CANUS 72143
EDGAR 66615
CHUCKIE 63112
ALBERT 44721
COMMODORE 79062
ALBERTA 44722
PIPPIN 22361
MCINTOSH 51552
TERESA 13412
ROMEO 41612
ELF 00211
GRISLEE 11211
POLARO 41362
ELMO 99198
ZELDA 31624

Share this post


Link to post
Share on other sites
I'm suspicious of the "else" in linker(). I don't know why you are using pointers for the integer data.

You're program is complicated by the fact that all the code is put in a single class. Split the code into (at least) three classes, the data to be inserted, the Nodes and the Tree itself.

Here is the interface I came up with:


// Utility type

class noncopyable
{
protected:
noncopyable()
{
}
private:
// Deliberately private & unimplemented, don't copy me!
noncopyable(const noncopyable &);
noncopyable &operator=(const noncopyable &);
};

// Data type

struct Data
{
std::string name;
int id;
};

std::ostream &operator<<(std::ostream &out, const Data &data);

bool operator<(const Data &a, const Data &b);

// Node type

class TreeNode : noncopyable
{
public:
TreeNode(const Data &data);

~TreeNode();


const TreeNode *left() const;

const TreeNode *right() const;

bool is_leaf() const;

const Data& data() const;

Data& data();

void insert(const Data &data);

private:
// Helper functions omitted.

Data data_;
TreeNode *left_;
TreeNode *right_;
};

// Tree type

class Tree : noncopyable
{
public:
Tree();

~Tree();

void insert(const Data &data);

void print() const;

private:
// Helper functions omitted

TreeNode *root;
};

// Main program:

bool Read_Data_File(Tree &tree)
{
std::fstream infile("USERS.dat");
if(!infile.is_open())
{
return false;
}

Data data;
while(infile >> data.name >> data.id)
{
tree.insert(data);
}

return true;
}


int main()
{
Tree tree;
if(!Read_Data_File(tree))
{
std::cout << "No data file found" << std::endl;
std::cout << "Program exiting" << std::endl;
}

tree.print();
std::system("PAUSE");
}

If you are careful, implementing the above is quite easy. Note also that the client interface for the Tree is much nicer. The client should not have to deal with the node pointers.

Ideally, we would remove the print() function from the tree class and provide iterators so the client can visit the tree nodes to perform an arbitrary operation, but for the moment this will get you up and running.

Note that in the above implementation we could eventually mode the TreeNode type to be a private inner class of the Tree, because it is a implementation detail.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!