I need help with the following program:
Create linear representation of stack with size N. Stack contains N binary search trees with serial number of the position on the stack from 1 to N and with the minimum p and maximum q nodes in a tree. Values p and q are entered as command line arguments, and for each tree, the number of nodes in interval [p,q] is randomly generated. Information of a tree's node is generated number in interval [0,9999]. Criterion for adding new node in a tree is by the value of generated number in interval [0,9999] (ascending order). In main, create stack and add N trees in it with generated nodes. Print serial number of the tree which has the largest sum of it's nodes (integers). Sum is checked while removing the content from stack and before removing a tree.

The following code gives allocation errors:

``````#include <stdio.h>
#include <stdlib.h>
#define N 2

typedef struct node
{
int info;
struct node *left,*right;
}NODE;

typedef struct stack
{
NODE arr[N];
int tos;
}STACK;

int isFull(STACK *s)
{
return s->tos == N-1;
}

int isEmpty(STACK *s)
{
return s->tos == -1;
}

int push(STACK *s,NODE info)
{
if (isFull(s))
return 0;
s->arr[++s->tos]=info;
return 1;
}

{
printf("Positive integer: ");
scanf("%d",&nd->info);
}

NODE* search(NODE *root,int info)
{
if(root == 0)
return 0;
else if(info == root->info)
return root;
else if(info <= root->info)
return search(root->left,info);
else return search(root->right,info);
}

NODE* formNode(int info)
{
NODE *newNode=(NODE*)malloc(sizeof(NODE));
newNode->left=newNode->right=0;
newNode->info=info;
return newNode;
}

{
if(root == 0)
return formNode(info);
if(info <= root->info)
return root;
}

NODE* emptyTheStack(STACK *s)
{
if(isEmpty(s))
return 0;
int i=0;
NODE *arr=(NODE*)malloc(((s->tos+1) * sizeof(NODE))*N);
while(!isEmpty(s))
arr[i++]=s->arr[s->tos--];
return arr;
}

void deleteTree(NODE *root)
{
if(root != 0)
{
deleteTree(root->left);
deleteTree(root->right);
free(root);
}
}

void printInorder(NODE *root)
{
if(root != 0)
{
printInorder(root->left);
printf("%d",root->info);
printInorder(root->right);
}
}

int sum(NODE *root)
{
if(root == 0)
return 0;
return root->info+sum(root->left)+sum(root->right);
}

int main()
{
printf("Adding N trees on the stack.\n");
int i;
NODE *root=0;
NODE nd;
STACK s;
int info;
char c;
NODE *res;
int nodeCounter=0;

int p,q;
do
{
printf("p = ");
scanf("%d",&p);
}
while(p < 1);
do
{
printf("q = ");
scanf("%d",&q);
}
while(q < 1);
if(p > q)
{
int temp=p;
p=q;
q=temp;
}

for(i=1;i<=N;i++)
{
jump:

do{
fflush(stdin);
printf("Enter option: ");
scanf("%c",&c);
if(c=='A'){
printf("Enter data: \n");
NODE *n=search(root,nd.info);
if(n)
{
n->info=info;
nodeCounter++;
if(push(&s,nd))
printf("Node of %d. tree is added to the stack.\n");
if(isFull(&s))
printf("Stack is full.\n");
}
else
{
nodeCounter++;
if(push(&s,nd))
printf("Node of %d. tree is added to the stack.\n");
if(isFull(&s))
printf("Stack is full.\n");
}
}
else if(c=='E')
{
printf("Empty the stack.\n");
res=emptyTheStack(&s);
if(isEmpty(&s))
printf("Stack is empty.\n");
}
else if(c=='S')
{
//print serial number of a tree with the largest sum
}
else if(c!='0')
printf("Unknown option.\n");
}
while(c!='0');

if(nodeCounter < p || nodeCounter > q)
goto jump;
}

return 0;
}
``````

Could someone point out where are the errors?