I'm having trouble adding ("books") nodes to a tree. I have a class (BookRecord) that has a pointer to BookRecord *left and BookRecord *right, they are private but each have a get and set function..get/set left/right()

I am trying to accomplish:
- Add a single node that is passed in (br) to the tree, ordered by its key, a stock number.
- Return TRUE after it works.

Now, the part I am confused about is, how to add a node to the tree. I can print out the info on the nodes that are being passed in with no problem, so they are actually being passed in correctly.

But, when I try temp->setLeft(temp); or temp->setRight(temp) the program crashes, which could be caused by trying to access a NULL pointer.

How would I add a node to this tree??

I can post more code or information if this is too vague but any suggestions would help!

(Also, no, I cant use the STL. This is an exercise in trees so we MUST write it out without using the STL or vectors.)

bool Book_Database::addBook(BookRecord *br)
{
BookRecord *temp;
BookRecord *back;

temp = root;
back = NULL;

if(temp != NULL)
cout << "Current stock number = " << temp->getStockNum() << "\n";
while(temp != NULL) // Loop till temp falls out of the tree
{
back = temp;
//********************# 3 gets stuck here*****************
if(br->getStockNum() < temp->getStockNum()){
temp = temp->getLeft();

}
else {
temp = temp->getRight();
}

if(temp != NULL)
cout << "Current stock number = " << temp->getStockNum() <<"\n";
else
cout << "Temp is now NULL.\n";
}



// Now attach the new node to the node that back points to
if(back == NULL) // Attach as root node in a new tree
root = br;
else
{
if(br->getStockNum() < back->getStockNum())
back->setLeft(br);
else
back->setRight(br);
}
return(true);
}

I usually try to write things down on paper before
trying to write code. In this case I would probably
start with something like this:

Goal:
Create an unbalanced binary tree starting at root
If br value less than root value add br to left child
else add br to right

Ideas:
1) using a recursive function where temp is the current 
node and br is the node to add. Start by passing root 
to add().

void add(temp, br)
{
  If root is NULL 
    assign br to root
  else
    if br value less than temp value
      if temp->left is NULL
        assign br to temp->left
      else
        assign temp->left to temp
        call function again passing temp and br
    else if br value more than temp value
      if temp->right is NULL
        assign bbr to temp->right
      else
        assign temp->right to temp
        call function again passing temp and br
    else //br value equal to temp value
      do nothing
}
//////////////////////////////////////////

2) using iterative approach: br is the node to add
void add(br)
{
  assign root to temp
  assign NULL to back

  If temp == NULL 
    assign br to root
  else
  {
    while temp not NULL
    {
      if br value less than temp value
        if temp->left is NULL
          assign temp to back
          assign NULL to temp
          assign br to back->left
        else
          assign temp->left to temp
      else if br value more than temp value
        if temp->right is NULL
          assign temp to back
          assign NULL to temp
          assign br to back->right
        else
          assign temp->right to temp
      else //br value equals temp value
        assign NULL to temp
    }
  }
}

knowing full well that there's probably some logic
mistake hidden in there someplace, though it should
be a good starting point from which to proceed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.