473,566 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

binary tree problem

5 New Member
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(istrea m& fin)
{
char buff[256];

// Clear any remaining end of line characters
if(fin.peek()== '\n')
{
fin.ignore();
}
fin.getline(buf f, 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(inFil e);

if(temp==4)
{

//Create node
load->name=value[0];
load->surname=valu e[1];
load->adress=value[2];
load->phoneNo=valu e[3];
load->Email=value[4];
load->value=charValo r(value[0]);
load->pLeft =0;
load->pRight =0;
//Build tree
tree=addNode(tr ee,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 2449
gpraghuram
1,275 Recognized Expert Top Contributor
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
FARAECHILIBRU
5 New Member
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=phon e;
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 Recognized Expert Moderator Specialist
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
6709
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
3390
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 to display, > arrange, find, delete the leaf node in binary tree. > > I hope that anyone with expereince in building binary tree could help > me in...
7
3623
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
9005
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, even the removal, I just don't know how to keep track of the parent, so that I can set its child to the child of the node to be removed. IE - if...
15
5073
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 lists. I'm totally new to binary searches by the way. Can anyone help me with the commented sections below? Much of the code such as functions and...
4
2943
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 looks the data up in the tree.
3
4430
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 decided that the most effective way would be : When i connect to the server once, I download the list and disconnect, instead of connecting every time...
1
2916
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 <typename Tclass Tree{ private: Tree<T*left;
4
11000
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 into an infinite loop, i think im messing up with the pointers. Heres the code. //press 1, first, Do not press it a second time!!!
7
3858
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 binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element...
0
7673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.