|
1 | | -#include<stdio.h> |
2 | | -#include<conio.h> |
3 | | -#include<stdlib.h> |
4 | | -#include<graphics.h> |
5 | | - |
6 | | -// graphics functions |
7 | | - |
8 | | -struct points |
9 | | -{ |
10 | | - int x; |
11 | | - int y; |
12 | | -}; |
13 | | - |
14 | | -void plotpixel(int x, int y) |
15 | | -{ |
16 | | - putpixel(x + 320, 240 - y, 4); |
17 | | -} |
18 | | - |
19 | | -void dda(int x0, int y0, int x1, int y1) |
| 1 | +#include <graphics.h> |
| 2 | +#include <conio.h> |
| 3 | +#include <math.h> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +// Function that prints Tree using |
| 7 | +// functions graphic.h header file |
| 8 | +void printTree(int x, int y, int* array,int index,int total_elements) |
20 | 9 | { |
21 | | - float dx, dy, steps, xinc, yinc; |
22 | | - setbkcolor(0); |
23 | | - dx = x1 - x0; |
24 | | - dy = y1 - y0; |
25 | | - |
26 | | - steps = ((dx >= dy) ? dx : dy); |
27 | | - |
28 | | - xinc = dx / steps; |
29 | | - yinc = dy / steps; |
30 | | - int x = x0, y = y0; |
31 | | - |
32 | | - for(int i = 1; i <= steps; ++i) |
| 10 | + int left,right; |
| 11 | + char str[10]; |
| 12 | + //Base case of our recursive function |
| 13 | + if (index >= total_elements) |
| 14 | + return; |
| 15 | + |
| 16 | + // Convert int value into string |
| 17 | + itoa(array[index],str,10); |
| 18 | + // Set color of the boundary of |
| 19 | + // circle as green |
| 20 | + setcolor(GREEN); |
| 21 | + // Draw the circle of radius 15 |
| 22 | + // that represent node of Tree |
| 23 | + circle(x, y, 15); |
| 24 | + delay(100); |
| 25 | + floodfill(x, y, GREEN); |
| 26 | + delay(100); |
| 27 | + // Print the values of the node |
| 28 | + // in the circle |
| 29 | + outtextxy(x - 2, y - 3, str); |
| 30 | + delay(100); |
| 31 | + // Set the color of the line |
| 32 | + // from parent to child as green |
| 33 | + setcolor(GREEN); |
| 34 | + // Evaluating left and right child |
| 35 | + left = 2 * index + 1; |
| 36 | + right = 2 * index + 2; |
| 37 | + // Recursively draw the left subtree |
| 38 | + // and the right subtree |
| 39 | + printTree(x - y / (index + 1), y + 50,array, left, total_elements); |
| 40 | + printTree(x + y / (index + 1), y + 50,array, right, total_elements); |
| 41 | + // Draw the line (or link) when the |
| 42 | + // node is not the leaf node |
| 43 | + if (left < total_elements) |
33 | 44 | { |
34 | | - plotpixel(x, y); |
35 | | - x += xinc; |
36 | | - y += yinc; |
| 45 | + delay(100); |
| 46 | + line(x, y, x - y / (index + 1), y + 50); |
37 | 47 | } |
| 48 | + if (right < total_elements) |
| 49 | + { |
| 50 | + delay(100); |
| 51 | + line(x, y, x + y / (index + 1), y + 50); |
| 52 | + } |
| 53 | + return ; |
38 | 54 | } |
39 | | -// graphics functions end; |
40 | | - |
41 | | - |
42 | | - |
43 | | -//queue functions |
44 | | -/* |
45 | | - 1 |
46 | | - 2 3 |
47 | | -4 5 6 7 |
48 | | -*/ |
49 | | -struct node |
50 | | -{ |
51 | | - struct node* next; |
52 | | - struct t_node* data; |
53 | | -}; |
54 | | - |
55 | | -struct node* new_qNode(struct t_node* x) |
56 | | -{ |
57 | | - struct node* temp=(struct node*)malloc(sizeof(struct node)); |
58 | | - temp->next=NULL; |
59 | | - temp->data=x; |
60 | | - return temp; |
61 | | -} |
62 | | -//queue functions end; |
63 | | - |
64 | | - |
65 | | -//trees functions |
66 | | -struct t_node |
67 | | -{ |
68 | | - struct t_node* left; |
69 | | - struct t_node* right; |
70 | | - int data; |
71 | | -}; |
72 | | - |
73 | | -struct t_node* new_tNode(int x) |
74 | | -{ |
75 | | - struct t_node* temp=(struct t_node*)malloc(sizeof(struct t_node)); |
76 | | - temp->left=temp->right=NULL; |
77 | | - temp->data=x; |
78 | | - return temp; |
79 | | -} |
80 | | - |
81 | | -void Inorder(struct t_node* root) |
82 | | -{ |
83 | | - // LNR |
84 | | - if(!root) |
85 | | - return; |
86 | | - Inorder(root->left); |
87 | | - printf("%d ",root->data); |
88 | | - Inorder((root->right)); |
89 | | -} |
90 | | - |
91 | | -void Preorder(struct t_node* root) |
92 | | -{ |
93 | | - // NLR |
94 | | - if(!root) |
95 | | - return; |
96 | | - printf("%d ",root->data); |
97 | | - Preorder(root->left); |
98 | | - Preorder(root->right); |
99 | | -} |
100 | | - |
101 | | -void Postorder(struct t_node* root) |
102 | | -{ |
103 | | - // LRN |
104 | | - if(!root) |
105 | | - return; |
106 | | - Postorder(root->left); |
107 | | - Postorder((root->right)); |
108 | | - printf("%d ",root->data); |
109 | | -} |
110 | | - |
111 | | -/* |
112 | | -Traversing: |
113 | | -1. 1 |
114 | | -2. 2 3 |
115 | | -3. 4 5 |
116 | | -
|
117 | | -In-Order: 4 2 5 1 3 |
118 | | -Pre-Order: 1 2 4 5 3 |
119 | | -Post-Order: 4 5 2 3 1 |
120 | | -*/ |
121 | | -// trees functions end; |
122 | | - |
123 | | - |
124 | | -// GLOBAL DECLARATIONS; |
125 | | -int *a, count; |
126 | | -bool present[10][10]; |
127 | | -struct points *p; |
128 | 55 |
|
129 | 56 | int main() |
130 | 57 | { |
131 | | - printf("\nNOTE: For tree input take -1 as NULL\n\n"); |
132 | | - struct t_node* root = NULL; |
133 | | - int x,lc,rc,count, i = 0, j, k; |
134 | | - a = (int *)malloc(count * sizeof(int)); |
135 | | - p = (struct points *)malloc(count * sizeof(struct points)); |
136 | | - |
137 | | - printf("Enter number of nodes: "); |
138 | | - scanf("%d", &count); |
139 | | - for(int j = 0; j < count; j++) |
140 | | - for(int k = 0; k < count; k++) |
141 | | - present[j][k] = 0; |
142 | | - |
143 | | - printf("\nEnter root node data: "); |
144 | | - scanf("%d",&x); |
145 | | - root=new_tNode(x); |
146 | | - a[i] = x; |
147 | | - p[i].x = 0; |
148 | | - p[i].y = 100; |
149 | | - j = i; |
150 | | - i += 1; |
151 | | - |
152 | | - //queue |
153 | | - struct node* head=new_qNode(root); |
154 | | - struct node* tail=head;//rear pointer |
155 | | - while(head) |
156 | | - { |
157 | | - struct t_node* temp=head->data; |
158 | | - int curr_xco = p[j].x, curr_yco = p[j].y; |
159 | | - |
160 | | - printf("\nEnter left child of %d: ",temp->data); |
161 | | - scanf("%d",&lc); |
162 | | - if(lc!=-1) |
163 | | - { |
164 | | - //tree Linking Nodes |
165 | | - temp->left=new_tNode(lc); |
166 | | - //tree |
167 | | - //queue |
168 | | - tail->next=new_qNode(temp->left); |
169 | | - tail=tail->next; |
170 | | - //queue |
171 | | - |
172 | | - a[i] = lc; |
173 | | - p[i].x = curr_xco - 50; |
174 | | - p[i].y = curr_yco - 50; |
175 | | - k = i; |
176 | | - present[j][k] = 1; |
177 | | - i += 1; |
178 | | - } |
179 | | - |
180 | | - printf("Enter right child of %d: ",temp->data); |
181 | | - scanf("%d",&rc); |
182 | | - if(rc!=-1) |
183 | | - { |
184 | | - //tree linking |
185 | | - temp->right=new_tNode(rc); |
186 | | - //tree |
187 | | - //queue |
188 | | - tail->next=new_qNode(temp->right); |
189 | | - tail=tail->next; |
190 | | - //queue |
191 | | - |
192 | | - a[i] = rc; |
193 | | - p[i].x = curr_xco - 50; |
194 | | - p[i].y = curr_yco - 50; |
195 | | - k = i; |
196 | | - present[j][k] = 1; |
197 | | - i += 1; |
198 | | - } |
199 | | - struct node* t=head; |
200 | | - head=head->next; |
201 | | - free(t); |
202 | | - |
203 | | - j += 1; |
204 | | - } |
205 | | - int c=1,choice; |
206 | | - while(c) |
| 58 | + int gd = DETECT, gm,size=0,i; |
| 59 | + int *array; |
| 60 | + initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); |
| 61 | + printf("Enter the number of nodes: "); |
| 62 | + scanf("%d",&size); |
| 63 | + array=(int*)malloc(sizeof(int)*size); |
| 64 | + for(i=0;i<size;++i) |
207 | 65 | { |
208 | | - printf("0. Visualize Tree \n1.In-order Traversal\n2.Pre-order Traversal\n3.Post-order traversal\n4.Exit\nEnter your choice: "); |
209 | | - scanf("%d",&choice); |
210 | | - switch(choice) |
211 | | - { |
212 | | - case 0: |
213 | | - { |
214 | | - clrscr(); |
215 | | - printf("Creating tree...."); |
216 | | - delay(2000); |
217 | | - c = 0; |
218 | | - break; |
219 | | - } |
220 | | - case 1: |
221 | | - { |
222 | | - printf("In-order Traversal: "); |
223 | | - Inorder(root); |
224 | | - break; |
225 | | - } |
226 | | - case 2: |
227 | | - { |
228 | | - printf("Pre-order Traversal: "); |
229 | | - Preorder(root); |
230 | | - break; |
231 | | - } |
232 | | - case 3: |
233 | | - { |
234 | | - printf("Post-order Traversal: "); |
235 | | - Postorder(root); |
236 | | - break; |
237 | | - } |
238 | | - case 4: |
239 | | - { |
240 | | - c = 0; |
241 | | - break; |
242 | | - } |
243 | | - default: |
244 | | - { |
245 | | - printf("Invalid choice! Please Try again! \n"); |
246 | | - } |
247 | | - } |
248 | | - printf("\n"); |
| 66 | + printf("Enter node %d: ",i+1); |
| 67 | + scanf("%d",(array+i)); |
249 | 68 | } |
250 | | - |
251 | | - // MAIN Graphics code. |
252 | | - int gd = DETECT, gm, i, m, n; |
253 | | - int *s, x1, y1, x2, y2; |
254 | | - char snum[5]; |
255 | | - initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); |
256 | | - cleardevice(); |
257 | | - s =(int *)malloc(count * sizeof(int)); |
258 | | - for(i = 0; i < count; i++) |
259 | | - { |
260 | | - itoa(a[i], snum, 10); |
261 | | - outtextxy(p[i].x + 320, 240 - p[i].y, snum); |
262 | | - s[i] = 0; |
263 | | - } |
264 | | - for(m = 0; m < count; m++) |
265 | | - { |
266 | | - for(n = 0; n < count; n++) |
267 | | - { |
268 | | - if(present[m][n]) |
269 | | - { |
270 | | - x1 = p[m].x; |
271 | | - y1 = p[m].y; |
272 | | - x2 = p[n].x; |
273 | | - y2 = p[n].y; |
274 | | - dda(x1, y1, x2, y2); |
275 | | - } |
276 | | - } |
277 | | - } |
278 | | - getch(); |
279 | | - closegraph(); |
280 | | - |
| 69 | + clrscr(); |
| 70 | + printTree(300, 100, array, 0, size); |
| 71 | + getch(); |
| 72 | + closegraph(); |
281 | 73 | return 0; |
282 | 74 | } |
0 commit comments