gpt4 book ai didi

c - 二叉搜索树显示 “Preorder” 、 “Inorder” 和 “Postorder”

转载 作者:行者123 更新时间:2023-11-30 19:28:38 24 4
gpt4 key购买 nike

我想编写一个程序,可以构建二叉搜索树并显示
“预序”、“中序”和“后序”。

第一个输入是输入系列的数量。从第二行开始,每一行代表一个串行输入,用于构建二叉搜索树。

输入:

3
9,5,6,7,1,8,3
22,86,-5,8,66,9
45,3,5,3,8,6,-8,-9

输出:

Preorder: 9 5 1 3 6 7 8
Inorder: 1 3 5 6 7 8 9
Postorder: 3 1 8 7 6 5 9
Preorder: 22 -5 8 9 86 66
Inorder: -5 8 9 22 66 86
Postorder: 9 8 -5 66 86 22
Preorder: 45 3 3 -8 -9 5 8 6
Inorder: -9 -8 3 3 5 6 8 45
Postorder: -9 -8 3 6 8 5 3 45

这是我的代码来检测字母(,)

int limit,i;
char tree_input[1000];
char *pch;
scanf("%d",&limit);
printf("%d",limit);

for(i=0; i<limit; i++){
scanf("%s",tree_input);
pch = strtok (tree_input,",");
while (pch != NULL){
printf ("%s\n",pch);
pch = strtok (NULL, ",");
}
}

我的想法是使用“pch”作为树

然后我不知道下一步。这是我到目前为止的全部代码。

这是my code

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

struct node
{
int data;
struct node *left;
struct node *right;
};

struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}

/* A utility function to insert a new node with given key in BST */
struct node* insert(struct node* node, int data)
{
/* If the tree is empty, return a new node */
if (node == NULL) return newNode(data);

/* Otherwise, recur down the tree */
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);

/* return the (unchanged) node pointer */
return node;
}


void preorder(struct node* root)
{
if(root == NULL)
return;
printf("%d", root->data);
preorder(root->left);
preorder(root->right);
}

void inorder(struct node* root)
{
if(root == NULL)
return;
inorder(root->left);
printf("%d", root->data);
inorder(root->right);
}

void postorder(struct node* root)
{
if(root == NULL)
return;
postorder(root->left);
postorder(root->right);
printf("%d", root->data);
}

//void change(char a) //change char to int
//{
//
// for (char ch = '0'; ch <= '9'; ++ch) {
// printf("%d\t", ch - '0');
// }
//}

int main()
{
int limit,i;
char tree_input[1000];
char *pch;
scanf("%d",&limit);
printf("%d",limit);

for(i=0; i<limit; i++)
{
scanf("%s",tree_input);
pch = strtok (tree_input,",");

// struct node *root = NULL;
// root = insert(root, 50);
// insert(root, 30);
// insert(root, 20);
// insert(root, 40);
// insert(root, 70);
// insert(root, 60);
// insert(root, 80);

struct node *root = NULL;
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, ",");
printf("%d\n")
// if (root == NULL)
// {
// root = insert(root, pch);
// }
}
}
return 0;
}

最佳答案

您可以从像这样更改 main 开始,然后从那里开始工作。

int main()
{
int limit,i, j;
char *pch;

struct node* tree;

printf("Enter the length of the string you will use: ");
scanf("%d", &limit); // number of keys and delimiters in initial string
char str[limit]; // array containing the input string

scanf("%s",str);
printf("%s",str);

/* get the first token */
pch = strtok(str, ",");

/* walk through other tokens */
while( pch != NULL ) {
printf("\n%d", atoi(pch) ); // atoi turns an alphanumeric (char) to an integer
insert(tree, atoi(pch) ); // insert the integer to the tree

pch = strtok(NULL, ",");
}

printf("\n");
printf("Preorder: ");
preorder(tree);

printf("\n");
printf("Postorder: ");
postorder(tree);

printf("\n");
printf("Inorder: ");
inorder(tree);
printf("\n");

return 0;
}

当然,你绝对不能定义像“char str[limit];”这样的数组,你应该使用malloc。不过,我不确定您是否熟悉 malloc,因此为了清楚起见,我避免使用它。即使您不是,我建议您阅读并使用它(即使在网上也有很多资源)。

一些要点:

1) 要使用 strtok(),您需要包含 string.h

2) atoi 用于将字母数字转换为整数。由于您最初有一个字符串并且想要将整数输入到树中,因此您应该将字符转换为整数

3) 您需要将 main 中的一些代码放入循环中,以便能够输入多棵树。

关于c - 二叉搜索树显示 “Preorder” 、 “Inorder” 和 “Postorder”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53698134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com