You are on page 1of 29

LAB 10 BFS graph

typedef int * Graph;


typedef int Vertex;
void print(int *a, int n)
{
int i,j;
for(i=0; i<n; i++){
for (j=0; j<n; j++) printf( "%3d\t", *(a + i*n + j) ); printf("\n"); }
}
Graph createGraph(int numV)
{
int *arr = (int *)malloc(sizeof(int)*numV*numV); int i,j;
for (i=0; i<numV; i++)
for(j=0; j<numV; j++)
*(arr + i*numV + j) = 0;
return arr; }
int * getAdjacent(Graph g,int numV, Vertex v)
{
int i,count = 0,k=0;
for (i=0; i<numV; i++)
if (*(g + (v-1)*numV + i) != 0)
count++;
int *arr = (int*)malloc(sizeof(int)*(count+1));
for (i=0; i<numV; i++)
if (*(g + (v-1)*numV + i) != 0)
arr[k++] = *(g + (v-1)*numV + i) ;
arr[count] = -1; return arr;
// returned array can be printed till -1 is encountered
}

Graph addEdge(Graph g, int numV, Vertex v, Vertex v1)


{
*( g + (v-1)*numV + (v1-1) ) = 1;
return g;
}

int degree(Graph g, int numV, Vertex v)


{
int outdegree=0, indegree=0,i;
for (i=0; i<numV; i++)
if (*(g + (v-1)*numV + i) != 0)
outdegree++;
for (i=0; i<numV; i++)
if (*(g + i*numV + (v-1)) != 0) indegree++;
return indegree + outdegree -1 ; }

struct node
{
int val;
struct node * next;
};
typedef struct node * Node;
struct graph
{
int num;
Node *arr;
int *visited;
};
typedef struct graph Graph;
typedef int Vertex;
void printll(Node n)
{ while(n!= NULL){ printf("%d -> ",n->val); n = n->next; } printf("\n"); }

void print(Graph g)
{ int i=0; for (i=0; i< g.num; i++) printll(g.arr[i]); }
Graph createGraph(int numV)
{
Graph g; g.num = numV; g.arr = (Node *)malloc(sizeof(struct node)* numV);
g.visited = (int *)malloc(sizeof(int)*numV); int i;
for (i=0; i<numV; i++){
g.arr[i] = (Node)malloc(sizeof(struct node));
g.arr[i]->val = i;
g.arr[i]->next = NULL;
g.visited[i] = 0;
}
return g; }

int * getAdjacent(Graph g, Vertex v)


{
// get all elements of linked list with head g.arr[v]
Node temp = g.arr[v]->next;
int count=0,i=0;
while(temp!=NULL){
count++; temp = temp->next; }
temp = g.arr[v]->next;
int *arr = (int*)malloc(sizeof(int)*(count+1));
while(temp!=NULL)
{ arr[i++] = temp->val; temp = temp->next; }
arr[i] = -1; return arr; }

Graph addEdge(Graph g, Vertex v, Vertex v1)


{ if ( v >= g.num || v1 >= g.num )
return g;
Node new_node = (Node)malloc(sizeof(struct node));
new_node->val = v1; new_node->next = NULL;

Node temp = g.arr[v];


while(temp->next != NULL)
{ temp = temp->next; if (temp->val == v1) return g; }
temp->next = new_node; return g;
}

int degree(Graph g, Vertex v)


{
int i,indegree = 0,outdegree = 0;
for (i=0; i<g.num; i++) {
if (i == v){
Node temp = g.arr[i]->next;
while(temp != NULL){
if (temp->val == v)
indegree++;
outdegree++;
temp=temp->next;
}
}
else{
Node temp = g.arr[i]->next;
while(temp!= NULL){
if (temp->val == v)
indegree++;
temp = temp -> next;
}
}
}
return indegree + outdegree;
}
struct que
{ int isEmpty; Node head; Node tail; };
typedef struct que * queue;
queue createQueue() {
queue q = (queue)malloc(sizeof(struct que)); q->head = NULL; q->tail = NULL;
return q; }
queue enqueue(queue q, Vertex v) {
Node new_node = (Node)malloc(sizeof(struct node));
new_node->val = v;
new_node->next = NULL;
if (q->head == NULL && q->tail == NULL){
q->head = q->tail = new_node;
q->isEmpty = 0;
return q;
}
q->tail->next = new_node; q->tail = new_node; return q;
}
int dequeue(queue q) {
Node temp = q->head;
int x = temp->val;
if (q->head == NULL && q->tail == NULL)
return -1;
if (q->head == q->tail)
q->head = q->tail = NULL;
else
q->head = q->head->next;

free(temp);
return x;
}
void BFS(Graph g, Vertex v)
{
int u,level = 0;
queue q = createQueue();
enqueue(q,v);
g.visited[v] = 1;
Node temp;
while ( q->head != NULL )
{
u = q->head->val;
printf("\n%d ",u );
// g.arr[u]
temp = g.arr[u]->next;
while(temp!= NULL) {
if (g.visited[temp->val] == 0) {
g.visited[temp->val] = 1;
enqueue(q,temp->val);
}
temp = temp->next;
}
dequeue(q);
} return; }

