Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old August 18th, 2008, 08:50 AM
Newbie
 
Join Date: Aug 2008
Location: IL
Age: 28
Posts: 5
Default binary tree problem

i'm building a binary tree the problem is when i'm reading from e text file .First i'm sending to tree builder function a empty node and e new node the function return me firt node of a tree, but when i try to built the secund node ,i dont anderstend whay, my program write to my new node and to first tree node;
string readLine(istream& fin)
{
char buff[256];

// Clear any remaining end of line characters
if(fin.peek()=='\n')
{
fin.ignore();
}
fin.getline(buff, 256, '\n');
return string(buff);
}
Node* readFile(Node* tree)
{


string value[5];
Node* load=new Node();

int temp=0,i;
ifstream inFile("c://treeFile.txt");
// Open files
if (!inFile)
{
// Error
cout << "input file not found" << endl;
}
else
{
// Read input file until end reached

while (!inFile.eof())
{
// Read data from file
value[temp]=readLine(inFile);

if(temp==4)
{

//Create node
load->name=value[0];
load->surname=value[1];
load->adress=value[2];
load->phoneNo=value[3];
load->Email=value[4];
load->value=charValor(value[0]);
load->pLeft =0;
load->pRight =0;
//Build tree
tree=addNode(tree,load);

temp=0;
for(i=0;i<5;i++)
{
value[i]="null";
}
}
else
{
temp++;
}


}

inFile.close();
return tree;
}

}
this are the function that build the nodes and read from file;
Reply
  #2  
Old August 18th, 2008, 01:29 PM
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Age: 29
Posts: 1,169
Default

How u have written the addnode function?
R u adding the new node to a proper position to right or left of first node depending on its value?

Raghu
Reply
  #3  
Old August 18th, 2008, 04:40 PM
Newbie
 
Join Date: Aug 2008
Location: IL
Age: 28
Posts: 5
Default

this is my addnode
Node* addNode(Node* tree, Node* toAdd)
{
if (toAdd == 0)
{
return tree;
}
else if (tree == 0|| tree->value==0)
{
return toAdd;
}
else if(toAdd->value != tree->value)
{
if(toAdd->value < tree->value)
{
tree->pLeft = addNode(tree->pLeft, toAdd);
return tree;
}
else
{
tree->pRight = addNode(tree->pRight, toAdd);
return tree;
}

}
else if(toAdd->surname == tree->surname)
{
return tree;

}
}
but when I try to put data in to Node* load
I get chenges in the Node* tree in the seame time
here:
tempPtr->value = value;
tempPtr->phoneNo=phone;
tempPtr->name=nam;
tempPtr->surname=sNam;
tempPtr->adress=adr;
tempPtr->Email=eM;

tempPtr->pLeft =0;
tempPtr->pRight =0;
Reply
  #4  
Old August 18th, 2008, 05:22 PM
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,855
Default

Please use code tags in your post

http://bytes.com/forum/faq.php?faq=p...ask_a_question
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 204,687 network members.
Post your question now . . .
It's fast and it's free

Popular Articles