xlw12
xlw12

Reputation: 781

print trees in preorder separated by commas

I have following function to print trees in in order that works properly:

void PrintInOrder(TTreeNode const * const pRoot) {
    if (pRoot != 0) {
        PrintInOrder(pRoot->pLeft);
        if(pRoot->pLeft) std::cout << ",";
        std::cout << pRoot->Data;
        if(pRoot->pRight) std::cout << ",";
        PrintInOrder(pRoot->pRight);
    }
}

This is my preorder printing function:

void PrintPreOrder(TTreeNode const * const pRoot)  {
    if (pRoot != 0) {
        std::cout << pRoot->Data << std::endl;
        PrintPreOrder(pRoot->pLeft);
        PrintPreOrder(pRoot->pRight);
    }
}

As i'm too stupid to figure it out how to print it separated the way like the inorder function i hope you guys can help me out!

thanks!

Update:

Preorder function works now, so is this the right postorder function?

void PrintPostOrder(TTreeNode const * const pRoot)  {
        if (pRoot != 0) {
            PrintPostOrder(pRoot->pLeft);
            if(pRoot->pLeft) std::cout << ",";
            PrintPostOrder(pRoot->pRight);
            if(pRoot->pRight) std::cout << ",";
            std::cout << pRoot->Data;
        }
 }

Upvotes: 0

Views: 2297

Answers (4)

Matthew Stromberg
Matthew Stromberg

Reputation: 51

I know this is years old, but still helpful!

I was wondering how to do this myself, and found this question, but realize there is an easy answer:

void Preorder(Node* root){
  if(root==NULL){
    return;
  }
  cout << root->data << " ";
  Preorder(root->left);
  Preorder(root->right);
}

You're starting with the root of the tree. If the root is null, the code is done.

Otherwise, print out the data at that node.

Then, we call Preorder on the left and right subtree for that node, which is now the root of its own subtree. It is important we call the left subtree first, as this is preorder (right subtree for post order).

We check if the root is null, so we don't have to worry if the left subtree is null or the right subtree is null individually. If the node is null, just backtrack.

Upvotes: 0

LihO
LihO

Reputation: 42083

Your in-order version:

  1. Visits the left subtree.
  2. Prints the root's data.
  3. Visits the right subtree.
What you want to do in pre-order version is:
  1. Print the root's data.
  2. Visit the left subtree.
  3. Visit the right subtree.

so it should look like this:

void PrintPreOrder(TTreeNode const * const pRoot) {
    if (pRoot) {

        // print the root:
        std::cout << pRoot->Data;
        
        // visit the left subtree:
        if (pRoot->pLeft) {
            std::cout << ",";
            PrintPreOrder(pRoot->pLeft);
        }
        
        // visit the right subtree:
        if (pRoot->pRight) {
            std::cout << ",";
            PrintPreOrder(pRoot->pRight);
        }
    }
}

Upvotes: 0

meyumer
meyumer

Reputation: 5064

This is the proper way to do it:

void PrintPreOrder(TTreeNode const * const pRoot)  {
    if (pRoot != 0) {
        std::cout << pRoot->Data << std::endl;
        // take care of left node
        if(pRoot->pLeft || pRoot->pRight) std::cout << ",";
        PrintPreOrder(pRoot->pLeft);
        // take care of right node
        if(pRoot->pLeft && pRoot->pRight) std::cout << ",";
        PrintPreOrder(pRoot->pRight);
    }
}

Upvotes: 1

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

void PrintPreOrder(TTreeNode const * const pRoot)  {
    if (pRoot != 0) {
        std::cout << pRoot->Data << std::endl;
        if(pRoot->pLeft || pRoot->pRight) std::cout << ",";
        PrintPreOrder(pRoot->pLeft);
        if(pRoot->pLeft && pRoot->pRight) std::cout << ",";
        PrintPreOrder(pRoot->pRight);
    }
}

Also please note this will print the tree in preorder traversal.

Upvotes: 1

Related Questions