LAB 11
typedef struct stack * Stack;
struct stack{
Vertex v;
Node nxt;
Stack next;
};

Stack push(Stack s, Vertex v, Node nxt){


if(v <= -1)
return s;
Stack nd = (Stack)malloc(sizeof(struct stack));
nd->v = v;
nd->nxt = nxt;
nd->next = s;
return nd;
}

Vertex pop(Stack * st, Node * curr){


if(st == NULL || *st == NULL){
*curr = NULL;
return -1;
}
Stack top = *st;
*st = top->next;
*curr = top->nxt;
int v = top->v;
free(top);
return v;
}

Node DFS(Graph g, Vertex s, int * discovered, Node * e){


Node L = NULL, curr;
Vertex v;
int flag;
if(g->adj_list[s] == NULL){//No need to check this
discovered[s] = 1;
L = addf(L,s);
*e = L;
return L;
}
Stack st = push(NULL,s,g->adj_list[s]);
discovered[s] = -1; //discovered temporarily
while(st != NULL){
flag = 0;
v = pop(&st,&curr);
while(curr != NULL){
st = push(st, v, curr->next);
v = curr->v;
if(discovered[v] == -1){//cycle detected
while(st!= NULL)
pop(&st,&curr);
while(L != NULL){
curr = L;
L = L->next;
free(curr);
}
*e = NULL;
return L;
}
if(discovered[v] == 1){
flag = 1;
break;
}
discovered[v] = -1;
curr = g->adj_list[v];
}
if(flag == 1)
continue;
discovered[v] = 1; //discovered permanently
L = addf(L, v);
if(L->next == NULL)
*e = L;
v = pop(&st,&curr);
st = push(st, v, curr);
}
return L;//reverse(L,e);
}

Node topoSort1(Graph g){


if(g == NULL)
return NULL;

int * discovered = (int *)malloc(sizeof(int)*(g->numV));


int i;
for(i = 0; i < g->numV;++i)
discovered[i] = 0;
Node L = NULL, endL = NULL, t, s;
for(i = 0; i < g->numV; ++i){
if(discovered[i])
continue;
if(g->adj_list[i] == NULL){
discovered[i] = 1;
endL = add(endL, i);
if(L == NULL)
L = endL;
continue;
}
t = DFS(g, i, discovered,&s);
if(t == NULL){
while(L != NULL){
s = L;
L = L->next;
free(s);
}
return NULL;
}
s->next = L;
L = t;
}
free(discovered);
return L;
}

typedef struct node * Stack2;

Stack2 push2(Stack2 s, Vertex v){


if(v <= -1)
return s;
Stack2 top = (Stack2)malloc(sizeof(struct node));
top->v = v;
top->next = s;
return top;
}

Vertex pop2(Stack2 * s){


if(*s == NULL || s == NULL)
return -1;
Stack2 t = *s;
Vertex v = t->v;
*s = t->next;
free(t);
return v;
}

Node DFS2(Graph g, Vertex s, int * discovered, Node * e){


Stack2 st = push2(NULL, s);
discovered[s] = -1;
Node start = NULL,temp;
Edge ed;
Vertex v;
while(st != NULL){
v = pop2(&st);
for(ed = g->edg_list; ed != NULL; ed = ed->next)
if(ed->from == v && discovered[ed->to] != 1){
if(discovered[ed->to] == -1){//Cycle detected
while(st != NULL)
pop2(&st);
while(start != NULL){
temp = start;
start = start->next;
free(temp);
}
*e = NULL;
return NULL;
}
if(discovered[ed->to] == 0){
st = push2(st, v);
discovered[ed->to] = -1;
v = ed->to;
ed = g->edg_list;
}
}
start = addf(start, v);
discovered[v] = 1;
if(start->next == NULL)
*e = start;
}
return start;
}

