LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2018, 08:42 AM   #1
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Rep: Reputation: 9
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.

Last edited by jamesbon; 09-25-2018 at 08:44 AM.
 
Old 09-25-2018, 12:15 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,851
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
What is the goal of this? Testing an existing binary-tree implementation?
 
Old 09-25-2018, 12:34 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,198

Rep: Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307
Quote:
Originally Posted by NevemTeve View Post
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.

Last edited by dugan; 09-25-2018 at 07:36 PM.
 
Old 09-25-2018, 02:48 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,876
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 09-27-2018, 12:26 AM   #5
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Original Poster
Rep: Reputation: 9
Quote:
Originally Posted by dugan View Post

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.
 
Old 09-27-2018, 12:42 AM   #6
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Original Poster
Rep: Reputation: 9
Quote:
Originally Posted by rtmistler View Post
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;
            }

Last edited by jamesbon; 09-27-2018 at 12:47 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Oracle SQL - generating permutations of data between two tables. devnull10 Programming 1 09-07-2011 12:33 AM
[SOLVED] generating series of numbers of sequence vjramana Programming 7 03-19-2011 10:34 AM
i need a linux programe for generating fibonacci series vinki Linux - Newbie 5 09-02-2009 04:21 PM
generating the map file for binary image steve_e Linux - Software 0 05-15-2006 05:45 AM
Generating A Script gizza23 Linux - Software 7 07-24-2005 07:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration