This repository has been archived by the owner on Jan 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
153 lines (117 loc) · 2.56 KB
/
tree.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
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct trnode{
int data;
struct trnode* left;
struct trnode* right;
} node;
#define ARR_SIZE 15
node* find_node(node* root, int data);
void add_node(node** root, node* n);
void remove_node(node** root, int data);
int main(int argc, char ** argv) {
node* root = NULL;
srand((unsigned int)time(NULL));
for (unsigned int i = 0; i < ARR_SIZE; i++) {
node* n = (node *)calloc(1, sizeof(node));
n->data = rand() % 100;
add_node(&root, n);
}
return 0;
}
node* find_node(node* root, int data) {
if (root) {
if (root->data == data)
return root;
else if (root->data > data)
return find_node(root->right, data);
else
return find_node(root->left, data);
}
else
return NULL;
}
void add_node(node** root, node* n) {
if (!(*root)) {
*root = n;
return;
}
if ((*root)->data > n->data)
add_node(&((*root)->left), n);
else
add_node(&((*root)->right), n);
}
void remove_node(node **root, int data) {
if (*root == NULL)
return;
if ((*root)->data < data) {
remove_node(&((*root)->right), data);
}
else if ((*root)->data > data) {
remove_node(&((*root)->left), data);
}
else {
if ((*root)->left == NULL && (*root)->right == NULL){
free(*root);
*root = NULL;
}
else if ((*root)->right == NULL) {
node * tmp = (*root)->left;
free(*root);
*root = tmp;
}
else if ((*root)->left == NULL) {
node * tmp = (*root)->right;
free(*root);
*root = tmp;
}
else if ((*root)->right->left == NULL) {
(*root)->right->left = (*root)->left;
node * tmp = (*root)->right;
free(*root);
*root = tmp;
}
else {
node * tmp = (*root)->left;
node * r = (*root)->right;
free(*root);
*root = r;
node * most_left = (*root)->left;
for (;most_left->left;)
most_left = most_left->left;
most_left->left = tmp;
}
}
}
// node* find_parent(node* root, node* n) {
// if (root) {
// if (root->left == n || root->right == n)
// return root;
// else if (root->data > n->data)
// return find_parent(root->right, n);
// else
// return find_parent(root->left, n);
// }
// else
// return NULL;
// }
// void remove_node(node** root, node* n) {
// if (*root == n && *root->left == NULL && *root->right == NULL){
// *root = NULL;
// return;
// }
// node * parent = find_parent(*root, n);
// if (parent){
// if (parent->left == n) {
// parent->left = n->left;
// }
// else {
// parent->right = n->left;
// }
// }
// else {
// *root = n->left;
// }
// }