Passing a function with parameters also a function [duplicate] - c

This question already has answers here:
Wrong output while running C code [duplicate]
(8 answers)
Closed 5 years ago.
I came across some problems while doing my assignment. Part of this assignment involves creating a linked-list with the data given and printing them out. The data stored in the list is of datatype 'Atom', which can be an integer, a string, a "pair"(self-defined datatype), or a list made of Nodes.
If the input is (the first integer indicates the number of data)
4
1 2 3 4
it prints
1 2 3 4
What makes me feel strange is that, if I change the function read_list_helper(int n) like this
Node* read_list_helper(int n)
{
Atom *atom;
if (n == 0) return NULL;
return combine_atom_and_list(get_atom(), read_list_helper(n - 1));
}
the output becomes
4 3 2 1
Why would this happen? I have tried several keywords to search for the answer, but none of them works.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define STRING 0
#define NUM 1
#define PAIR 2
#define LIST 3
struct Atom;
typedef struct Pair
{
struct Atom *left;
struct Atom *right;
} Pair;
typedef struct Node
{
struct Atom *data;
struct Node *next;
} Node;
typedef struct Atom
{
int dtype;
int val = 0;
char str[21] = {'\0'};
Pair *pair = NULL;
Node *Node = NULL;
} Atom;
Node* read_list();
Node* read_list_helper(int);
Atom* get_atom();
Node* combine_atom_and_list(Atom*, Node*);
void print_atom(Atom*);
void print_pair(Pair*);
void print_list(Node*);
int main()
{
Node *head1;
head1 = read_list();
print_list(head1);
system("pause");
return 0;
}
Node* read_list()
{
int n;
scanf("%d", &n);
return read_list_helper(n);
}
Node* read_list_helper(int n)
{
Atom *atom;
if (n == 0) return NULL;
atom = get_atom();
return combine_atom_and_list(atom, read_list_helper(n - 1));
}
Atom* get_atom()
{
int num;
char str[21];
int i;
Atom* res = (Atom*)malloc(sizeof(Atom));
if (scanf("%d", &num) == 1)
{
res->dtype = NUM;
res->val = num;
}
else
{
scanf(" %20s", str);
res->dtype = STRING;
strncpy(res->str, str, 20);
}
return res;
}
Node* combine_atom_and_list(Atom* atom, Node* node)
{
Node *res = (Node*)malloc(sizeof(Node));
res->data = atom;
res->next = node;
return res;
}
void print_atom(Atom* atom)
{
if (atom == NULL) return;
switch (atom->dtype)
{
case NUM:
printf("%d", atom->val);
break;
case STRING:
printf("%s", atom->str);
break;
case PAIR:
print_pair(atom->pair);
break;
case LIST:
print_list(atom->Node);
break;
default:
break;
}
}
void print_pair(Pair* pair)
{
print_atom(pair->left);
print_atom(pair->right);
}
void print_list(Node* node)
{
print_atom(node->data);
if (node->next != NULL)
{
printf(" ");
print_list(node->next);
}
}

Function arguments are not evaluated in any particular order. Since one of your arguments does I/O, the order of evaluation affects the order of the inputs.

Related

I am learning data structures and stuck at implementation of queues using linked-list in c

