通过输入整数的排列生成所有可能的二叉树,这些整数从文件

时间:2018-09-26 14:36:49

标签: c algorithm file struct binary-tree

我正在尝试从给定输入生成所有可能的二叉树,例如,如果输入为1,2,3,4,则此输入将有24个排列 例如

2       4       3       1
2       4       1       3
3       2       1       4
3       2       4       1
3       1       2       4
3       1       4       2
3       4       1       2
3       4       2       1
4       2       3       1
4       2       1       3

我将这些排列存储在文件result.txt中,我已经分别编写了程序来创建一个将这两个程序结合在一起的二叉树,最后的代码如下所示

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

struct btnode
{
    int value;
    struct btnode *l;
    struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;

void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);

void printarray(int arr[],int size);

int flag = 1;
/* To insert a node in the tree */
void insert()
{
    create();
    if (root == NULL)
        root = temp;
    else
        search(root);
}

/* To create a node */
void create()
{
    int data;

    printf("Enter data of node to be inserted : ");
    scanf("%d", &data);
    temp = (struct btnode *)malloc(1*sizeof(struct btnode));
    temp->value = data;
   temp->value = data;
    temp->l = temp->r = NULL;
}

/* Function to search the appropriate position to insert the new node */
void search(struct btnode *t)
{
    if ((temp->value > t->value) && (t->r != NULL))      /* value more than root node value insert at right */
        search(t->r);
    else if ((temp->value > t->value) && (t->r == NULL))
        t->r = temp;
    else if ((temp->value < t->value) && (t->l != NULL))    /* value less than root node value insert at left */
        search(t->l);
    else if ((temp->value < t->value) && (t->l == NULL))
        t->l = temp;
}

/* recursive function to perform inorder traversal of tree */
void inorder(struct btnode *t)
{
   if (root == NULL)
    {
        printf("No elements in a tree to display");
        return;
    }
    if (t->l != NULL)
        inorder(t->l);
    printf("%d -> ", t->value);
    if (t->r != NULL)
        inorder(t->r);
}

/* To check for the deleted node */
void delete()
{
    int data;

    if (root == NULL)
    {
        printf("No elements in a tree to delete");
        return;
  }
    printf("Enter the data to be deleted : ");
    scanf("%d", &data);
    t1 = root;
    t2 = root;
    search1(root, data);
}

/* To find the preorder traversal */
void preorder(struct btnode *t)
{
    if (root == NULL)
    {
        printf("No elements in a tree to display");
        return;
    }
    printf("%d -> ", t->value);
    if (t->l != NULL)
        preorder(t->l);
    if (t->r != NULL)
        preorder(t->r);
}

/* To find the postorder traversal */
void postorder(struct btnode *t)
{
    if (root == NULL)
    {
        printf("No elements in a tree to display ");
        return;
    }
    if (t->l != NULL)
        postorder(t->l);
    if (t->r != NULL)
        postorder(t->r);
    printf("%d -> ", t->value);
}

/* Search for the appropriate position to insert the new node */
void search1(struct btnode *t, int data)
{
    if ((data>t->value))
    {   t1 = t;
        search1(t->r, data);
    }
    else if ((data < t->value))
    {
        t1 = t;
        search1(t->l, data);
    }
    else if ((data==t->value))
    {
        delete1(t);
    }
}

/* To delete a node */
void delete1(struct btnode *t)
{
    int k;

    /* To delete leaf node */
  /* To delete leaf node */
    if ((t->l == NULL) && (t->r == NULL))
    {
        if (t1->l == t)
        {
            t1->l = NULL;
        }
        else
        {
            t1->r = NULL;
        }
        t = NULL;
        free(t);
        return;
    }

    /* To delete node having one left hand child */
    else if ((t->r == NULL))
    {
        if (t1 == t)
        {
            root = t->l;
      t1 = root;
        }
        else if (t1->l == t)
        {
            t1->l = t->l;

        }
        else
        {
            t1->r = t->l;
        }
        t = NULL;
        free(t);
        return;
    }

    /* To delete node having right hand child */
    else if (t->l == NULL)
    {
        if (t1 == t)
        {
     root = t->r;
            t1 = root;
        }
        else if (t1->r == t)
            t1->r = t->r;
        else
            t1->l = t->r;
        t == NULL;
        free(t);
        return;
    }

    /* To delete node having two child */
    else if ((t->l != NULL) && (t->r != NULL))
    {
        t2 = root;
        if (t->r != NULL)
        {
            k = smallest(t->r);
            flag = 1;
        }
   else
        {
            k =largest(t->l);
            flag = 2;
        }
        search1(root, k);
        t->value = k;
    }

}

