0

I am coding to print out BST preorder and postorder traversals.

The class tree is defined like this

class BinSearchTree{
  char symbol;
  BinSearchTree  *lChild;
  BinSearchTree  *rChild;

 public:
 BinSearchTree(char letter) { symbol = letter;lChild = NULL; rChild = NULL;}
  BinSearchTree() { symbol = '0';}
  BinSearchTree* buildTree(BinSearchTree *tree, char letter);
  void printTreePreOrder (BinSearchTree *temp, std::ofstream &fp1);
  void printTreeInOrder (BinSearchTree *temp, std::ofstream &fp1);
};

I have used a simple recursion to create the BST

BinSearchTree* BinSearchTree::buildTree(BinSearchTree *tree, char letter){
  if (tree == NULL) {
    tree = new BinSearchTree (letter); 
    return tree;
  }
  else {
        if (letter<(tree->symbol))
        {
                tree->lChild = (BinSearchTree*) buildTree(tree->lChild, letter);
                return tree;
        }
        else{
                tree->rChild = (BinSearchTree*) buildTree(tree->rChild, letter);
                return tree;
        }
      }
    return tree;
}

But when I print out the traversal, I am getting segmentation faults. I use this piece of code for preorder and something similar for postorder

void BinSearchTree::printTreePreOrder (BinSearchTree *temp, std::ofstream &fp1) {
    if (temp == NULL){
        return;
    }
    else{
        fp1 << symbol << " ";
        printTreePreOrder(lChild, fp1);
        printTreePreOrder(rChild, fp1);
    }
}

In my main code, I create my tree using

T = new BinSearchTree(str[0]);
    for(i=1; i<num; i++){
        fp >> str[i];
        T->buildTree(T,str[i]);
    }

and do traversal using

T->printTreePreOrder(T,fp1)

I have been trying to figure out the error since days and I think am committing a silly error. Any help is appreciated.

PS - Working on Ubuntu 14.04 and using G++ compiler.

1 Answers1

0

First you need to decide where do you want the traversal algorithm to be, in the class or out of the class. Now the method is belong to the class but receive a parameter of the type of the class (this seem odd, and in this particular case are wrong).

void BinSearchTree::printTreePreOrder (BinSearchTree *temp, std::ofstream &fp1);

The correct signature should be:

void BinSearchTree::printTreePreOrder(std::ofstream &fp1) in this case you are iterating using the actual node as root the code would be something like:

void BinSearchTree::printTreePreOrder(std::ofstream &fp1) {
    fp1 << symbol << " ";
    if (lChild != NULL)
        printTreePreOrder(lChild, fp1);
    if (rChild != NULL)
        printTreePreOrder(rChild, fp1);
}

Or if the function is out of the class:

void printTreePreOrder(BinSearchTree *temp, std::ofstream &fp1) {
    if (temp == NULL)
        return ;
    else {
        fp1 << temp->symbol << " ";
        if (temp->lChild != NULL)
            printTreePreOrder(temp->lChild, fp1);
        if (temp->rChild != NULL)
            printTreePreOrder(temp->rChild, fp1);
    }
}

In this case you would need access to the members symbol, lChild and rChild in the actual implementation are private (or declare the method as friend).

Your problem is you are miss matching parameters and member variables, eg:

In your call to the printTreePreOrder method: T->printTreePreOrder(T,fp1) in this case temp would be T, lChild and rChild would be the child's of T, there is no problem here, let suppose that the two child exist, you print symbol of T and call printTreePreOrder(lChild, fp1); in this call temp is equal to lChild, but lChild and rChild are already refering to the member variables of T, and this is the problem.

In your case is probably better to be in class for not declaring the members variables public or friend.

It seem odd and wrong the return of the method buildTree (it have the same problem as printThreePreOrder. You don't have to need returning anything, the new data is inserted in the actual node or forwarded to one of the child, not need to return anything:

Code would be something like (in class version):

void BinSearchTree::buildTree(char letter){
    if (letter < tree->symbol) {
        if (lChild == NULL)
            lChild = new BinSearchTree(letter);
        else
            lChild->buildTree(letter);
    } else {
        if (rChild == NULL)
            rChild = new BinSearchTree(letter);
        else
            rChild->buildTree(letter);
    }
}
NetVipeC
  • 4,402
  • 1
  • 17
  • 19
  • Thanks, this solves the problem of the segmentation fault which I was getting. But the output is still wrong, is there an error in the way I create the tree? I used the second approach that you suggested in which the function is out of the class. – user3706865 Sep 25 '14 at 12:43
  • Updated answer, see new recommendations. – NetVipeC Sep 25 '14 at 13:08