-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_queue_stack_reverse_level.c
More file actions
115 lines (109 loc) · 3.4 KB
/
tree_queue_stack_reverse_level.c
File metadata and controls
115 lines (109 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<stdio.h>
#include<conio.h>
#include<stdbool.h>
#define SIZE 500
struct node
{int data;
struct node *left,*right;
};
struct node* newNode(int val)
{struct node *new=(struct node*)malloc(sizeof(struct node));
new->data=val;
new->left=NULL;
new->right=NULL;
return new;
}
void print(struct node* root)
{if(root==NULL)
return;
else
{print(root->left);
printf("%d->",root->data);
print(root->right);
}
}
struct node* insert(struct node* node,int data)
{if(node==NULL)
{return(newNode(data));
}
else
{if(data<=node->data)
node->left=insert(node->left,data);
if(data>node->data)
node->right=insert(node->right,data);
return (node);
}
}
void enQ(struct node* root,struct node** queue,int *rear)
{queue[(*(rear))++]=root;
}
struct node* deQ(struct node** queue,int *front)
{*front=(*front)+1;
return (queue[(*front)-1]);
}
void push(struct node* root,struct node** stack,int *top)
{*top=(*top)+1;
stack[*top]=root;
}
struct node* pop(struct node** stack,int *top)
{*top=(*top)-1;
return stack[(*top)+1];
}
void readStack(struct node** stack,int *top)
{if(*top==-1)
{printf("Stack empty");
return;
}
else
{int i;
printf("\n");
for(i=*top;i>=0;i--)
printf("%d->",*(stack[i]));
}
}
void printLevel(struct node* root,struct node** queue,struct node** stack)
{struct node* temp=root;
int front=0,rear=0;
int top=-1;
while(temp)
{//printf("\n data->%d",temp->data);
push(temp,stack,&top);
//printf("\n front=%d, rear=%d",*front,*rear);
if(temp->left)
{printf("\nroot->left front=%d, rear=%d, data->%d, top->%d",front,rear,temp->left->data,*stack[top]);
enQ(temp->left,queue,&rear);
//push(temp->left,stack,&top);
}
if(temp->right)
{printf("\nroot->right front=%d, rear=%d, data->%d, top->%d",front,rear,temp->right->data,*stack[top]);
enQ(temp->right,queue,&rear);
//push(temp->right,stack,&top);
}
temp=deQ(queue,&front);
}
readStack(stack,&top);
}
int main()
{
struct node *root = NULL;
root=insert(root,10);
root=insert(root,8);
root=insert(root,12);
root=insert(root,6);
root=insert(root,9);
root=insert(root,14);
root=insert(root,11);
root=insert(root,4);
root=insert(root,16);
//print(root);
struct node** queue=(struct node**)malloc(sizeof(struct node*)*10);
struct node** stack=(struct node**)malloc(sizeof(struct node*)*10);
//struct node** stack2=(struct node**)malloc(sizeof(struct node*)*10);
printLevel(root,queue,stack);
/*queue[0]=root;
queue[1]=root->left;
printf("\n root %d",*(queue[0]));
printf("\n %d",*(queue[1])); */
getchar();
return 0;
}