二进制搜索树,显示“预定”,“预定”和“预定”

时间:2018-12-10 00:37:56

标签: 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: -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;
}

1 个答案:

答案 0 :(得分: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;
 }

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

一些要点:

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

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

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