main.cpp
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
// Contributed by Huzaifa Ahmad(softwaresinn) // Libraries used:graphic.h(Borland Graphics) #include<cstdio> #include<iostream> #include<conio.h> #include<malloc.h> #include<string.h> #include<cstring> #include<string> #include"graphics.h" #include<cstdlib> #include<math.h> using namespace std; void initgraph(int *graphdriver, int *graphmode, char *pathtodriver); #define YINC 60 #define F getmaxx() #define G getmaxy() int midx, anim; //Nodes of the tree struct node { int data; struct node *left; struct node *right; }*root = NULL; //Helper function to create new nodes struct node *newNode(int data) { struct node *node = (struct node*)malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } void displayData(int data, int x, int y) { //Converting integer to string int n = data, i = 0, j = 0; char str[10]; while (n) { str[i++] = n % 10 + '0'; n /= 10; str[i] = '\0'; } for (i--; j<i; j++, i--) { str[i] += str[j]; str[j] = str[i] - str[j]; str[i] -= str[j]; } setcolor(WHITE); outtextxy(x - 5, y, str); settextstyle(DEFAULT_FONT,HORIZ_DIR,2); setcolor(WHITE); } void drawLine(int X0, int Y0, int X1, int Y1) { int i; //calculate dx & dy int dx = X1 - X0; int dy = Y1 - Y0; //calculate steps required for generating pixels int steps = abs(dx)>abs(dy) ? abs(dx) : abs(dy); //calculateincrementin x&y for each steps float Xinc = dx / (float)steps; float Yinc = dy / (float)steps; //Putpixelforeachstep float X = X0; float Y = Y0; for (i = 0; i <= steps; i++) { putpixel(X, Y, getcolor()); X += Xinc; Y += Yinc; if (anim) delay(30); } } void drawCircle(int xc, int yc, int x, int y) { putpixel(xc + x, yc + y, getcolor()); putpixel(xc - x, yc + y, getcolor()); putpixel(xc + x, yc - y, getcolor()); putpixel(xc - x, yc - y, getcolor()); putpixel(xc + y, yc + x, getcolor()); putpixel(xc - y, yc + x, getcolor()); putpixel(xc + y, yc - x, getcolor()); putpixel(xc - y, yc - x, getcolor()); } //Functionforcircle-generation //using Bresenham's algorithm void circleBres(int xc, int yc, int r) { int x = 0, y = r; int d = 3 - 2 * r; while (y >= x) { //foreachpixelwewill //drawalleightpixels drawCircle(xc, yc, x, y); x++; //checkfordecisionparameter //andcorrespondingly //updated,x,y if (d>0) { y--; d = d + 4 * (x - y) + 10; } else d = d + 4 * x + 6; drawCircle(xc, yc, x, y); if (anim) delay(60); } } void drawTree(struct node*ptr, char LorR, int depth, int xc, int yc, int xc1, int yc1) { if (LorR == '\0') circleBres(xc1, yc1,30); else if (LorR == 'L') { //Calculation for edges int len, x_diff, x1, y1, x2, y2; x_diff = abs(xc1 - xc); len = sqrt(pow(x_diff, 2) + pow(YINC, 2)); x1 = 25*x_diff / len; y1 = 25*YINC / len; x2 = xc1 + x1; y2 = yc1 - y1; x1 = xc - x1; y1 = yc + y1; drawLine(x1, y1, x2, y2); circleBres(xc1, yc1, 25); } else { //Calculationforedges int len, x_diff, x1, y1, x2, y2; x_diff = abs(xc1 - xc); len = sqrt(pow(x_diff, 2) + pow(YINC, 2)); x1 = 25*x_diff / len; y1 = 25*YINC / len; x2 = xc1 - x1; y2 = yc1 - y1; x1 = xc + x1; y1 = yc + y1; drawLine(x1, y1, x2, y2); circleBres(xc1, yc1, 25); } //Display data inside the circle displayData(ptr->data, xc1, yc1); } void calPos(int xc, int yc, int *xc1, int *yc1, int depth, char LorR) { int i = 1, x = midx; //Calculatingpositionfornextchild for (; (i++) <= depth; x /= 2); if (xc == -1); else if (LorR == 'L') { *xc1 = xc - x; *yc1 = yc + YINC; } else if (LorR == 'R') { *xc1 = xc + x; *yc1 = yc + YINC; } } int calTree(struct node*ptr, char LorR, int depth, int xc, int yc, int xc1, int yc1) { calPos(xc, yc, &xc1, &yc1, depth, LorR); if (ptr != NULL) { drawTree(ptr, LorR, depth, xc, yc, xc1, yc1); calTree(ptr->left, 'L', depth + 1, xc1, yc1, xc1, yc1); } else return 0; calTree(ptr->right, 'R', depth + 1, xc1, yc1, xc1, yc1); } struct node*insert(struct node*ptr, char LorR, int depth, int xc, int yc, int xc1, int yc1, int data) { calPos(xc, yc, &xc1, &yc1, depth, LorR); if (ptr == NULL) { ptr = newNode(data); drawTree(ptr, LorR, depth, xc, yc, xc1, yc1); return ptr; } if (data == ptr->data) { puts("Duplicate elements are not allowed"); delay(3000); return ptr; } else if (data<ptr->data) ptr->left = insert(ptr->left, 'L', depth + 1, xc1, yc1, xc1, yc1, data); else ptr->right = insert(ptr->right, 'R', depth + 1, xc1, yc1, xc1, yc1, data); return ptr; } struct node*minValue(struct node*temp) { while (temp->left != NULL) temp = temp->left; return temp; } struct node*del(struct node*ptr, char LorR, int depth, int xc, int yc, int xc1, int yc1, int data) { struct node*temp; calPos(xc, yc, &xc1, &yc1, depth, LorR); if (ptr == NULL) { cout << "Invalid Value"; delay(3000); return ptr; } if (data<ptr->data) ptr->left = del(ptr->left, 'L', depth + 1, xc1, yc1, xc1, yc1, data); else if (data>ptr->data) ptr->right = del(ptr->right, 'R', depth + 1, xc1, yc1, xc1, yc1, data); else { setcolor(BLACK); if (ptr->left == NULL) { temp = ptr->right; drawTree(ptr, LorR, depth, xc, yc, xc1, yc1); free(ptr); return temp; } else if (ptr->right == NULL) { temp = ptr->left; drawTree(ptr, LorR, depth, xc, yc, xc1, yc1); free(ptr); return temp; } drawTree(ptr, LorR, depth, xc, yc, xc1, yc1); temp = minValue(ptr->right); ptr->data = temp->data; ptr->right = del(ptr->right, 'R', depth + 1, xc1, yc1, xc1, yc1, temp->data); } return ptr; } void boundary(int j, int k); // Main int main() { int gdriver = DETECT, gmode,errorcode; int data; char cmnd[5]; system("cls"); initgraph(&gdriver, &gmode, ""); midx = getmaxx() / 2; while (1) { anim = 1; cin >> cmnd; if (strcmp(cmnd, "ins") == 0) { cin >> data; if (root == NULL) root = insert(root, '\0', 0, -1, -1, midx, 30, data); else insert(root, '\0', 0, -1, -1, midx, 30, data); } else if (strcmp(cmnd, "del") == 0) { cin >> data; if (root == NULL) cout << "Tree is already empty" << endl; else root = del(root, '\0', 0, -1, -1, midx, 30, data); } else if (strcmp(cmnd, "root") == 0) { cout << endl << root->data; delay(3000); } else if (strcmp(cmnd, "exit") == 0) { cleardevice(); setbkcolor(15); boundary(8, 5); setcolor(5); settextstyle(3, 0, 5); outtextxy(130, 110, "EXIT"); settextstyle(3, 0, 4); outtextxy(110, 180, "EXIT"); delay(2000); closegraph(); exit(0); } else { cout << "Not a Valid Command" << endl; delay(3000); } setcolor(WHITE); system("cls"); cleardevice(); anim = 0; calTree(root, '\0', 0, -1, -1, midx, 30); } system("pause>0"); return 0; } //boundary animations void boundary(int j, int k) { int i, r = 10; for (i = 10; i<getmaxx(); i++) { setcolor(j); circle(10, i, r); circle(getmaxx() - r, i, r); setcolor(k); circle(i, 10, r); circle(i, getmaxy() - r, r); delay(1); } } |