I have written the below piece of code for implementing the queues and their operations(enqueue). The program compiled well with no errors but when the input is given for insertion(enqueue operation) the program stops working and shows a.exe has stopped working
The program contains create_node() function which returns a node, linkedlist initialize function, queue initialize function both of these functions are void functions, and insertion at the end of the linked list function is written which in turn calls the enqueue function.
Note: There's no display function to print all the elements to queue
I think there might be something wrong with initialization functions but I am not sure about it.
#include <stdlib.h>
#include <stdio.h>
typedef struct node node;
struct node
{
int id;
node *link;
};
typedef struct list
{
node *head;
node *tail;
int number_of_nodes;
} List;
typedef struct queue
{
List *ptr_list;
} Queue;
static node *create_node(int id,node *link)
{
node *temp = (node*)malloc(sizeof(node));
temp->id=id;
temp->link=link;
return temp;
}
void list_initialize(List *ptr_list)
{
ptr_list = (List*)malloc(sizeof(List));
ptr_list->head=ptr_list->tail=NULL;
ptr_list->number_of_nodes = 0;
}
void list_insert_rear(List *ptr_list, int id)
{L
node *temp = create_node(id,NULL);
if(ptr_list->tail==NULL)
{
ptr_list->head=ptr_list->tail=NULL;
ptr_list->number_of_nodes++;
}
else
{
ptr_list->tail->link=temp;
ptr_list->tail=temp;
ptr_list->number_of_nodes++;
}
}
void queue_initialize(Queue *queue_list)
{
queue_list = (Queue*)malloc(sizeof(Queue));
list_initialize(queue_list->ptr_list);
}
void queue_enqueue(Queue *ptr, int id)
{
list_insert_rear(ptr->ptr_list,id);
}
int main()
{
Queue queue;
queue_initialize(&queue);
int choice, id, t;
int loop = 1;
while (loop)
{
scanf("%d", &choice);
switch (choice)
{
case 0:
scanf("%d", &id);
queue_enqueue(&queue, id);
break;
default:
loop =0;
break;
}
}
}
This is a problem I actually ran in to a few times and can be tricky to spot. In list_initialize, you should use List **ptr_list.
In the first line of this function, you are changing the pointer, but only in the scope of this variable, so the pointer you put in doesn't actually point to the data anymore, this can be solved by making it a pointer to a pointer. (It's kinda hard to explain)
The function would be (didn't test it):
void list_initialize(List **ptr_list)
{
List *ptr_listnew;
ptr_listnew = (List*)malloc(sizeof(List));
ptr_listnew->head=ptr_listnew->tail=NULL;
ptr_listnew->number_of_nodes = 0;
*ptr_list = ptr_listnew;
}
This is also the case in queue_initialize, which requires the same kind of fix.
You are confusing/combining struct allocation and struct initialization so that your functions do neither well. (See my top comments). As a result, your struct pointers aren't initialized properly and you're getting UB (undefined behavior), most likely a SIGSEGV (segfault).
Although a single function can do both, separating them can give you some insight.
Side note: Don't cast the result of malloc. See: Do I cast the result of malloc?
Here's a refactored version. Original code is marked with #if 0 and new code is marked with #if 1. Bug/fixes are annotated.
#include <stdlib.h>
#include <stdio.h>
typedef struct node node;
struct node {
int id;
node *link;
};
typedef struct list {
node *head;
node *tail;
int number_of_nodes;
} List;
// NOTE/STYLE: use more descriptive names
typedef struct queue {
#if 0
List *ptr_list;
#else
List *queue_list;
#endif
} Queue;
static node *
create_node(int id, node *link)
{
node *temp = (node *) malloc(sizeof(node));
temp->id = id;
temp->link = link;
return temp;
}
void
list_initialize(List *ptr_list)
{
// NOTE/BUG: you are blowing away caller's pointer value
#if 0
ptr_list = malloc(sizeof(List));
#endif
ptr_list->head = ptr_list->tail = NULL;
ptr_list->number_of_nodes = 0;
}
#if 1
List *
list_create(void)
{
List *ptr_list;
ptr_list = malloc(sizeof(List));
list_initialize(ptr_list);
return ptr_list;
}
#endif
void
list_insert_rear(List *ptr_list, int id, int time)
{
node *temp = create_node(id, NULL);
if (ptr_list->tail == NULL) {
ptr_list->head = ptr_list->tail = NULL;
ptr_list->number_of_nodes++;
}
else {
ptr_list->tail->link = temp;
ptr_list->tail = temp;
ptr_list->number_of_nodes++;
}
}
void
queue_initialize(Queue *queue_list)
{
// NOTE/BUG: you are blowing away caller's pointer value
#if 0
queue_list = malloc(sizeof(Queue));
#endif
queue_list->queue_list = list_create();
}
#if 1
Queue *
queue_create(void)
{
Queue *queue_list;
queue_list = malloc(sizeof(Queue));
queue_initialize(queue_list);
return queue_list;
}
#endif
// NOTE/BUG: wrong prototype
#if 0
void
queue_enqueue(Queue *queue, int id)
#else
void
queue_enqueue(Queue *queue, int id, int time)
#endif
{
// NOTE/BUG: not enough arguments for call
#if 0
list_insert_rear(queue->queue_list, id);
#else
list_insert_rear(queue->queue_list, id, time);
#endif
}
int
main(void)
{
Queue queue;
queue_initialize(&queue);
int choice, id, t;
int loop = 1;
#if 1
t = 0;
#endif
while (loop) {
scanf("%d", &choice);
switch (choice) {
case 0:
scanf("%d", &id);
queue_enqueue(&queue, id, t);
++t;
break;
default:
loop = 0;
break;
}
}
#if 1
return 0;
#endif
}

False value passed on binary tree adt lead to wrong output

So i try to make binary tree using array implementation. I just start it but I got a problem. First, this is my struct that I use to build my node for the tree
typedef char infotype;
typedef int letak; //it's the variable to tell the array position number
typedef struct simpul
{
infotype info;
letak parent;
letak rightSon, leftSon;
}elemenSimpul;
typedef struct
{
elemenSimpul treeMember[30]; //my tree have very maximum 30 nodes in it.
int maksimum; //it's the variable to put how many node the user wants and should be under 30
}tree;
here is my btreestatic.h
#ifndef btreestatic_H
#define btreestatic_H
void createTree(tree T);
void createNode(tree T, int i, char value);
void initNode(tree T, int i);
#endif
and for construct the tree I use these at my btreestatic.c
#ifndef btreestatic_C
#define btreestatic_C
#include <stdio.h>
#include <math.h>
#include "btreestatic.h"
void createTree(tree T)
{
T.treeMember[0].parent = -1;
}
void createNode(tree T,int i, char value)
{
int leftchild;
int rightchild;
T.treeMember[i].info = value;
leftchild = 2 * i + 1;
rightchild = 2 * i + 2;
if(leftchild < T.maksimum)
{
T.treeMember[i].leftSon = leftchild;
}
else
{
T.treeMember[i].leftSon = -1;
}
if (rightchild < T.maksimum)
{
T.treeMember[i].rightSon = rightchild;
}
else
{
T.treeMember[i].rightSon = -1;
}
if(i == 0)
{
T.treeMember[i].parent = -1;
}
else
{
T.treeMember[i].parent = (i - 1) / 2;
}
}
void initNode(tree T, int i)
{
char info;
printf("Input node info : ");
scanf(" %c", &info);
createNode(T, i, info);
}
#endif
and on my driver is
#include <stdio.h>
#include <stdlib.h>
#include "btreestatic.h"
int main()
{
tree A;
int maksimum;
int j;
createTree(A);
scanf("%d", &maksimum);
A.maksimum = maksimum;
for(j = 0; j < A.maksimum; j ++)
{
initNode(A, j);
}
printf("%c", A.treeMember[0].info);
return 0;
}
then I try my input as 3 (the maximum of the tree nodes are 3). First node(root) is 'a', second node is 'b', and third node is 'c'. But what i got on my screen is 'd' instead of 'a'. Can anyone tell me where did I go wrong? Thank you.

Split a single linked list into 2 SIngly linked lists -one containing nodes with even data and other containing nodes with odd data

I tried coding the above problem but I'm getting a segmentation error.Below is the code that I've written :
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct oddeven
{
int data;
struct oddeven *link;
};
typedef struct oddeven m;
int main()
{
int z;
m *head=NULL,*ptr,*current;
m *x,*y,*q,*head1=NULL,*current1,*head2=NULL,*current2;
while(1)
{
int ch;
ptr=(m*)malloc(sizeof(m));
printf("Enter the data: ");
scanf("%d",&ptr->data);
ptr->link=NULL;
if(head==NULL)
{
head=ptr;
current=ptr;
}
else
{
current->link=ptr;
current=ptr;
}
printf("Do you want to continue?Y=1/N=0");
scanf("%d",&ch);
if(ch!=1)
break;
}
x=head;
while(x!=NULL)
{
z=x->data;
if(z%2==0)
{
ptr=(m*)malloc(sizeof(m));
ptr->data=z;
ptr->link=NULL;
if(head1==NULL)
{
head1=ptr;
current1=ptr;
}
else
{
current1->link=ptr;
current1=ptr;
}
}
else
{
ptr=(m*)malloc(sizeof(m));
ptr->data=z;
ptr->link=NULL;
if(head2=NULL)
{
head2=ptr;
current2=ptr;
}
else
{
current2->link=ptr;
current2=ptr;
}
}
x=x->link;
}
y=head1;
q=head2;
while (y!=NULL)
{
printf("%d\t",y->data);
y=y->link;
}
printf("\n");
while (q!=NULL)
{
printf("%d\t",q->data);
q=q->link;
}
}
I can't figure out where am I going wrong. Any help would be much appreciated.
It takes the inputs but after that it says segmentation error. Split the given single linked list into two where I can store the odd vales and even values separately .I tried different methods but couldn't get it to work.
#VladfromMoscow mentions one reason why your program faults, but it is too complicated anyway. Here is a simpler version. With a singly linked list, you don't need a separate operation for the first element, when head == NULL. You don't need any new nodes when splitting, just relink.
#include <stdio.h>
#include <stdlib.h>
struct oddeven {
int data;
struct oddeven *link;
};
typedef struct oddeven m;
int main(void) {
int num;
m *head = NULL, *ptr;
// read the list and end it with any letter or non-digit
while(scanf("%d", &num) == 1) {
ptr = malloc(sizeof *ptr);
if(ptr == NULL) {
exit(1);
}
ptr->data = num;
ptr->link = head;
head = ptr;
}
// split the list
m *x, *odd = NULL, *even = NULL, *next;
x = head;
while(x != NULL) {
next = x->link; // remember what the link was
if(x->data % 2 == 0) {
x->link = even;
even = x;
}
else {
x->link = odd;
odd = x;
}
x = next;
}
// output the odds
m *y = odd;
while (y != NULL) {
printf("%d\t", y->data);
y = y->link;
}
printf("\n");
// output the evens
m *q = even;
while (q != NULL) {
printf("%d\t", q->data);
q = q->link;
}
printf("\n");
}
Program session:
1 5 8 3 44 9 0 1 4 3 6 77 3 42 q
1 5 3 9 1 3 77 3
8 44 0 4 6 42

Traversing and printing a queue in C behaves differently when input is given by the user

I apologise if this concept has been explained on SOF before! I believe my case is slightly different and couldn't find a similar question in the website.
Here's the problem:
I'm trying to store char arrays (strings) in a Queue structure that I'm trying to implement.
The structure and its functions seem to work fine when I hardcode the data myself like this:
#include "Time.h"
#include <unistd.h>
int main(void){
struct Queue* q = CreateQueue();
Enqueue(q, "element1");
Enqueue(q, "element2");
Enqueue(q, "element3");
Enqueue(q, "element4");
PrintAll(q->first); // this outputs all elements and the time they've been in the queue.
return 0;
}
The output is as expected, a list of all 4 elements.
However, as soon as I put a simple menu together, to capture the data from the user instead of it being hardcoded as above, the PrintAll() function outputs a duplicate of the very last element enqueued. You also notice that I am timing each node to keep a track on when it was added to the queue and that seem to work fine. Although the ouput shows the last element entered duplicated N times (N being the size of the queue) the timer seems to show correctly for each node!
I am suspecting it's to do with the stdin stream that is not being cleaned but I thought I handled that with a block of code that is shown in main() function.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
char name[31];
char c;
int option;
int ch;
struct Queue* q = CreateQueue();
do
{
printf("\n 1. Add a an element to the queue");
printf("\n 2. Print all elements");
printf("\n 0. Exit");
printf("\n Please select an option");
while(scanf("%d", &option)!=1){
puts("Value non good");
ch=getchar();
while(ch!=EOF && ch!='\n'){
ch=getchar();
}
}
switch(option)
{
case 1:
{
ch=getchar();
while(ch!=EOF && ch!='\n')
{
ch=getchar();
}
printf("Please enter the name of the element.\n ");
fgets(name,30,stdin);
Enqueue(q, name);
PrintAll(q->first);
break;
}
case 2:
{
PrintAll(q->last);
break;
}
default:
return 0;
}
}while(option != 0);
return 0;
}
Can anybody please shed light on the problem ? I would appreciate it.
here's the rest of the code:
Time.c:
#include "Time.h"
struct Queue* CreateQueue()
{
struct Queue* q = malloc(sizeof(struct Queue));
q->first = NULL;
q->last = NULL;
q->size = 0;
return q;
}
void Enqueue(struct Queue* queue, char* string)
{
struct Node* newNode = malloc(sizeof(struct Node));
newNode->next = NULL;
newNode->student = string;
newNode->start_time = time(0);
if(queue->size == 0)
{
queue->first = newNode;
}
else
{
queue->last->next = newNode;
}
queue->last = newNode;
queue->size = queue->size + 1;
}
char* Dequeue(struct Queue* queue)
{
if (queue->size < 0)
{
exit(0);
}
char* toBeRemoved = queue->first->student;
struct Node* oldNode = queue->first;
queue->first = oldNode->next;
queue->size = queue->size - 1;
if(queue->size == 0)
{
queue->last = NULL;
}
free(oldNode);
return toBeRemoved;
}
int IsEmpty(struct Queue *q)
{
return q->size == 0;
}
char* Peek(struct Queue *q)
{
return q->first->student;
}
void PrintOne(struct Node *node)
{
if(node !=NULL)
{
int elapsed = ElapsedTime(node);
printTime(elapsed, node->student);
//printf("%s\n", node->student);
}
}
void PrintAll(struct Node* node)
{
if (node !=NULL)
{
PrintAll(node->next);
PrintOne(node);
}
}
// returns the waiting time for a student node.
int ElapsedTime(struct Node* node)
{
int elapsed;
time_t stop_time;
stop_time = time(NULL);
elapsed = difftime( stop_time , node->start_time );
return elapsed;
}
void printTime(int elapsed, char* student_name)
{
printf("%s : waiting for ", student_name);
int minutes_or_hours = 0; //Stores a zero to indicate that it is not neccesary to print minutes or hours.
//Stores a one to indicate that hours and/or minutes have been printed.
if( (elapsed / 3600) >= 1)
{
int hours = elapsed/3600;
if(hours == 1)
{
printf("1 hour, ");
}
else
{
printf("%d hours, ", hours);
}
elapsed = elapsed - (hours*3600);
minutes_or_hours = 1;
}
if( (elapsed / 60) >= 1)
{
int minutes = elapsed/60;
if(minutes == 1)
{
printf("1 minute, ");
}
else
{
printf("%d minutes, ", minutes);
}
minutes_or_hours = 1;
elapsed = elapsed - (minutes*60);
}
if(minutes_or_hours == 1)
{
printf("and ");
}
printf("%d seconds\n", elapsed);
}
Time.h:
#ifndef TIME_H_
#define TIME_H_
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Node
{
time_t start_time;
struct Node* next;
char* student;
};
struct Queue
{
int size;
struct Node* first;
struct Node* last;
};
struct Queue* CreateQueue();
void Enqueue(struct Queue* , char* );
char* Dequeue(struct Queue* );
int IsEmpty(struct Queue *);
char* Peek(struct Queue *);
void PrintOne(struct Node *);
void PrintAll(struct Node *);
int ElapsedTime(struct Node* );
void printTime(int , char* );
#endif /* TIME_H_ */
In the function Enqueue() you have only copied the string pointer to your structure. In your first case that works, because all the four strings have different pointers to string literals. But in your second example, you are storing the pointer of your data entry name, and the contents of this string change with each entry. Each structure store the same pointer, so all point to the most recent string you typed in. If your struct stored the actual string, it would work (but you need to be careful with string lengths).
struct Node
{
time_t start_time;
struct Node* next;
char student[31];
};
void Enqueue(struct Queue* queue, char* string)
{
...
strncpy (newNode->student, 30, string);
...
}

