initializing a 'root' pointer to non NULL value - BST

Hi All,
I'm trying to build a database using a Binary Search Tree.  However, I can't get the pointer to root to initialize to a non-NULL value.

The following are the constructors for my Node file:

Node::Node()
{
      class *ptr1 = new class;
      ptr1 = NULL;
      ptr2 = NULL;      
}  

Node::Node( class* classPtr )
{
      class *ptr1 = new class;
      ptr1 = classPtr;
      ptr2 = NULL;      
}  

My Database constructor looks as follows:

DB::DB()            //Creates an empty database
{
      //create a new pointer to the root of the BST
      class *classPtr = new class();
      classPtr ->setMember(1);
      classPtr->setMember(2);
      classPtr->setMemberFn(1);
      Node *root = new Node(classPtr);
}

When I try to gdb the program, the pointer  "root" still shows up as NULL, why is this?
theakstAsked:
Who is Participating?
 
AxterConnect With a Mentor Commented:
class DB{
private:
  Node* root;
 
In above code, root is a member of the class.

You need to allocate memory to it in your constructor
DB::DB()          //Creates an empty database
{
     //create a new pointer to the root of the BST
     class *classPtr = new class();
     classPtr ->setMember(1);
     classPtr->setMember(2);
     classPtr->setMemberFn(1);
     root = new Node(classPtr); //******* Notice NO Node type declaration behind root  *****
}
0
 
AxterCommented:
>>When I try to gdb the program, the pointer  "root" still shows up as NULL, why is this?

How are you evaluating pointer root?
Since it's the last line in your function, it's probably going to be NULL when you evaluated.

Trying putting an extra dummy line after that line, and then put the breakpoint there.
Then you should be able to evaluate root, and I'm sure you'll find that it's not NULL.
0
 
theakstAuthor Commented:

root gets evaluated later by calling one of its member fn's and thusly creating a 'segmentation fault'

I added the following:
.
.
Node *base = new Node(classPtr);
root = base;

<breakpoint>
}

sadly, root still evaluates to 0...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
efnCommented:
You probably want root to be a member of DB.  As shown, it's just a local variable in the constructor.

You seem to have a class named "class".  Since "class" is a language keyword, this is likely to confuse the compiler.  You should choose a different name for your class.
0
 
theakstAuthor Commented:
Is there another (simpler) way of setting a pointer of type object to a non NULL value?

0
 
AxterCommented:
>>sadly, root still evaluates to 0...

Did you check if Base is evaluated to zero?


>>root gets evaluated later by calling one of its member fn's and thusly creating a 'segmentation fault'

You can not have root be evaluated later.
It goes out of scope at the end of that function.

So what ever you're evaluating is not valid.
0
 
AxterCommented:
>>Is there another (simpler) way of setting a pointer of type object to a non NULL value?

Please show us your class declaration.

Try the following:
base = new Node(classPtr);

Do not include the Node local variable declaration.
0
 
theakstAuthor Commented:
Ok,

>> You probably want root to be a member of DB.  As shown, it's just a local variable in the constructor.


how do I make root to be a member of DB and not just a local var? (sorry for the simple question)


>>Please show us your class declaration.

which class declaration do you need?
0
 
AxterCommented:
>>which class declaration do you need?

class DB
0
 
AxterCommented:
example

class DB
{
public:
  DB();
private:
  Node *base;
};
0
 
theakstAuthor Commented:
#ifndef _TREEDB_H
#define _TREEDB_H

#include "treeNode.h"

class studentDB {
private:
  Node* root;
  int pCount;
  Node findSmallestRight();

public:
      DB();
      ~DB();

      bool add(class* newRec);
      bool withdraw(unsigned int num, class* searchRecord);
      bool remove(unsigned int num);
      void delete();
      bool Full();
      void print();
}; //DB

#endif

class DB is just the database.  I also have class node & class 'class' (which could (has been)  be renamed record to avoid confusion)
0
 
theakstAuthor Commented:

#include "Node.h" instead of "treeNode.h"
0
 
AxterCommented:
Either you're giving the class the wrong name, or you're giving your constructors and destructors the wrong name.

class studentDB { //You've declared the class name as studentDB

However, you have the following constructor and destructor.

public:
     DB();
     ~DB();

The constructor and destructor must have the same name as the class name.
0
 
AxterCommented:
class studentDB {
private:
  Node* root;

In above line, you'll already have root as a member of the class, so all you need to do is NOT to declare the local variable when you use it in your functions.
0
 
theakstAuthor Commented:

Ok, but how would I make root to be a dynamically allocated member of DB instead of a local var?

For the database to work, I think 'root' has to point to the first node in the BST.

0
 
theakstAuthor Commented:
When de-referenced, it also has to point to a non-NULL value
0
 
AxterCommented:
Try above code first
0
 
theakstAuthor Commented:
Works Beautifully!!

Axter, you're an artist, par excellence!!
0
All Courses

From novice to tech pro — start learning today.