473,379 Members | 1,235 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,379 software developers and data experts.

need help finding error in binary tree code

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

//CONSTRUCTOR
tree():data(value_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(value_type new_data){
tree* node = new tree (data);
node->data = data;
node->left = 0;
node->right= 0;
return node;
}

tree::tree* tree::insert(tree* 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<iostream>
#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_print(tree_ptr);
return 0;

}

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

//CONSTRUCTOR
tree():data(value_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(value_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(tree* 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<iostream>
#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
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
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...
8
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...
66
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...
19
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...
1
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...
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
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...
20
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;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.