Node topoSort2(Graph g){


if(g == NULL || g->edg_list == NULL)
return NULL;
int * discovered = (int *)malloc(sizeof(int)*(g->numV));
int i;
for(i = 0; i < g->numV;++i)
discovered[i] = 0;
Edge curr;
Node start = NULL, end = NULL,t,s;
for(i = 0; i < g->numV; ++i){
if(discovered[i])
continue;
for(curr = g->edg_list; curr != NULL; curr = curr->next)
if(curr->from == i)
break;
if(curr == NULL){
end = add(end,i);
if(start == NULL)
start = end;
discovered[i] = 1;
continue;
}
t = DFS2(g,i,discovered,&s);
if(t == NULL){
while(start != NULL){
s = start;
start = start->next;
free(s);
}
return NULL;
}
s->next = start;
start = t;
}
free(discovered);
return start;
}

void main(){
FILE * fin = fopen("graph0.txt","r");
if(fin == NULL)
printf("error");
int a1, a2, max = -1, temp, i = 0,j, *en;
Node N = NULL, L;
while(!feof(fin)){
fscanf(fin,"%d %d\n",&a1,&a2);
temp = a1 > a2 ? a1:a2;
if(temp > max)
max = temp;
}
fseek(fin, 0, SEEK_SET);
Graph g = createGraph(++max);
while(!feof(fin)){
fscanf(fin,"%d %d\n",&a1,&a2);
g = addEdge(g,a1,a2);
}
for(i = 0; i < max; ++i){
en = getAdjacent(g,i);
if(en == NULL)
continue;
printf("%d :- ",i);
for(j = 1;j<=en[0];++j)
printf("%d ",en[j]);
printf("\n");
free(en);
}
Edge st = g->edg_list;
printf("\nEdge List : \n");
while(st != NULL){
printf("%d %d\n",st->from, st->to);
st = st->next;
}
// g = addEdge(g,0,5);
printf("\nStarting TopoSort1 : \n");
L = topoSort1(g);
while(L != NULL){
printf("%d ",L->v);
N = L;
L = L->next;
free(N);
}
L = topoSort2(g);
printf("\nStarting TopoSort2 : \n");
while(L != NULL){
printf("%d ",L->v);
N = L;
L = L->next;
free(N);
}

LAB 7

void print(char **s)


{
int i,j;
i = 0;
while(1)
{
if ( strcmp(s[i],"0") == 0 )
break;
for(j=0; j<strlen(s[i]); j++)
printf("%c",s[i][j]);

printf("\n");
i++;
}
}
char *randstring(int length)
{
static int mySeed = 25011984;
char *string = "abcdefghijklmnopqrstuvwxyz";
size_t stringLen = strlen(string);
char *randomString = NULL;
srand(time(NULL) * (length+1) + ++mySeed);
randomString = malloc(sizeof(char) * (length +1));
//randomString = myalloc(sizeof(char) * (length +1));

if (randomString)
{
short key = 0;
int n = 0;
for (n = 0;n < length;n++)
{
key = rand() % stringLen;
randomString[n] = string[key];
}
randomString[length] = '\0';
return randomString;
}
}

char **randstringarray(int length)


{
char **randomStringarray = NULL;
randomStringarray = malloc((length+1)*sizeof(char *));
int i;
for (i=0; i<length; i++)
{
randomStringarray[i] = randstring(length);
}
randomStringarray[i] = randstring(1);
strcpy (randomStringarray[i],"0");
return randomStringarray;
}
int collisioncount(char **s, int slen, int basenum, int tablesize)
{
int i, colcnt, arr[tablesize];
memset(arr,0,tablesize*sizeof(int));
colcnt = 0;

for (i=0; i<slen; i++)


{
int x = hash_f1(s[i],basenum,tablesize);
if (arr[x]==0)
arr[x] = 1;
else
colcnt++;
printf("%s hashes to value %d.\n", s[i], x);
}
printf("%d\n",colcnt );
return colcnt;
}

LAB 7 ques 2 hashtable creation insert


int tablesize, basenum;
struct node
{
int val;
struct node * next;
};
typedef struct node * Node;

struct hasht
{
int elementcount;
Node *arr;
// hasht.arr = (Node)malloc(sizeof(struct node)* tsize);
// above cmd for allocation of tsize array
};
typedef struct hasht hashtable;

int valid(char *buf)


{
int i,l;

l = strlen(buf);
char c;
for(i=0; i<l; i++)
{
c = buf[i];
// printf(" %d ",c );
if ( c<65 || c > 122 || (c<97 && c>90) )
{
//printf("Checking %s \n",buf);
return 0;
}
}
return 1;
}

int hash_f1(char *s, int basenum, int tablesize)


{
int i=0 , sum=0;
while(s[i]!='\0')
sum = sum + s[i++];
return ((sum%basenum)%tablesize);
}

void printll(Node n)
{
printf( YEL );
while(n!= NULL)
{
printf("%d -> ",n->val);
n = n->next;
}
printf("\n");
printf(RESET);
}

void printh(hashtable h, int tablesize)


{
int i;
for(i=0; i<tablesize; i++)
{
printf(BLU "%.2d has \n", i);
printf(RESET);
printf(" ");
printll(h.arr[i]);
}
printf("Done Printing\n\n");
}

char ** file_parser(char *s)


{
FILE *fp = fopen(s,"r");
char **temp_words = NULL;
char **words;
int i,first_non_alpha_flag = 0,flag = 1,count = 0;
char buf[100];
char c ;
while(c != EOF)
{
c = (char)fgetc(fp);
// printf("%d ",c);
if ( c==32 || c==10 || c == EOF)
{
if (first_non_alpha_flag)
{
flag = 1;
buf[i] ='\0';
if (valid(buf))
{
count++;
words = (char **)realloc(temp_words, count*sizeof(char *));
words[count-1] = (char*)malloc(strlen(buf) * sizeof(char));
temp_words = words;
strcpy(words[count-1], buf);
}
first_non_alpha_flag = 0;
}
}
else
{
if ( flag )
{
i = 0;
flag = 0;
}
buf[i++] = c;
first_non_alpha_flag = 1;
}
}
fclose(fp);
words = (char **)realloc(temp_words, (++count)*sizeof(char *));
words[count-1] = (char*) malloc(sizeof(char)*2);
strcpy(words[count-1],"0");
// printf("%d\n", count );
return words;
}

hashtable createhasht(int tablesize)


{
hashtable h;
h.elementcount = 0;
h.arr = (Node *)malloc(sizeof(struct node)* tablesize);
int i;
for (i=0; i<tablesize; i++)
{
h.arr[i] = (Node)malloc(sizeof(struct node));
h.arr[i]->val = -1;
h.arr[i]->next = NULL;

}
return h;
}

void insert(hashtable h, char **book, int index)


{
char s[100]; strcpy(s,book[index]);

int flag = 1, done = 0;


int x = hash_f1(s, basenum, tablesize);
//printf("\t\"%s\" : %d\n", s, x);

Node new_node = (Node)malloc(sizeof(struct node));


new_node->val = index;
new_node->next = NULL;

Node temp_node, prev_node;


temp_node = h.arr[x];
prev_node = NULL;

if ( h.arr[x]->val == -1 )
{
h.arr[x]->val = index;
return;
}
if(strcmp(book[temp_node->val],s) == 0 && temp_node->next == NULL )
{
temp_node->val = index;
return;
}

if ( strcmp(book[temp_node->val],s) == 0 )
{
h.arr[x] = temp_node->next;
while(temp_node->next != NULL)
temp_node = temp_node->next;
temp_node->next = new_node;
return;
}

while(temp_node->next != NULL)
{
if ( strcmp(book[temp_node->val],s) != 0 )
{
if (flag)
prev_node = temp_node;
}
else
flag = 0;
temp_node = temp_node->next;
}

if (strcmp(book[temp_node->val],s) == 0)
{
temp_node->val = index;
return;
}

if (!flag)
{
Node node_to_del = prev_node->next;
prev_node->next = node_to_del->next;
node_to_del->next = NULL ;
free(node_to_del);
}
else
{
temp_node->next = new_node;
return;
}
}

void insertall(hashtable h, char**book)


{
int i =0;
while(strcmp(book[i],"0")!=0)
insert(h, book, i++);
return;
}

int lookup(hashtable h, char **book, char *word)


{
int x = hash_f1(word, basenum, tablesize);
// find string in linked list with starting node h.arr[x]
int querying_cost=0;
Node temp = h.arr[x];

if (temp == NULL)
return querying_cost;

while ( temp != NULL )


{
querying_cost ++;
if ( strcmp(book[x],word)==0 )
return querying_cost;
temp = temp->next;
}
}

void lookupall (hashtable h, char **book, int m)


{
int book_len = 22698, i;
m = (m*22698)/100;

for(i=0; i<m; i++)


printf("lookup cost of \"%s\"\t\t%d\n", book[i], lookup(h,book,book[i]));

LAB 8 trees

struct t_node
{
int val;
struct t_node * left_tree;
struct t_node * right_tree;
int height;
};

typedef struct t_node * tree_node;

int rand_num()
{

return 150 +rand()%21;


}
int max(int x, int y)
{

return ((x>=y) ? x : y);


}

int height(tree_node t)
{

return ( (t==NULL) ? -1 : max( height(t->left_tree), height(t->right_tree) ) + 1);


}

void update_height(tree_node t)
{
if (t==NULL)
return;
update_height(t->left_tree);
t->height = height(t);
update_height(t->right_tree);
}

void printtree(tree_node t)
{
if(t==NULL)
return;
update_height(t);
printtree(t->left_tree);
printf("%d of height %d\n", t->val, t->height);
printtree(t->right_tree);
return;
}

tree_node add(tree_node t1, tree_node t, int num)


{
tree_node temp = (tree_node)malloc(sizeof(struct t_node));
temp->val = num;
temp->left_tree = NULL;
temp->right_tree = NULL;
temp->height = -1;

if ( t == NULL )
{
t = temp;
// update_height(t1);
return t;
}
if (t->val == num)
return t;
else if ( t->val < num )
t->right_tree = add(t1, t->right_tree, num);
else if ( t->val > num )
t->left_tree = add(t1, t->left_tree, num);
}

tree_node addBST(tree_node t1, tree_node t, int num)


{
t1 = add(t1,t,num);
update_height(t1);
return t1;
}

tree_node * get_imbalance_point(tree_node t1,int num)


{
tree_node *temp1 = (tree_node *)malloc(2*sizeof(tree_node));
tree_node temp[2];
temp[0] = t1;
temp[1] = NULL;
temp1[0] = NULL;
temp1[1] = NULL;
while(1)
{
if (height(t1)<=1)
break;
// printf("Node: %d\n", t1->val );
if (abs(height(t1->left_tree)-height(t1->right_tree))>1)
{
temp1[0] = temp[0];
temp1[1] = temp[1];
// printf("Here\n");
// printf("temp1 changed to: %d\n",temp1[0]->val );
}
// if (temp1[0]!= NULL)
// printf("Last Unbalanced: %d\n", temp1[0]->val );
if (t1->val <= num)
{
temp[0] = t1->right_tree;
temp[1] = t1;
t1 = t1->right_tree;
}
else
{
temp[0] = t1->left_tree;
temp[1] = t1;
t1= t1->left_tree;
}
}
// printf("temp1 changed to: %d\n",temp1[0]->val );
return temp1;
}

tree_node find(tree_node t, int num)


{
if (t==NULL)
return t;
else if (t->val==num)
return t;
else if (t->val < num)
find(t->right_tree, num);
else if (t->val > num)
find (t->left_tree, num);
}

tree_node min(tree_node t, int val)


{
if(t->left_tree == NULL)
return t;
else
min(t->left_tree, val);
}

tree_node delete(tree_node t, int num)


{
if (t==NULL)
return t;
if ( t->val > num )
t->left_tree = delete(t->left_tree, num);
else if ( t->val < num )
t->right_tree = delete( t->right_tree, num );
else if (t->val == num )
{
if ( t->right_tree == NULL && t->left_tree ==NULL )
{
t = NULL;
free(t);
}
else if (t->left_tree == NULL)
{
tree_node temp = t;
t = t->right_tree;
temp = NULL;
free(temp);
}
else if (t->right_tree == NULL )
{
tree_node temp = t;
t = t->left_tree;
temp = NULL;
free(temp);
}
else
{
tree_node temp = min(t->right_tree, num);
t->val = temp->val;
t->right_tree = delete(t->right_tree, temp->val);
}
return t;
}
return t;
}

tree_node * left_to_right_list(tree_node x, tree_node y, tree_node z)


{
tree_node *a = (tree_node *)malloc(sizeof(struct t_node)*3);

if ((x->val < y->val && x->val < z->val))


{
a[0] = x;
if (y->val < z->val)
{
a[1] = y;
a[2] = z;
}
else
{
a[1] = z;
a[2] = y;
}
// printf("in rotate: %d %d %d\n",a[0]->val, a[1]->val, a[2]->val );
}
else if ((y->val < x->val && y->val < z->val))
{
a[0] = y;
if (x->val < z->val)
{
a[1] = x;
a[2] = z;
}
else
{
a[1] = z;
a[2] = x;
}
// printf("in rotate: %d %d %d\n",a[0]->val, a[1]->val, a[2]->val );

}
else if ((z->val < y->val && z->val < x->val))
{
a[0] = z;
if (y->val < x->val)
{
a[1] = y;
a[2] = x;
}
else
{
a[1] = x;
a[2] = y;
}
// printf("in rotate: %d %d %d\n",a[0]->val, a[1]->val, a[2]->val );
}
return a;
}

void identify_other_child(tree_node x, tree_node y, tree_node z, tree_node *t)


{
if (z->left_tree->val != y->val)
{
t[0] = z->left_tree;
if (y->left_tree->val != x->val)
{
t[1] = y->left_tree;
t[2] = x->left_tree;
t[3] = x->right_tree;
}
else
{
t[1] = x->left_tree;
t[2] = x->right_tree;
t[3] = y->right_tree;
}
}
else
{
t[3] = z->right_tree;
if (y->left_tree->val != x->val)
{
t[0] = y->left_tree;
t[1] = x->left_tree;
t[2] = x->right_tree;
}
else
{
t[0] = x->left_tree;
t[1] = x->right_tree;
t[2] = y->right_tree;
}
}
}

tree_node rotate( tree_node root, tree_node z_parent, tree_node x, tree_node y, tree_node z)


{
int flag =0;
tree_node t[4];
tree_node a,b,c;
tree_node *arr = left_to_right_list(x,y,z);
a = arr[0]; b= arr[1]; c= arr[2];
free(arr);

identify_other_child(x,y,z,t);
//balancing root
// root->right and z are the same thing but different pointer
// so to change at z change root->right rather than z

if (z_parent == NULL)
{
z = b;
flag = 1;
}
else{
if ( z_parent -> val < z->val)
z_parent->right_tree = b;
else
z_parent->left_tree = b;
}
b->left_tree = a;
b->right_tree = c;
a->left_tree = t[0];
a->right_tree = t[1];
c->left_tree = t[2];
c->right_tree = t[3];

if (flag)
return z;
return root;
}

tree_node addAVL(tree_node t1, tree_node t, int num)


{
// printf("here\n");
t1 = addBST(t1,t,num);
tree_node *t2 = get_imbalance_point(t1,num);
if ( t2[0]!= NULL && t2[1] != NULL)
{
tree_node x, y, z, z_parent;
z = t2[0];
z_parent = t2[1];
if ( num < z->val)
y = z->left_tree;
else
y = z->right_tree;

if ( num < y->val)


x = y->left_tree;
else
x = y->right_tree;

t1 = rotate(t1, z_parent, x, y, z);


}
return t1; }
LAB 9 n ary tree

#define MaxHeight 3
#define N 7
#define size 111111
typedef struct treeNode * Element;
typedef struct iterator * Iterator;
typedef struct treeNode * TreeNode;
typedef int Boolean;

int uarr[size];
int index;

void initialize(){
int i, temp, j;
for(i = 0; i < size; ++i)
uarr[i] = i;
for(i = 0; i < size; ++i){
j = rand()%size;
temp = uarr[j];
uarr[j] = uarr[i];
uarr[i] = temp;
}
index = 0;
}

int getUniqueNum(){
if(index == size)
index = 0;
return uarr[index++];
}

struct treeNode{
int key;
Iterator it;
};

struct iterator{
Element * arr;
int pos;
int max;
};

Boolean hasMoreElements(Iterator it){


if(it == NULL || it->pos == it->max)
return 0;
return 1;
}

Element getNextElement(Iterator it){


if(it == NULL || it->pos == it->max) //if(!hasMoreElements(it)
return NULL;
return ((it->arr)[(it->pos)++]);
}

TreeNode createTree(int n, int depth){


TreeNode root = (TreeNode)malloc(sizeof(struct treeNode));
root->key = getUniqueNum();
if(depth == MaxHeight || n == 0){
root->it = NULL;
return root;
}
root->it = (Iterator)malloc(sizeof(struct iterator));
root->it->arr = (Element *)malloc(sizeof(Element)*n);
root->it->pos = 0;
root->it->max = n;
int i;
for(i=0;i<n;++i)
(root->it->arr)[i] = createTree(rand()%N, depth+1);
return root;
}

typedef struct setN * SetN;


struct setN{
int depth;
int n;
SetN next;
TreeNode nd;
};

SetN addN(SetN s, TreeNode nd, int depth, int n){


SetN end = (SetN)malloc(sizeof(struct setN));
end->depth = depth;
end->nd = nd;
end->n = n;
end->next = NULL;
if(s != NULL)
s->next = end;
return end;
}

SetN popN(SetN s){


if(s == NULL)
return NULL;
SetN t = s;
s = s->next;
free(t);
return s;
}

TreeNode icreateTree(){
int depth = 0;
int n = rand()%N, i, m;
TreeNode root = (TreeNode)malloc(sizeof(struct treeNode));
root->key = getUniqueNum();
TreeNode nd = root;
if(n == 0 || depth == MaxHeight){
root->it = NULL;
return root;
}
SetN s = addN(NULL, nd, depth, n), end = s;
while(s != NULL){
nd = s->nd;
depth = s->depth;
n = s->n;
nd->it = (Iterator)malloc(sizeof(struct iterator));
nd->it->arr = (Element *)malloc(sizeof(Element)*n);
nd->it->pos = 0;
nd->it->max = n;

for(i = 0; i < n; ++i){


nd->it->arr[i] = (TreeNode)malloc(sizeof(struct treeNode));
nd->it->arr[i]->key = getUniqueNum();
m = rand()%N;
if(m == 0 || depth == MaxHeight-1){
nd->it->arr[i]->it = NULL;
continue;
}
end = addN(end, nd->it->arr[i],depth+1,m);
}
s = popN(s);
}

return root;
}

typedef struct stack * Stack;


struct stack{
TreeNode nd;
int pos;
int depth;
Stack next;
};

Stack pushN(Stack s, TreeNode nd, int pos, int depth){


Stack top = (Stack)malloc(sizeof(struct stack));
top->nd = nd;
top->pos = pos;
top->depth = depth;
top->next = s;
return top;
}

Stack popN_st(Stack s){


if(s == NULL)
return NULL;
Stack t = s;
s = s->next;
free(t);
return s;
}

TreeNode icreateTree2(){
int depth = 0;
int m = rand()%N, i;
TreeNode root = (TreeNode)malloc(sizeof(struct treeNode));
root->key = getUniqueNum();
TreeNode nd = root;
if(m == 0 || depth == MaxHeight){
root->it = NULL;
return root;
}
nd->it = (Iterator)malloc(sizeof(struct iterator));
nd->it->arr = (Element *)malloc(sizeof(Element)*m);
nd->it->pos = 0;
nd->it->max = m;
Stack s = pushN(NULL, nd, 0, depth);
while(s != NULL){
nd = s->nd;
i = s->pos;
depth = s->depth;
s = popN_st(s);
if(i < (nd->it->max)-1)
s = pushN(s, nd, i+1, depth);

while(depth++ < MaxHeight){


(nd->it->arr)[i] = (TreeNode)malloc(sizeof(struct treeNode));
(nd->it->arr)[i]->key = getUniqueNum();
m = rand()%N;
if(m == 0 || depth == MaxHeight){
(nd->it->arr)[i]->it = NULL;
break;
}
(nd->it->arr)[i]->it = (Iterator)malloc(sizeof(struct iterator));
(nd->it->arr)[i]->it->arr = (Element *)malloc(sizeof(Element)*m);
(nd->it->arr)[i]->it->pos = 0;
(nd->it->arr)[i]->it->max = m;
if(m > 1)
s = pushN(s,(nd->it->arr)[i],1,depth);
nd = (nd->it->arr)[i];
i = 0;
}
}

return root;
}

Boolean isEmptyTree(TreeNode t){


if(t == NULL)
return 1;
return 0;
}

int getRootVal(TreeNode t){


return t->key;
}

Iterator getChildren(TreeNode t){


return t->it;
}

typedef struct set* Set;


struct set{
TreeNode nd;
Set next;
};

Set addMoreNodes(Set currentSet, Iterator it){


if(it == NULL)
return currentSet;
if(currentSet == NULL){
currentSet = (Set)malloc(sizeof(struct set));
currentSet->nd = getNextElement(it);
currentSet->next = NULL;
}
while(hasMoreElements(it)){
currentSet->next = (Set)malloc(sizeof(struct set));
currentSet = currentSet->next;
currentSet->nd = getNextElement(it);
}
currentSet->next = NULL;
it->pos = 0;
return currentSet;
}

Set push(Set s, TreeNode nd){


if(nd == NULL)
return s;
Set n = (Set)malloc(sizeof(struct set));
n->nd = nd;
n->next = s;
return n;
}

TreeNode pop(Set * s){


if(s == NULL || *s == NULL)
return NULL;
TreeNode nd = (*s)->nd;
Set top = *s;
*s = (*s)->next;
free(top);
return nd;
}

Boolean isEmptySet(Set s){


return (s == NULL);
}

void BFT(TreeNode root){


Set s = (Set)malloc(sizeof(struct set));
s->nd = root;
s->next = NULL;
Set end = s, curr1, curr2;
int i = 0;
while(!isEmptySet(s)){
curr2 = end;
printf("Level %d : ",i);
while(s != curr2->next){
printf("%d ",(s->nd)->key);
end = addMoreNodes(end, (s->nd)->it);
curr1 = s;
s = s->next;
if(curr1 == curr2){
free(curr1);
break;
}
free(curr1);
}
printf("\n");
++i;
}
}
void resetIt(TreeNode nd, int depth){
if(depth == MaxHeight || nd->it == NULL || nd->it->max == 0)
return;
nd->it->pos = 0;
int i;
for(i=0; i< nd->it->max; ++i)
resetIt((nd->it->arr)[i], depth+1);
}
void postDFT(TreeNode root){
Set s = push(NULL,root);
TreeNode nd, child;
while(!isEmptySet(s)){
nd = pop(&s);
child = getNextElement(nd->it);
while(child != NULL){
s = push(s, nd);
nd = child;
child = getNextElement(child->it);
}
printf("%d ",nd->key);
} resetIt(root,0); }
void preDFT(TreeNode root){
Set s = push(NULL, root);
Set s2 = NULL;
TreeNode nd, child;
while(!isEmptySet(s)){
nd = pop(&s);
child = getNextElement(nd->it);
while(child != NULL){
while(hasMoreElements(nd->it))
s2 = push(s2, getNextElement(nd->it));
while(!isEmptySet(s2))
s = push(s,pop(&s2));
printf("%d ",nd->key);
nd = child;
child = getNextElement(child->it);
}
printf("%d ",nd->key);
}
resetIt(root,0);
}

void inDFT(TreeNode root){


Set s = push(NULL, root);
Set s2 = NULL;
TreeNode nd, child;
while(!isEmptySet(s)){
nd = pop(&s);
child = getNextElement(nd->it);
while(child != NULL){
while(hasMoreElements(nd->it))
s2 = push(s2, getNextElement(nd->it));
while(!isEmptySet(s2))
s = push(s,pop(&s2));
s = push(s, nd);
nd = child;
child = getNextElement(child->it);
}
printf("%d ",nd->key);

}
resetIt(root,0);
}

void inDFT_R(TreeNode root){


if(root == NULL)
return;
inDFT_R(getNextElement(root->it));
printf("%d ",root->key);
while(hasMoreElements(root->it))
inDFT_R(getNextElement(root->it));
}
void postDFT_R(TreeNode root){
if(root == NULL)
return;
while(hasMoreElements(root->it))
postDFT_R(getNextElement(root->it));
printf("%d ",root->key);
}

void preDFT_R(TreeNode root){


if(root == NULL)
return;
printf("%d ",root->key);
while(hasMoreElements(root->it))
preDFT_R(getNextElement(root->it));
}

You might also like