C语言 一般树->二叉树->打印(数据结构)

rsaldnfx  于 2023-03-28  发布在  其他
关注(0)|答案(1)|浏览(122)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable: 4996)

typedef struct node {
    int level;
    int key;
    int child_key;
    struct node* left;
    struct node* right;
} Node;

Node* root = NULL;

// function to create a new node
Node* newNode(int level, int key, int child_key) {
    Node* node = (Node*)malloc(sizeof(Node));
    node->level = level;
    node->key = key;
    node->child_key = child_key;
    node->left = NULL;
    node->right = NULL;
    return node;
}

// read the general tree input
void input(Node* node, int level, int key, int child_key) {
    if (node->level == level) {
        if (node->right == NULL) {
            node->right = newNode(level, key, child_key);
            return;
        }
        else 
            input(node->right, level, key, child_key);
    }
    else {
        if (node->child_key == key) {
            node->left = newNode(level, key, child_key);
            return;
        }
        else {
            if (root->left == NULL)
                input(node->right, level, key, child_key);
            else
                input(node->left, level, key, child_key);
        }
    }
}

// function to traverse a binary tree in preorder and print the keys of the nodes visited
void traversePreorder(Node* root) {
    if (root != NULL) {
        printf("%d ", root->key);
        traversePreorder(root->left);
        traversePreorder(root->right);
    }
}

int main() {
    int level, key, child_key;
    char child[5];

    while (1) {
        printf("Please input data following format\n");
        printf("(if you want to stop, input = 0 0 0)\n");
        printf("(tree-level, key, child_key) -> 1 100 75 : ");
        scanf("%d %d %s", &level, &key, child);
     
        if (level == 0)
            break;

        if (strcmp(child ,"NULL\n")==0)
            child_key = 0;
        else 
            child_key = atoi(child);

        if (level == 1) {
            root = newNode(level, key, child_key);
        }
        else
            input(root, level, key, child_key);
        printf("\n");
    }
    printf("[Preorder traversal result]\n");
    traversePreorder(root);

    return 0;
}

你知道为什么当我输入(3 25 NULL)时程序退出吗?
这是我的作业。所以,我花了很多时间来解决这个问题,但我不能这样做。

输入:(1 100 200)(2 200 NULL)(2 75 25)(2 300 NULL)(3 25 NULL)(3 50 NULL)(3 30 120)(3 150 NULL)(4 120 NULL)(4 55 NULL)
输出:100 200 75 300 25 50 30 120 55 150

hmmo2u0o

hmmo2u0o1#

我想你的意思是node而不是input()中的root

void input(Node* node, int level, int key, int child_key) {
    if (node->level == level)
        if (node->right)
            input(node->right, level, key, child_key);
        else
            node->right = newNode(level, key, child_key);
    else
        if (node->child_key == key)
            node->left = newNode(level, key, child_key);
        else
            if (node->left)
                input(node->left, level, key, child_key);
            else
                input(node->right, level, key, child_key);
}

和example run(注意,这不是预期的输出,但它不再崩溃):

$ echo -e "1 100 200\n2 200 NULL\n2 75 25\n2 300 NULL\n3 25 NULL\n3 50 NULL\n3 30 120\n3 150 NULL\n4 120 NULL\n4 55 NULL\n0 0 0" | ./a.out
100 200 75 25 50 30 120 55 150 300

相关问题