473,785 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help finding error in binary tree code

gbd says the segmentation fault is being generated in the insert
function.

//CONSTRUCTOR
tree():data(val ue_type()), left(0), right(0){};
tree(value_type vt, tree* l = 0, tree* r = 0):
data(vt), left(l), right(r) {};

//PRIVATE MEMBERS

value_type data;

tree *left;

tree *right;

//INSERT AND ADD NODE FUNCTIONS

tree::tree* tree::newNode(v alue_type new_data){
tree* node = new tree (data);
node->data = data;
node->left = 0;
node->right= 0;
return node;
}

tree::tree* tree::insert(tr ee* node, value_type new_data){
if (node == 0){
return(newNode( data));
}else

if(data <= node->data){
node->left = insert(node->left, data);
}else
node->right = insert(node->right, data);
return node;
}

//DRIVER

#include<iostre am>
#include "tree.h"
using namespace std;
using namespace abrowning_13;

int main(){

tree t1;
tree* tree_ptr;
int user_input;

while(cin.peek( ) != EOF){
t1.insert(tree_ ptr, user_input);
}
t1.inorder_prin t(tree_ptr);
return 0;

}

May 13 '06 #1
2 2181
andrew browning schrieb:
gbd says the segmentation fault is being generated in the insert
function.

//CONSTRUCTOR
tree():data(val ue_type()), left(0), right(0){};
tree(value_type vt, tree* l = 0, tree* r = 0):
data(vt), left(l), right(r) {};

//PRIVATE MEMBERS

value_type data;

tree *left;

tree *right;

//INSERT AND ADD NODE FUNCTIONS

tree::tree* tree::newNode(v alue_type new_data){
tree* node = new tree (data);
node->data = data;
new_data, not data.
node->left = 0;
node->right= 0;
return node;
}

tree::tree* tree::insert(tr ee* node, value_type new_data){
if (node == 0){
return(newNode( data));
new_data again.
}else

if(data <= node->data){
node->left = insert(node->left, data);
}else
node->right = insert(node->right, data);
return node;
}
The insert function ignores the class instance, it should be a static
function, as should newNode (which is completly useless, since new
tree(new_data) does the same).
//DRIVER

#include<iostre am>
#include "tree.h"
using namespace std;
using namespace abrowning_13;

int main(){

tree t1;
tree* tree_ptr;
int user_input;

while(cin.peek( ) != EOF){
t1.insert(tree_ ptr, user_input);


You use uninitialized tree_ptr, that causes UB and yields to
segmentation fault.

Thomas
May 13 '06 #2
i fixed new_data and made it data to comply with the class instance.
i'm a little stuck, however on how to initialize tree_ptr... pointers
are a little arcane at this point. what do i initialize it to point to?

May 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3961
by: JC | last post by:
Hello, Does anyone know how to get the level(s) in a binary tree. For example: (60) Level 0 / \ (50) (65) Level 1 / \ \
1
3407
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 explaining the binary tree functions, and give a sample code of > the whole picture behind the...
8
1659
by: Roman Mashak | last post by:
Hello, All! I started to implement simple command line interface (CLI), and wanna know how to use table of fucntion calls. Could you please, recommend me some link or give example directly in conference? Thanks in advance! With best regards, Roman Mashak. E-mail: mrv@tusur.ru
66
5415
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it occurs. After the program has read the file, it should offer a menu with three choices. the first is to list all the words along with the number of occurences. The second is to let you enter a word, with the program reporting how many times the...
19
8591
by: ramu | last post by:
Hi, I have, suppose 1000 numbers, in a file. I have to find out 5 largest numbers among them without sorting. Can you please give me an efficient idea to do this? My idea is to put those numbers into a binary tree and to find the largest numbers. How else can we do it? Regards
1
4681
by: satan | last post by:
I need to write the definition of the method leavesCount that takes as a parameter a reference of the root node of a binary tree and returns the numbers of leaves in the binary tree. This what i get so far public class BinaryTree { //Definition of the node protected class BinaryTreeNode { DataElement info;
1
2952
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
1910
memoman
by: memoman | last post by:
Can any body help me in that program ??? mail me if anybody could reach any -helpfull- thing Write a C++ program that namely insert, delete, and search in a fixed record length file (containing a person’s name and age). Associated with the record file will be a file that stores a binary search tree index that allows for a quick search through the index. The required operations will be provided using an input file that will be specified...
20
2406
by: DemonFox | last post by:
i have started my midterm exersize than is on binary treescan anyone help me on the basics i have started and i have made the following on my tree.h file: struct treenode { int data; struct treenode *left; struct treenode *right;
0
10147
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
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
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.