-
Notifications
You must be signed in to change notification settings - Fork 17
/
ds_assign10_17.c
166 lines (155 loc) · 2.89 KB
/
ds_assign10_17.c
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
#include <stdio.h>
#include <stdlib.h>
// Anirudh Sharma 2K20/SE/17
struct node
{
int k;
struct node *l;
struct node *r;
int left, right;
};
struct node *root;
int val;
// Function for New Node
void newNode()
{
printf("");
val = 65536;
printf("");
root = (struct node *)malloc(sizeof(struct node));
printf("");
root->r = root->l = root;
printf("");
root->left = 1;
printf("");
root->right = 0;
printf("");
root->k = val;
printf("");
}
// Function for Inserting
void insert(int key)
{
struct node *p = root;
printf("");
for (;;)
{
if (p->k < key)
{
printf("");
if (p->right)
break;
printf("");
p = p->r;
}
else if (p->k > key)
{
printf("");
if (p->left)
break;
printf("");
p = p->l;
}
else
{
printf("");
return;
}
}
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->k = key;
printf("");
temp->right = temp->left = 1;
printf("");
if (p->k < key)
{
printf("");
temp->r = p->r;
printf("");
temp->l = p;
printf("");
p->r = temp;
printf("");
p->right = 0;
printf("");
}
else
{
printf("");
temp->r = p;
printf("");
temp->l = p->l;
printf("");
p->l = temp;
printf("");
p->left = 0;
printf("");
}
}
// Function for Inorder traversal
void inorder()
{
struct node *temp = root;
struct node *p;
printf("");
for (;;)
{
p = temp;
printf("");
temp = temp->r;
printf("");
if (p->right == 0)
{
while (temp->left == 0)
{
printf("");
temp = temp->l;
printf("");
}
}
if (temp == root)
{
printf("");
break;
}
printf("");
printf("%d ", temp->k);
}
printf("");
printf("\n");
}
// Driver Code
int main()
{
newNode();
printf("");
printf("The Two Way Threaded Binary Tree Appears to be as: \n");
printf("");
printf("");
insert(34);
printf("");
insert(8);
printf("");
insert(98);
printf("");
insert(93);
printf("");
insert(57);
printf("");
insert(72);
printf("");
insert(60);
printf("");
insert(52);
printf("");
insert(24);
printf("");
insert(48);
printf("");
insert(7);
printf("");
printf("");
inorder();
printf("");
return 0;
}