0

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;
        }
 }
4

4 回答 4

1
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.

于 2013-03-10T16:26:08.097 回答
1

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);
    }
}
于 2013-03-10T16:27:35.993 回答
0
的有序版本:
  1. 访问左子树。
  2. 打印根的数据。
  3. 访问右子树。
您想要在预购版本中做的是:
  1. 打印根的数据。
  2. 访问左子树。
  3. 访问右子树。

所以它应该是这样的:

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);
        }
    }
}
于 2013-03-10T16:35:43.573 回答
0

我知道这已经多年了,但仍然很有帮助!

我想知道自己该怎么做,发现了这个问题,但意识到有一个简单的答案:

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

你从树的根开始。如果根是null,则代码完成。

否则,打印出该节点的数据。

然后,我们调用该节点Preorderleftandright子树,它现在root是它自己的子树的。重要的是我们首先调用左子树,因为这是前序(后序的右子树)。

我们检查是否rootnull,所以我们不必担心左子树是nullright子树是null单独的。如果节点是null,则回溯。

于 2015-10-10T20:43:50.033 回答