473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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;
Aug 18 '08 #1
3 2434
gpraghuram
1,275 Expert 1GB
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
Aug 18 '08 #2
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;
Aug 18 '08 #3
sicarie
4,677 Expert Mod 4TB
Please use code tags in your post

http://bytes.com/forum/faq.php?faq=p...ask_a_question
Aug 18 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Will Oram | last post by:
Hi, My assignment is to create a non-binary tree of arbitrary form, and then print out the data in an orderly fashion. The handout contains a tree to be inputted: 2 / | \ 3 7 5 / \ |
1
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am > cs students and are now facing difficult problems in understanding > what a binary tree is, how it works, and the algorithm...
7
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
4
by: Ken | last post by:
I have a binary tree in VB NET and insertions seem to be slow. The program receives data from one source and inserts it into the tree. The program receives data from another source and...
3
by: piotrek | last post by:
Hi I would like to ask you a question. Ian creating app. that download from server directory structure ( whole tree ) and those data are placed in proper places into my treeview control. I...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
4
by: whitehatmiracle | last post by:
Hello I have written a program for a binary tree. On compiling one has to first chose option 1 and then delete or search. Im having some trouble with the balancing function. It seems to be going...
7
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.