-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
145 lines (116 loc) · 3.23 KB
/
main.c
File metadata and controls
145 lines (116 loc) · 3.23 KB
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
#include <stdio.h>
#include <stdlib.h>
typedef struct Element {
int key;
int val;
} Element;
typedef struct Node {
Element* elem;
void *p;
void *l;
void *r;
} Node;
typedef Node* Tree;
/*****************************************************************************/
Tree insert_element_with_parent(void* parent,Tree _tree,Element* _elem) {
if(_tree == NULL) {
//printf("insert here\n");
_tree = malloc(sizeof(Node));
_tree -> p = parent;
_tree -> l = NULL;
_tree -> r = NULL;
_tree -> elem = _elem;
}
else if(_elem -> key < _tree->elem->key) {
// add to left side
//printf("insert left\n");
_tree->l = insert_element_with_parent(_tree,_tree->l,_elem);
}
else {
// add to right side
//printf("insert right\n");
_tree->r = insert_element_with_parent(_tree,_tree->r,_elem);
}
return _tree;
}
/*****************************************************************************/
Tree insert_element(Tree _tree,Element* _elem) {
return insert_element_with_parent(NULL,_tree,_elem);
}
/*****************************************************************************/
void destroy_tree(Tree _tree) {
if(_tree) {
if(_tree->l) {
destroy_tree(_tree->l);
}
if(_tree->r) {
destroy_tree(_tree->r);
}
free(_tree);
}
}
/*****************************************************************************/
size_t get_tree_height(Tree _tree) {
if(_tree == NULL) return 0;
size_t lh = get_tree_height(_tree->l);
size_t rh = get_tree_height(_tree->r);
//printf("lh: %d rh: %d \n",lh,rh);
return 1 + (lh>rh? lh:rh);
}
Node* search_for_node(Tree _tree, int _key) {
if(_key == _tree->elem->key) {
return _tree;
}
else if(_key < _tree->elem->key) {
// go left
if(_tree->l) {
return search_for_node(_tree->l,_key);
}
else {
return NULL;
}
}
else {
// go right
if(_tree->r) {
return search_for_node(_tree->r,_key);
}
else {
return NULL;
}
}
// should be unreachable
return NULL;
}
/*****************************************************************************/
int main(int argc, char* argv[]) {
printf("Hello World!\n");
Tree _tree = NULL;
Element first_element;
first_element.key = 5;
first_element.val = 10;
_tree = insert_element(_tree,&first_element);
Element _second_element;
_second_element.key = 6;
_second_element.val = 12;
_tree = insert_element(_tree,&_second_element);
Element third_elem;
third_elem.key = 4;
third_elem.val = 8;
_tree = insert_element(_tree,&third_elem);
size_t h = get_tree_height(_tree);
printf("H: %d\n",h);
Node* res = search_for_node(_tree,4);
Node* res_two = search_for_node(_tree,1000);
if(res) {
printf("Node found! Key: %d Val %d\n",res->elem->key,res->elem->val);
}
if(res_two) {
printf("Node two found! Key %d Val %d\n",res->elem->key,res->elem->val);
}
else {
printf("Node two not found!\n");
}
destroy_tree(_tree);
return 0;
}