473,811 Members | 4,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Runtime errors with c++ binary search tree

I have two classes, a Node class and a Bstree class. The Bstree class
contains a pointer to a Node class as one of its private members. Now I
need to build a binary search tree from multiple objects of the Node
class. I have a function addNode which is defined as follows

void Bstree::addNode ( Node **start, int val )
{
if( *start == NULL ) //if bst empty
{
*start = new Node ( val );
}
else //if bst not empty
{ //val is less than current nodes value
if( val < (*start)->getValue() )
{
Node * pLeft = ((*start)->getLeftPtr() );
addNode ( &pLeft, val );
}

//val is greater than current nodes value
else if( val > (*start)->getValue() )
{
Node *pRight = ((*start)->getRightPtr()) ;
addNode ( &pRight, val );
}

else{
cout <<"Duplicate data. IGNORED!!"<<end l;
}
}
}

When I call this function in my main program, I am calling it this way.

cout<<"Enter a number to add to the binary tree"<<endl;
cin >> num;
Node* temp = btree.getRoot() ;
btree.addNode ( &temp, num );[/highlight]

Function getRoot() returns the root pointer of type class Node which is
a private member of class Bstree. However, when I try to print the
tree, I get memory errors. Its as if the left or right pointers dont
have any values/as if they are null yet I am inputting values in them.
I have tried since this morning and still cant get it. This is my first
C++ assignment. Any help is greatly appreciated.

Dec 9 '05 #1
3 3536
My guess (without seeing details for Node class) you are not getting
updated left/right pointers back to Node structure. You allocating new
node for pLeft, but **start node is not getting this new value. Same
goes to pRight.

Nick Kiguta wrote:
I have two classes, a Node class and a Bstree class. The Bstree class
contains a pointer to a Node class as one of its private members. Now I
need to build a binary search tree from multiple objects of the Node
class. I have a function addNode which is defined as follows

void Bstree::addNode ( Node **start, int val )
{
if( *start == NULL ) //if bst empty
{
*start = new Node ( val );
}
else //if bst not empty
{ //val is less than current nodes value
if( val < (*start)->getValue() )
{
Node * pLeft = ((*start)->getLeftPtr() );
addNode ( &pLeft, val );
}

//val is greater than current nodes value
else if( val > (*start)->getValue() )
{
Node *pRight = ((*start)->getRightPtr()) ;
addNode ( &pRight, val );
}

else{
cout <<"Duplicate data. IGNORED!!"<<end l;
}
}
}

When I call this function in my main program, I am calling it this way.

cout<<"Enter a number to add to the binary tree"<<endl;
cin >> num;
Node* temp = btree.getRoot() ;
btree.addNode ( &temp, num );[/highlight]

Function getRoot() returns the root pointer of type class Node which is
a private member of class Bstree. However, when I try to print the
tree, I get memory errors. Its as if the left or right pointers dont
have any values/as if they are null yet I am inputting values in them.
I have tried since this morning and still cant get it. This is my first
C++ assignment. Any help is greatly appreciated.


Dec 10 '05 #2
The Node class's private members are
...//
private:
int value;
Node* left_ptr;
Node* right_ptr;
static int NumNodes;
};
The interface has set functions for all except NumNodes and has get
functions for all the variables (and a printNodeValue( ) ).
The get functions for the pointers are defined as follows;

Node* Node::getRightP tr( )
{
return right_ptr;
}

The constructor for the Node class sets value to user supplied entry
and sets the left and the right pointers to NULL right of the bat.
Is there a catch im falling for here such that the start node is not
getting new values? I dont understand why this isnt working. Any help
is appreciated. Thanks.

Dec 10 '05 #3
Why you need Bstree class separate to Node?
void Node::Add(int newVal)
{
if (newVal > value)
{
if (right_ptr) right_ptr->Add(newVal);
else
right_ptr = new Node(newVal);
}
else if (newVal < value)
{
// Same for right ptr
}
else
{
// Duplicate
}
}

Dec 11 '05 #4

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

Similar topics

11
2535
by: jova | last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so can someone please explain. Thank You.
0
4256
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a random binary search tree. We've tried everything but are in the land of pointer hell. If someone could help it would be a huge help. our code follows. We've tried 2 diff methods the split() and splitter() functions #include <iostream> #include...
4
9024
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 I had C / \ B D
15
5106
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 printfs has already been completed. Any help is greatly appreciated. Thanks,
1
2513
by: mathon | last post by:
hi, now i facing a problem which i do not know how to solve it...:( My binary search tree structures stores a double number in every node, whereby a higher number is appended as right child and a less or equal number is appended as a left child. Now i want to write a function which deletes the node with the highest number in the tree. I started the function as follows:
1
2954
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
11016
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!!!
11
1191
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
7
3878
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 called the 'Root' of the tree. The other two subsets are themselves binary trees, called the 'Left'...
0
9724
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10394
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9201
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7665
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
6882
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();...
1
4336
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
3863
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.