Creating a binary search tree in C99

I've got a programming class assignment due tonight at 8 PM CDT that I'm having trouble with. We are to take a list of the following numbers via reading a file:
9
30
20
40
35
22
48
36
37
38
place them in an array (easy enough), and then read these into a binary search tree using C. The first number in the list is the number of elements in the tree. The rest are placed into the following struct:
typedef struct node_struct {
int data;
struct node_struct* left;
struct node_struct* right;
} Node;
I think I've got the first part down pat. Take the stuff in using fscanf (I didn't choose to use this method, I like fgets better), call an insertion function on each member of the array, then call a "createNode" function inside the insertion function.
Problem is, I'm only getting one member into the BST. Furthermore, the BST must satisfy the condition node->left->data <= node->data < node->right->data... in other words, the nodes must be in order in the tree.
Here's what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// def BST node struct
typedef struct node_struct {
int data;
struct node_struct* left;
struct node_struct* right;
} Node;
// prototypes
Node* createNode(int data);
Node* bstInsert(Node* root, int data);
// helper function prototypes
void padding(char ch, int n);
void displayTree(Node* root, int depth);
int main(int argc, char **argv)
{
FILE *in = NULL;
int num_read, count=0, array_size = 0;
if(argc != 2){
printf("hw3 <input-file>\n");
return 1;
}
in = fopen(argv[1], "r");
if(in == NULL){
printf("File can not be opened.\n");
return 2;
}
// read in the first line to get the array size
fscanf(in, "%d", &array_size);
// declare the array
int array[array_size];
// read from the second line to get each element of the array
while(!feof(in)){
fscanf(in, "%d", &num_read);
array[count] = num_read;
count++;
}
fclose(in);
if (array_size != count) {
printf("data error. Make sure the first line specifies the correct number of elements.");
return 3;
}
Node *root1 = NULL, *root2 = NULL, *root3 = NULL;
int i;
// task1: construct a bst from the unsorted array
printf("=== task1: construct a bst from the unsorted array ===\n");
for (i = 0; i < array_size; i++) {
root1 = bstInsert(root1, array[i]);
}
displayTree(root1, 0);
return 0;
}
Node* bstInsert(Node* root, int data) {
if(root == NULL){
root = createNode(data);
if(root != NULL){
root= createNode(data);
}
else{
printf("%d not inserted, no memory available.\n", data);
}
}
Node* current, previous, right;
current = root;
previous = root->left;
next = root->right;
else{
if(previous->data <= current->data){
}
}
return root;
}
Node* createNode(int data) {
// TODO
Node* aRoot;
if(!data)
return NULL;
aRoot = malloc(sizeof(Node));
if(!aRoot){
printf("Unable to allocate memory for node.\n");
return NULL;
}
aRoot->data = data;
aRoot->left = NULL;
aRoot->right = NULL;
return aRoot;
}
/* helper functions to print a bst; You just need to call displayTree when visualizing a bst */
void padding(char ch, int n)
{
int i;
for (i = 0; i < n; i++)
printf("%c%c%c%c", ch, ch ,ch, ch);
}
void displayTree(Node* root, int depth){
if (root == NULL) {
padding (' ', depth);
printf("-\n");
}
else {
displayTree(root->right, depth+1);
padding(' ', depth);
printf ( "%d\n", root->data);
displayTree(root->left, depth+1);
}
}
main, createNode, displayTree, and padding are okay, I believe. It's bstInsert where I'm having trouble. I'm just not sure how to order things to create a valid tree.
EDIT:
I've edited bstInsert and injected some more logic. It should be printing out more leaves on the tree, but alas, it's only printing out the number "30". Here's the new function.
Node* bstInsert(Node* root, int data) {
if(root == NULL){
root = createNode(data);
if(root != NULL){
root= createNode(data);
}
else{
printf("%d not inserted, no memory available.\n", data);
}
}
else{
if(data < root->data){
bstInsert(root->left, data);
}
else if(data > root->data || data == root->data){
bstInsert(root->right, data);
}
}
return root;
}
You have to assign the newly created node pointer to the correct part of the tree. This code does that. The key change is using the return value from bstInsert() correctly. The other changes are cosmetic. Note that I checked the input array by printing it out; also, it is sensible to print out the BST as you build it.
Don't use feof() as a loop control condition. It is almost invariably wrong when used as a loop control, but at least you have to also check the input operation that follows. I've written a lot of programs in my time; I've hardly ever used feof() (I found two places in my own code with it; in both, it was correctly used to distinguish between EOF and an error after an input had failed.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// def BST node struct
typedef struct node_struct
{
int data;
struct node_struct* left;
struct node_struct* right;
} Node;
// prototypes
Node *createNode(int data);
Node *bstInsert(Node *root, int data);
// helper function prototypes
void padding(char ch, int n);
void displayTree(Node *root, int depth);
int main(int argc, char **argv)
{
FILE *in = NULL;
int num_read, count=0, array_size = 0;
if (argc != 2)
{
printf("hw3 <input-file>\n");
return 1;
}
in = fopen(argv[1], "r");
if (in == NULL)
{
printf("File can not be opened.\n");
return 2;
}
// read in the first line to get the array size
fscanf(in, "%d", &array_size);
// declare the array
int array[array_size];
// read from the second line to get each element of the array
while (count < array_size && fscanf(in, "%d", &num_read) == 1)
array[count++] = num_read;
fclose(in);
if (array_size != count)
{
printf("data error. Make sure the first line specifies the correct number of elements.");
return 3;
}
for (int i = 0; i < array_size; i++)
printf("%d: %d\n", i, array[i]);
Node *root1 = NULL;
// task1: construct a bst from the unsorted array
printf("=== task1: construct a bst from the unsorted array ===\n");
for (int i = 0; i < array_size; i++)
{
root1 = bstInsert(root1, array[i]);
displayTree(root1, 0);
}
displayTree(root1, 0);
return 0;
}
Node *bstInsert(Node *root, int data)
{
if (root == NULL)
{
root = createNode(data);
if (root == NULL)
printf("%d not inserted, no memory available.\n", data);
}
else if (data < root->data)
root->left = bstInsert(root->left, data);
else
root->right = bstInsert(root->right, data);
return root;
}
Node *createNode(int data)
{
Node *aRoot;
aRoot = malloc(sizeof(Node));
if (!aRoot)
{
printf("Unable to allocate memory for node.\n");
return NULL;
}
aRoot->data = data;
aRoot->left = NULL;
aRoot->right = NULL;
return aRoot;
}
/* helper functions to print a bst; You just need to call displayTree when visualizing a bst */
void padding(char ch, int n)
{
for (int i = 0; i < n; i++)
printf("%c%c%c%c", ch, ch, ch, ch);
}
void displayTree(Node *root, int depth)
{
if (root == NULL) {
padding (' ', depth);
printf("-\n");
}
else {
displayTree(root->right, depth+1);
padding(' ', depth);
printf ( "%d\n", root->data);
displayTree(root->left, depth+1);
}
}
Ok, think about what you want to do in the different tree configurations:
when the tree is empty -> create a root node
when the tree isn't empty -> how do the value to be inserted and the value of the root compare?
above -> insert in the right subtree
below -> insert in the left subtree
equal -> do nothing (this actually depends on how your assignment tells you to treat duplicates)
From this basic algorithm, you should be able to figure out all the corner cases.
A simplified solution (naive insertion with recursion, data input noise removed):
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
static int nums[] = { 6, 8, 4, 1, 3, 7, 14, 10, 13 }; // instead of the user input
typedef struct _node {
int value;
struct _node *left;
struct _node *right;
} node;
node *node_new(int v)
{
node *n = malloc(sizeof(*n));
assert(n);
n->value = v;
n->left = NULL;
n->right = NULL;
return n;
}
void insert(node **tree, node *leaf)
{
if (*tree == NULL) {
*tree = leaf;
} else if (leaf->value > (*tree)->value) {
insert(&((*tree)->right), leaf);
} else {
insert(&((*tree)->left), leaf);
}
}
void dump(node *tree, int level)
{
static const char *pad = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
if (tree != NULL) {
printf("%sSelf: %d\n", pad + 16 - level, tree->value);
if (tree->left) {
printf("%sLeft node:\n", pad + 16 - level);
dump(tree->left, level + 1);
}
if (tree->right) {
printf("%sRight node:\n", pad + 16 - level);
dump(tree->right, level + 1);
}
} else {
printf("%sEmpty\n", pad + 16 - level);
}
}
int main()
{
size_t n = sizeof(nums) / sizeof(*nums);
int i;
node *tree = NULL;
for (i = 0; i < n; i++) {
insert(&tree, node_new(nums[i]));
}
dump(tree, 0);
// give some work to the kernel
return 0;
}
You should consider doing this recursively. Remember that each node is a tree in itself:
#include <stdio.h>
#include <stdlib.h>
typedef struct tree_struct {
int value;
struct tree_struct* left;
struct tree_struct* right;
} Tree;
Tree* addToTree(int value, Tree* tree)
{
if (tree == NULL) {
tree = malloc(sizeof(Tree));
tree->value = value;
tree->left = NULL;
tree->right = NULL;
} else {
if (value < tree->value) {
tree->left = addToTree(value, tree->left);
} else {
tree->right = addToTree(value, tree->right);
}
}
return tree;
}
int main(int argc, char** argv)
{
Tree* tree = NULL;
int in;
while (scanf("%d", &in) != EOF) {
tree = addToTree(in, tree);
}
return 0;
}

Resources