二叉搜索树显示“;预订单”“;顺序”;和“;邮购”;
我想写一个程序,可以建立一个二进制搜索树,并显示二叉搜索树显示“;预订单”“;顺序”;和“;邮购”;,c,binary-search-tree,C,Binary Search Tree,我想写一个程序,可以建立一个二进制搜索树,并显示 “前序”、“顺序”和“后序” 第一个输入是输入序列的数量。从第二行开始,每一行代表一个串行输入,用于构建二元搜索树 输入: 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:
“前序”、“顺序”和“后序” 第一个输入是输入序列的数量。从第二行开始,每一行代表一个串行输入,用于构建二元搜索树 输入:
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限制,i;
字符树_输入[1000];
char*pch;
scanf(“%d”、&limit);
printf(“%d”,限制);
对于(i=0;i你可以这样改变你的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中,以便能够输入多个树
#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;
}
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;
}