Skip to content

Commit 243a236

Browse files
committed
🔨 refactor: Add nodes to tree_visualization
1 parent a80f984 commit 243a236

File tree

2 files changed

+173
-122
lines changed

2 files changed

+173
-122
lines changed

tree_traversal.c

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ struct node* new_qNode(struct t_node* x)
2727
temp->data=x;
2828
return temp;
2929
}
30-
int q_size(struct node* head)
31-
{
32-
if(!head)
33-
return 0;
34-
return 1+q_size(head->next);
35-
}
3630
//queue functions
3731

3832

@@ -79,83 +73,6 @@ void Postorder(struct t_node* root)
7973
Postorder((root->right));
8074
printf("%d ",root->data);
8175
}
82-
83-
int height(struct t_node* root)
84-
{
85-
if(!root)
86-
{
87-
return 0;
88-
}
89-
int l,r;
90-
l=height(root->left);
91-
r=height(root->right);
92-
if(l>r)
93-
{
94-
return l+1;
95-
}
96-
else
97-
{
98-
return r+1;
99-
}
100-
}
101-
void d_spaces(int space)
102-
{
103-
for(int i=0;i<space;++i)
104-
{
105-
printf(" ");
106-
}
107-
}
108-
109-
void display(struct t_node* root)
110-
{
111-
struct node* head=new_qNode(root);
112-
struct node* tail=head;
113-
int h=(1<<height(root)-1);
114-
int internal;
115-
int x=1;
116-
int spaces=h/2+(h/4)*3+(h/4);
117-
if(spaces==1)
118-
spaces=3;
119-
while(x<=h)
120-
{
121-
d_spaces(spaces);
122-
internal=spaces*2-1;
123-
if(x==h)
124-
internal=3;
125-
int c=0;
126-
for(int i=0;i<x;++i)
127-
{
128-
struct t_node* temp=head->data;
129-
printf("%d",temp->data);
130-
++c;
131-
if(x!=h)
132-
d_spaces(internal);
133-
else
134-
{
135-
if(c&1)
136-
d_spaces(internal);
137-
else
138-
printf(" ");
139-
}
140-
if(temp->left)
141-
{
142-
tail->next=new_qNode(temp->left);
143-
tail=tail->next;
144-
}
145-
if(temp->right)
146-
{
147-
tail->next=new_qNode(temp->right);
148-
tail=tail->next;
149-
}
150-
head=head->next;
151-
}
152-
printf("\n");
153-
x*=2;
154-
spaces/=2;
155-
}
156-
}
157-
158-
15976
/*
16077
1. 1
16178
2. 2 3
@@ -208,7 +125,7 @@ int main()
208125
int c=1,choice;
209126
while(c)
210127
{
211-
printf("1.In-order Traversal\n2.Pre-order Traversal\n3.Post-order traversal\n4.Display\n5.Exit\nEnter your choice: ");
128+
printf("1.In-order Traversal\n2.Pre-order Traversal\n3.Post-order traversal\n4.Exit\nEnter your choice: ");
212129
scanf("%d",&choice);
213130
switch(choice)
214131
{
@@ -231,11 +148,6 @@ int main()
231148
break;
232149
}
233150
case 4:
234-
{
235-
display(root);
236-
break;
237-
}
238-
case 5:
239151
{
240152
c=0;
241153
break;

tree_visualization.c

Lines changed: 172 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,211 @@
33
#include <math.h>
44
#include <stdlib.h>
55

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+
}
666
// Function that prints Tree using
767
// 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)
969
{
10-
int left,right;
70+
struct t_node* left;
71+
struct t_node* right;
72+
int lx,ry;
1173
char str[10];
1274
//Base case of our recursive function
13-
if (index >= total_elements)
14-
return;
75+
if (!root)
76+
return;
1577

1678
// 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
2385
circle(x, y, 15);
2486
delay(200);
25-
floodfill(x, y, RED);
87+
floodfill(x, y, GREEN);
2688
delay(200);
27-
2889
// Print the values of the node
2990
// in the circle
3091
outtextxy(x - 2, y - 3, str);
3192
delay(200);
3293
// Set the color of the line
33-
setcolor(RED);
94+
// from parent to child as green
95+
setcolor(GREEN);
3496
// 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;
37101
// Recursively draw the left subtree
38102
// 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);
42105
// Draw the line (or link) when the
43106
// node is not the leaf node
44-
if (left < total_elements)
107+
if (left!=NULL)
45108
{
46109
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);
48111
}
49-
if (right < total_elements)
112+
if (right!=NULL)
50113
{
51114
delay(200);
52115
line(x, y + 14, x + y / (index + 1), y + 50 - 14);
53116
}
117+
return ;
54118
}
55-
119+
//tree function
56120
int main()
57121
{
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;
60128
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)
65169
{
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");
68210
}
69-
clrscr();
70-
setbkcolor(BLACK);
71-
printTree(300, 100, array, 0, size);
72211
getch();
73212
closegraph();
74213
return 0;

0 commit comments

Comments
 (0)