LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   generating k permutations of a series and then generating all possible binary trees from them (https://www.linuxquestions.org/questions/programming-9/generating-k-permutations-of-a-series-and-then-generating-all-possible-binary-trees-from-them-4175639129/)

jamesbon 09-25-2018 08:42 AM

generating k permutations of a series and then generating all possible binary trees from them
 
I am trying to solve a problem , (1) Take a positive integer (say K) as an input from the user. Generate all the K! Binary Search Tree from numbers {1,2,…,K}. i.e. all permutations and then I have to check For each K! BST, check whether that particular BST is full binary tree, complete binary tree and perfect binary tree. Can any one share the code in C or any programming language.What I generated is permutation of all the number of a given series in a file.

Code:

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define N 10
 
void print(int *num, int n)
{
        FILE *fp;
        fp=fopen("result.txt","a");
    int i;
    for ( i = 0 ; i < n ; i++)
//        printf("%d ", num[i]);
          fprintf(fp,"%d ",num[i]);
    fprintf(fp,"\n");
  fclose(fp);
}
int main()
{
    int num[N];
    int *ptr;
    int temp;
    int i, n, j;
    printf("\nHow many number you want to enter: ");
        scanf("%d", &n);
    printf("\nEnter a list of numbers to see all combinations:\n");
    for (i = 0 ; i < n; i++)
        scanf("%d", &num[i]);
    for (j = 1; j <= n; j++) {
        for (i = 0; i < n-1; i++) {
            temp = num[i];
            num[i] = num[i+1];
            num[i+1] = temp;
            print(num, n);
        }
    }
    return 0;
}

but this generates all permutation and stores them in a file result.txt now from that file how do I read and create Binary search tree's that is not clear to me.

NevemTeve 09-25-2018 12:15 PM

What is the goal of this? Testing an existing binary-tree implementation?

dugan 09-25-2018 12:34 PM

Quote:

Originally Posted by NevemTeve (Post 5907665)
What is the goal of this?

It's obviously homework.

Honestly, I'm not sure how someone with the skill on display would be having trouble with either reading the file or creating the tree. You obviously know how to write files, so I assume you know how to read them. And each line is just a single integer, so there's no parsing involved other than atoi.

A typical C binary search tree implementation would involve a Node struct containing data (the integer) and two pointers: one to each child. You then implement the standard algorithms for, say, the insert operation. There are zillions of examples online.

rtmistler 09-25-2018 02:48 PM

That code opens, writes to, and closes the file n times. How ... inefficient.

@dugan,

You're likely correct. I do wonder if the code was also part of the given part of the problem and therefore the question is then the actual assignment.

Their most recent question was more clearly homework: https://www.linuxquestions.org/quest...em-4175636542/

@jamesbon,

There is so very much information about B-tree's on the web. This is a very old programming concept. Please take the time to look a bit further on this subject.

jamesbon 09-27-2018 12:26 AM

Quote:

Originally Posted by dugan (Post 5907673)

You obviously know how to write files, so I assume you know how to read them. And each line is just a single integer, so there's no parsing involved other than atoi.

that assumption is wrong. It is not just atoi there is strtok,fgets,atoi,while + fgets + strtok or strchr + atoi or strtol every thing required here some thing similar to programming a shell in c. I had a deadline which I missed so i could not implement all this in a hurry.

jamesbon 09-27-2018 12:42 AM

Quote:

Originally Posted by rtmistler (Post 5907727)
do wonder if the code was also part of the given part of the problem .

No the code was not given as part of problem it is mostly copy paste work from various sources on internet.I searched permutations program in C and implemented file handling in it (that was my modification) later on found binary search tree code and then merged that with this permutations code , now the only difficulty I am having is in reading line by line from file some thing similar to if (fgets(command, 4097, stdin)!= NULL) {
Code:

            // accept empty lines
            if (!strcmp(command, "\n")) {
                continue;
            }

            // parse the input
            int nwords = 0;
            char *token;
            token = strtok(command, " \t\n");
            while (token != NULL) {
                words[nwords] = token;
                token = strtok(NULL, " \t\n");
                nwords++;
            }
            words[nwords] = NULL;
            if (words[0] == NULL) { // prevent it from segfaulting when spaces are typed
                continue;
            }



All times are GMT -5. The time now is 09:29 PM.