-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp9.c
190 lines (189 loc) · 4.14 KB
/
Exp9.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left, *right;
}*root, *newNode, *t, *t1;
void createTree()
{
root = NULL;
}
void insert(int val)
{
newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = val;
newNode->left = NULL;
newNode->right = NULL;
if(root==NULL){
root = newNode;
}
else{
t = t1 = root;
while(t!=NULL){
t1 = t;
if(val<t->data){
t = t->left;
}
else{
t = t->right;
}
}
if(val<t1->data)
t1->left = newNode;
else
t1->right = newNode;
}
}
void inorder(struct node *n)
{
if(n!=NULL){
inorder(n->left);
printf("%d ", n->data);
inorder(n->right);
}
}
void preorder(struct node *n)
{
if(n!=NULL){
printf("%d ", n->data);
inorder(n->left);
inorder(n->right);
}
}
void postorder(struct node *n)
{
if(n!=NULL){
inorder(n->left);
inorder(n->right);
printf("%d ", n->data);
}
}
struct node* smallest( struct node *n)
{
if(n==NULL||n->left==NULL)
return n;
else
return smallest(n->left);
}
struct node *largest(struct node *n)
{
if(n==NULL||n->right==NULL)
return n;
else
return largest(n->right);
}
int totalNode(struct node *n)
{
if(n==NULL)
return 0;
else
return (totalNode(n->left) + totalNode(n->right)+1);
}
int externalNode(struct node* n)
{
if(n==NULL)
return 0;
else if(n->right==NULL && n->left==NULL)
return 1;
else
return (externalNode(n->left)+externalNode(n->right));
}
int internalNode(struct node* n)
{
if(n==NULL)
return 0;
else if(n->right==NULL && n->left==NULL)
return 0;
else
return(internalNode(n->left)+internalNode(n->right)+1);
}
int height(struct node *n)
{
int leftHeight, rightHeight;
if(n==NULL)
return 0;
else{
leftHeight = height(n->left);
rightHeight = height(n->right);
if(leftHeight>rightHeight)
return(leftHeight+1);
else
return(rightHeight+1);
}
}
void mirror(struct node *n)
{
struct node *ptr;
if(n!=NULL){
mirror(n->left);
mirror(n->right);
ptr = n->left;
n->left = n->right;
n->right = ptr;
}
}
struct node* minValueNode(struct node* n)
{
struct node* current = n;
while (current && current->left != NULL)
current = current->left;
return current;
}
struct node* delete(struct node *n, int val)
{
if(n==NULL){
return n;
}
if(val<n->data)
n->left = delete(n->left, val);
else if(val>n->data)
n->right = delete(n->right, val);
else{
if(n->left == NULL){
struct node *temp = n->right;
free(n);
return temp;
}
else if(n->right == NULL){
struct node *temp = n->left;
free(n);
return temp;
}
struct node *temp = minValueNode(n->right);
n->data = temp->data;
n->right = delete(n->right, temp->data);
}
return root;
}
int main()
{
createTree();
insert(50);
insert(30);
insert(70);
insert(20);
insert(40);
insert(60);
insert(80);
inorder(root);
t = smallest(root);
printf("\nSmallest: %d", t->data);
t = largest(root);
printf("\nLargest: %d", t->data);
int numN = totalNode(root);
printf("\nNumber of node: %d", numN);
numN = internalNode(root);
printf("\nNumber of internal node: %d", numN-1);
numN = externalNode(root);
printf("\nNumber of external node: %d", numN);
int h = height(root);
printf("\nHeight: %d", h);
//mirror(root);
printf("\n");
inorder(root);
root = delete(root, 20);
printf("\nI came here!");
inorder(root);
printf("\nI came here!");
return 0;
}