/* To find the smallest element in the right sub tree */
int smallest(struct btnode *t)
{
    t2 = t;
    if (t->l != NULL)
    {
        t2 = t;
        return(smallest(t->l));
    }
    else
      return (t->value);
}

/* To find the largest element in the left sub tree */
int largest(struct btnode *t)
{
    if (t->r != NULL)
    {
        t2 = t;
        return(largest(t->r));
    }
    else
        return(t->value);
}




//function to print the array
void printarray(int arr[], int size)
{
    FILE *fp;
    int i,j;
    fp=fopen("result.txt","a");
    for(i=0; i<size; i++)
    {
          //  printf("%d\t",arr[i]);
      fprintf(fp,"%d\t",arr[i]);
    }
    fprintf(fp,"\n");
    fclose(fp);
}

//function to swap the variables
void swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

//permutation function
void permutation(int *arr, int start, int end)
{
    if(start==end)
    {
        printarray(arr, end+1);
        return;
    }
    int i;
    for(i=start;i<=end;i++)
    {
        //swapping numbers
        swap((arr+i), (arr+start));
        //fixing one first digit
        //and calling permutation on
        //the rest of the digits
        permutation(arr, start+1, end);
        swap((arr+i), (arr+start));
    }
}

int main()
{
    //taking input to the array
    int size;
    printf("Enter the size of array\n");
    scanf("%d",&size);
    int i;
    int arr[size];
    for(i=0;i<size;i++)
        scanf("%d",&arr[i]);
    //calling permutation function

    permutation(arr, 0, size-1);
    return 0;
}

我被困在主要功能中,我无法理解的是我的results.txt文件包含排列输出为

3       4       2       1
4       2       3       1
4       2       1       3

为了能够创建树,我需要从文件result.txt中逐行读取,每一行都会生成一棵单独的树,我想在stdout上显示它。 现在文件太长了,所有输入排列都通过\n与以前的输入排列分开,因此如果我对文件进行fgets操作,该如何继续读取

while (fgets(line,sizeof(line),fp)){
        create_tree(line);<-- this function is not in above code I will implement just to give an idea of my logic of implementation

我无法理解如何在行用户中分隔输入值,而在启动时可能会给定变量序列,因此sscanf在这里不是解决方案,因为在每次执行程序时,用户都可能会这样执行

Enter the size of array
5

,输入的序列为1 2 3 4 5 在另一个执行中,用户可以像

一样执行
Enter the size of array
4

,输入的序列为1 2 3 4

我面临的另一个问题是多次执行result.txt第一次应该被覆盖,但我找不到文件打开模式或足够的方式。但是我可以通过实现是否存在文件来摆脱这种情况然后删除,但上面的问题逐行读取变量选择中的参数是我从文件中遇到的问题。如何实现呢?

1 个答案:

答案 0 :(得分:0)

这里似乎有多个问题。我将专注于这一部分:

  

我无法理解如何分隔输入值...

据我了解,您有一行(从文件中读取)具有可变数量的数字,并且您想解析该行以获得每个单独的数字。

下面是显示如何完成此操作的示例。为简单起见,我对用户/文件输入进行了硬编码。

#include <stdio.h>

#define MAX 10

int parseLine(char *line, int *arr )
{
    int j = 0;
    int x = 0;
    int t;
    while(j < MAX && sscanf(line + x, "%d%n", &arr[j], &t) == 1)
    {
        x += t;
        ++j;
    }
    return j;
}

int main(void) {
    int size = 5;                    // User input
    char line[] = "10 20 30 40 50";  // Line from file

    int numbers[MAX];
    if (size > MAX)
    {
        printf("Too many numbers!\n");
        return 0;
    }
    int count = parseLine(line, numbers);
    if (count != size)
    {
        printf("Wrong number found!\n");
        return 0;
    }
    printf("Found %d numbers in the line\n", count);
    for(int t=0; t<count; ++t) printf("%d\n", numbers[t]);

    // Build tree from the "count" number in the array

    return 0;
}

输出:

Found 5 numbers in the line
10
20
30
40
50