-
Notifications
You must be signed in to change notification settings - Fork 0
/
bstreess.txt
112 lines (100 loc) · 1.64 KB
/
bstreess.txt
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
/*
struct bst
{
int data;
struct bst *left;
struct bst *right;
};
struct bst *insert(struct bst *t, int x);
struct bst *tdelete(struct bst *t, int x);
void inorder(struct bst *t);
struct bst *find();
struct bst *findmin();
*/
/*
# include "adt.h"
# include <stdio.h>
# include <stdlib.h>
struct bst *insert(struct bst *t, int x)
{
if (t == NULL)
{
t = (struct bst *)malloc(sizeof(struct bst));
t->data = x;
t->left = NULL;
t->right = NULL;
}
else if (x < t->data)
{
t->left = insert(t->left, x);
}
else if (x > t->data)
{
t->right = insert(t->right, x);
}
return t;
}
struct bst *findmin(struct bst *t)
{
if (t == NULL)
{
return t;
}
while (t->left != NULL)
{
return findmin(t->left);
}
return t;
}
struct bst *tdelete(struct bst *t, int x)
{
struct bst *temp;
if (x < t->data)
{
t->left = tdelete(t->left, x);
}
else if (x > t->data)
{
t->right = tdelete(t->right, x);
}
else if (t->left && t->right)
{
temp = findmin(t->right);
t->data = temp->data;
t->right = tdelete(t->right, t->data);
}
else
{
temp = t;
if (t->right == NULL)
{
t = t->left;
}
if (t->left == NULL)
{
t = t->right;
}
free(temp);
return t;
}
}
void inorder(struct bst *t)
{
if (t->left != NULL)
inorder(t->left);
printf("%d ", t->data);
if (t->right != NULL)
inorder(t->right);
}
struct bst *find(struct bst *t, int x)
{
if (t == NULL)
return NULL;
else if (x == t->data)
return t;
else if (x < t->data)
return find(t->left, x);
else if (x > t->data)
return find(t->right, x);
}
*/