奪萃u 2023-06-26 17:14 采纳率: 0%
浏览 11

线索二叉树输出有问题

这个代码输出有问题,但是我问了很多同学也找不出来
这是输出结果
Pre-order Traversal: 1 2 4 5 3 6 7
In-order Traversal: 4 2 5 1 6 3 7
Post-order Traversal: 5
后序的只有一个数字


#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *left, *right;
    int isThreaded;
} Node;

Node *newNode(int data) {
    Node *temp = (Node *)malloc(sizeof(Node));
    temp->data = data;
    temp->left = temp->right = NULL;
    temp->isThreaded = 0;
    return temp;
}

void inOrderTraversal(Node *root) {
    if (!root) return;

    Node *current = root;
    while (current) {
        if (!current->left) {
            printf("%d ", current->data);
            current = current->right;
        } else {
            Node *pre = current->left;
            while (pre->right && pre->right != current) {
                pre = pre->right;
            }

            if (!pre->right) {
                pre->right = current;
                current = current->left;
            } else {
                pre->right = NULL;
                printf("%d ", current->data);
                current = current->right;
            }
        }
    }
}

void preOrderTraversal(Node *root) {
    if (!root) return;

    Node *current = root;
    while (current) {
        if (!current->left) {
            printf("%d ", current->data);
            current = current->right;
        } else {
            Node *pre = current->left;
            while (pre->right && pre->right != current) {
                pre = pre->right;
            }

            if (!pre->right) {
                printf("%d ", current->data);
                pre->right = current;
                current = current->left;
            } else {
                pre->right = NULL;
                current = current->right;
            }
        }
    }
}

void postOrderTraversal(Node* root) {
    if (!root) return;

    Node* dummy = newNode(-1);
    dummy->left = root;

    Node* current = dummy;
    while (current) {
        if (!current->left) {
            current = current->right;
        }
        else {
            Node* pre = current->left;
            while (pre->right && pre->right != current) {
                pre = pre->right;
            }

            if (!pre->right) {
                pre->right = current;
                current = current->left;
            }
            else {
                // Reverse the nodes from current->left to pre
                Node* first = current->left;
                Node* middle = first->right;
                Node* last;
                while (middle != current) {
                    last = middle->right;
                    middle->right = first;
                    middle->isThreaded = 1; // set the isThreaded flag to 1
                    first = middle;
                    middle = last;
                }

                // Print the reversed nodes
                first = pre;
                while (first != current->left) {
                    printf("%d ", first->data);
                    first = first->right;
                }

                // Re-reverse the nodes to restore the tree
                first = current->left;
                middle = first->right;
                while (middle != current) {
                    last = middle->right;
                    middle->right = first;
                    middle->isThreaded = 0; // set the isThreaded flag to 0
                    first = middle;
                    middle = last;
                }

                pre->right = NULL;
                current = current->right;
            }
        }
    }

    free(dummy);
}

int main() {
    Node *root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->left = newNode(6);
    root->right->right = newNode(7);

    printf("Pre-order Traversal: ");
    preOrderTraversal(root);
    printf("\nIn-order Traversal: ");
    inOrderTraversal(root);
    printf("\nPost-order Traversal: ");
    postOrderTraversal(root);
    printf("\n");

    return 0;
}

  • 写回答

1条回答 默认 最新

报告相同问题?

问题事件

  • 创建了问题 6月26日

悬赏问题

  • ¥15 asp写PC网站开通了微信支付,扫码付款不能跳转
  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 关于#c语言#的问题:我在vscode和codeblocks中编写c语言时出现打不开源文件该怎么办
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥500 服务器搭建cisco AnyConnect vpn
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