|
3 | 3 | #include <math.h>
|
4 | 4 | #include <stdlib.h>
|
5 | 5 |
|
| 6 | +//queue functions |
| 7 | +struct node |
| 8 | +{ |
| 9 | + struct node* next; |
| 10 | + struct t_node* data; |
| 11 | +}; |
| 12 | + |
| 13 | +struct node* new_qNode(struct t_node* x) |
| 14 | +{ |
| 15 | + struct node* temp=(struct node*)malloc(sizeof(struct node)); |
| 16 | + temp->next=NULL; |
| 17 | + temp->data=x; |
| 18 | + return temp; |
| 19 | +} |
| 20 | +//queue functions |
| 21 | + |
| 22 | + |
| 23 | +//trees function |
| 24 | +struct t_node |
| 25 | +{ |
| 26 | + struct t_node* left; |
| 27 | + struct t_node* right; |
| 28 | + int data; |
| 29 | +}; |
| 30 | + |
| 31 | +struct t_node* new_tNode(int x) |
| 32 | +{ |
| 33 | + struct t_node* temp=(struct t_node*)malloc(sizeof(struct t_node)); |
| 34 | + temp->left=temp->right=NULL; |
| 35 | + temp->data=x; |
| 36 | + return temp; |
| 37 | +} |
| 38 | + |
| 39 | +void Inorder(struct t_node* root) |
| 40 | +{//LNR |
| 41 | + if(!root) |
| 42 | + return; |
| 43 | + Inorder(root->left); |
| 44 | + printf("%d ",root->data); |
| 45 | + Inorder((root->right)); |
| 46 | +} |
| 47 | + |
| 48 | +void Preorder(struct t_node* root) |
| 49 | +{ |
| 50 | + //NLR |
| 51 | + if(!root) |
| 52 | + return; |
| 53 | + printf("%d ",root->data); |
| 54 | + Preorder(root->left); |
| 55 | + Preorder(root->right); |
| 56 | +} |
| 57 | + |
| 58 | +void Postorder(struct t_node* root) |
| 59 | +{ |
| 60 | + if(!root) |
| 61 | + return; |
| 62 | + Postorder(root->left); |
| 63 | + Postorder((root->right)); |
| 64 | + printf("%d ",root->data); |
| 65 | +} |
6 | 66 | // Function that prints Tree using
|
7 | 67 | // functions graphic.h header file
|
8 |
| -void printTree(int x, int y, int* array,int index,int total_elements) |
| 68 | +void printTree(int x, int y, struct t_node* root,int index) |
9 | 69 | {
|
10 |
| - int left,right; |
| 70 | + struct t_node* left; |
| 71 | + struct t_node* right; |
| 72 | + int lx,ry; |
11 | 73 | char str[10];
|
12 | 74 | //Base case of our recursive function
|
13 |
| - if (index >= total_elements) |
14 |
| - return; |
| 75 | + if (!root) |
| 76 | + return; |
15 | 77 |
|
16 | 78 | // Convert int value into string
|
17 |
| - itoa(array[index],str,10); |
18 |
| - |
19 |
| - // Set color of the boundary of circle |
20 |
| - setcolor(BLACK); |
21 |
| - |
22 |
| - // Draw the circle of radius 15 that represent node of Tree |
| 79 | + itoa(root->data,str,10); |
| 80 | + // Set color of the boundary of |
| 81 | + // circle as green |
| 82 | + setcolor(GREEN); |
| 83 | + // Draw the circle of radius 15 |
| 84 | + // that represent node of Tree |
23 | 85 | circle(x, y, 15);
|
24 | 86 | delay(200);
|
25 |
| - floodfill(x, y, RED); |
| 87 | + floodfill(x, y, GREEN); |
26 | 88 | delay(200);
|
27 |
| - |
28 | 89 | // Print the values of the node
|
29 | 90 | // in the circle
|
30 | 91 | outtextxy(x - 2, y - 3, str);
|
31 | 92 | delay(200);
|
32 | 93 | // Set the color of the line
|
33 |
| - setcolor(RED); |
| 94 | + // from parent to child as green |
| 95 | + setcolor(GREEN); |
34 | 96 | // Evaluating left and right child
|
35 |
| - left = 2 * index + 1; |
36 |
| - right = 2 * index + 2; |
| 97 | + left = root->left; |
| 98 | + right = root->right; |
| 99 | + lx=2*index+1; |
| 100 | + ry=2*index+1; |
37 | 101 | // Recursively draw the left subtree
|
38 | 102 | // 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 |
| - |
| 103 | + printTree(x - y / (index + 1), y + 50,left,lx); |
| 104 | + printTree(x + y / (index + 1), y + 50,right,ry); |
42 | 105 | // Draw the line (or link) when the
|
43 | 106 | // node is not the leaf node
|
44 |
| - if (left < total_elements) |
| 107 | + if (left!=NULL) |
45 | 108 | {
|
46 | 109 | delay(200);
|
47 |
| - line(x, y + 14, x - y / (index + 1), y + 50 - 14); |
| 110 | + line(x, y + 14, x - y / (index + 1), y + 50 - 14); |
48 | 111 | }
|
49 |
| - if (right < total_elements) |
| 112 | + if (right!=NULL) |
50 | 113 | {
|
51 | 114 | delay(200);
|
52 | 115 | line(x, y + 14, x + y / (index + 1), y + 50 - 14);
|
53 | 116 | }
|
| 117 | + return ; |
54 | 118 | }
|
55 |
| - |
| 119 | +//tree function |
56 | 120 | int main()
|
57 | 121 | {
|
58 |
| - int gd = DETECT, gm,size=0,i; |
59 |
| - int *array; |
| 122 | + int gd = DETECT, gm,i,x,lc,rc,c=1,choice; |
| 123 | + struct t_node* root; |
| 124 | + struct t_node* temp; |
| 125 | + struct node* head; |
| 126 | + struct node* tail; |
| 127 | + struct node* t; |
60 | 128 | 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) |
| 129 | + printf("NOTE:For tree input take -1 as NULL\n\n"); |
| 130 | + printf("Enter root node data: "); |
| 131 | + scanf("%d",&x); |
| 132 | + root=new_tNode(x); |
| 133 | + //queue |
| 134 | + head=new_qNode(root); |
| 135 | + tail=head;//rear pointer |
| 136 | + while(head) |
| 137 | + { |
| 138 | + temp=head->data; |
| 139 | + printf("Enter left child of %d: ",temp->data); |
| 140 | + scanf("%d",&lc); |
| 141 | + if(lc!=-1) |
| 142 | + { |
| 143 | + //tree Linking Nodes |
| 144 | + temp->left=new_tNode(lc); |
| 145 | + //tree |
| 146 | + //queue |
| 147 | + tail->next=new_qNode(temp->left); |
| 148 | + tail=tail->next; |
| 149 | + //queue |
| 150 | + } |
| 151 | + printf("Enter right child of %d: ",temp->data); |
| 152 | + scanf("%d",&rc); |
| 153 | + if(rc!=-1) |
| 154 | + { |
| 155 | + //tree linking |
| 156 | + temp->right=new_tNode(rc); |
| 157 | + //tree |
| 158 | + //queue |
| 159 | + tail->next=new_qNode(temp->right); |
| 160 | + tail=tail->next; |
| 161 | + //queue |
| 162 | + } |
| 163 | + t=head; |
| 164 | + head=head->next; |
| 165 | + free(t); |
| 166 | + } |
| 167 | + |
| 168 | + while(c) |
65 | 169 | {
|
66 |
| - printf("Enter node %d: ",i+1); |
67 |
| - scanf("%d",(array+i)); |
| 170 | + printf("1.In-order Traversal\n2.Pre-order Traversal\n3.Post-order traversal\n4.Display\n5.Exit\nEnter your choice: "); |
| 171 | + scanf("%d",&choice); |
| 172 | + switch(choice) |
| 173 | + { |
| 174 | + case 1: |
| 175 | + { |
| 176 | + printf("In-order Traversal: "); |
| 177 | + Inorder(root); |
| 178 | + break; |
| 179 | + } |
| 180 | + case 2: |
| 181 | + { |
| 182 | + printf("Pre-order Traversal: "); |
| 183 | + Preorder(root); |
| 184 | + break; |
| 185 | + } |
| 186 | + case 3: |
| 187 | + { |
| 188 | + printf("Post-order Traversal: "); |
| 189 | + Postorder(root); |
| 190 | + break; |
| 191 | + } |
| 192 | + case 4: |
| 193 | + { |
| 194 | + clrscr(); |
| 195 | + c=0; |
| 196 | + printTree(300, 100, root, 0); |
| 197 | + break; |
| 198 | + } |
| 199 | + case 5: |
| 200 | + { |
| 201 | + c=0; |
| 202 | + break; |
| 203 | + } |
| 204 | + default: |
| 205 | + { |
| 206 | + printf("Invalid choice!!!!Try again!!!!\n"); |
| 207 | + } |
| 208 | + } |
| 209 | + printf("\n"); |
68 | 210 | }
|
69 |
| - clrscr(); |
70 |
| - setbkcolor(BLACK); |
71 |
| - printTree(300, 100, array, 0, size); |
72 | 211 | getch();
|
73 | 212 | closegraph();
|
74 | 213 | return 0;
|
|
0 commit comments