打印表达式树

发布于 2024-12-11 03:11:09 字数 520 浏览 0 评论 0原文

我能够按顺序打印我的表达式树。但我需要能够用括号按顺序打印它。例如 postorder 53+ 应该输出 (5+3)

我目前有:

void printTree(struct TreeNode *tree)
{
   if (tree != NULL)
    {
      if (isalpha(tree->info) || isdigit(tree->info))
        cout << "(";
      printTree( tree->left);
      cout<< tree->info;
      printTree(tree->right);
      if (isalpha(tree->info) || isdigit(tree->info))
        cout << ")";
    }
}

但这给了我不正确的输出。如果我输入后缀表达式 62/5+ 它会给我 (6)/(2)+(5)

:(

I am able to print my expression tree in inorder. But I need to be able to print it in inorder with parenthesis. for example postorder 53+ should output (5+3)

I currently have:

void printTree(struct TreeNode *tree)
{
   if (tree != NULL)
    {
      if (isalpha(tree->info) || isdigit(tree->info))
        cout << "(";
      printTree( tree->left);
      cout<< tree->info;
      printTree(tree->right);
      if (isalpha(tree->info) || isdigit(tree->info))
        cout << ")";
    }
}

But this gives me incorrect output. If i enter postfix expression 62/5+ it gives me (6)/(2)+(5)

:(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后咖啡店 2024-12-18 03:11:09

您需要区分叶节点和非叶节点。只有非叶节点才用括号括起来。

bool isLeaf(struct TreeNode* tree) {
    return tree->left == 0 && tree->right == 0;
}

void printTree(struct TreeNode* tree) {
    if (tree != NULL) { // this test could be omitted if the printed tree is not empty
        if (isLeaf(tree)) {
            cout << tree->info;
        }
        else {
            cout << "(";
            printTree(tree->left);
            cout << tree->info;
            printTree(tree->right);
            cout << ")";            
        }
    }
}

You need to distinguish between leaf nodes and non-leaf nodes. Only non-leaf nodes are enclosed in parantheses.

bool isLeaf(struct TreeNode* tree) {
    return tree->left == 0 && tree->right == 0;
}

void printTree(struct TreeNode* tree) {
    if (tree != NULL) { // this test could be omitted if the printed tree is not empty
        if (isLeaf(tree)) {
            cout << tree->info;
        }
        else {
            cout << "(";
            printTree(tree->left);
            cout << tree->info;
            printTree(tree->right);
            cout << ")";            
        }
    }
}
木緿 2024-12-18 03:11:09

尝试反转决定是否应打印括号的 if 条件:

void printTree(struct TreeNode *tree)
{
    if (tree != NULL)
    {
        if (!(isalpha(tree->info) || isdigit(tree->info)))
            cout << "(";
        printTree( tree->left);
        cout<< tree->info;
        printTree(tree->right);
        if (!(isalpha(tree->info) || isdigit(tree->info)))
            cout << ")";
    }
}

或者可能更好:

void printTree(struct TreeNode *tree)
{
    if (tree != NULL)
    {
        if (isoperator(tree->info))
            cout << "(";
        printTree( tree->left);
        cout<< tree->info;
        printTree(tree->right);
        if (isoperator(tree->info))
            cout << ")";
    }
}

其中适当定义了 isoperator 以区分运算符和操作数。

Try reversing the if conditions that decide whether the parentheses should be printed :

void printTree(struct TreeNode *tree)
{
    if (tree != NULL)
    {
        if (!(isalpha(tree->info) || isdigit(tree->info)))
            cout << "(";
        printTree( tree->left);
        cout<< tree->info;
        printTree(tree->right);
        if (!(isalpha(tree->info) || isdigit(tree->info)))
            cout << ")";
    }
}

Or probably even better :

void printTree(struct TreeNode *tree)
{
    if (tree != NULL)
    {
        if (isoperator(tree->info))
            cout << "(";
        printTree( tree->left);
        cout<< tree->info;
        printTree(tree->right);
        if (isoperator(tree->info))
            cout << ")";
    }
}

where isoperator is appropriately defined to distinguish operators from operands.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文