-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-avl.c
141 lines (121 loc) · 3.78 KB
/
test-avl.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
#include "avl_core.h"
#include "avl_visualizer.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#define N_INSERT 1000 // The number of values to insert.
#define N_REMOVE 900 // The numver of values to delete.
int has(AvlTree *tree, int key){
Node *node = NULL;
return search_by_key(key, tree, &node);
}
/**
* @brief Generate a random number in a given range.
* @param min - The lower bound of the range.
* @param max - The upper bound of the range.
* @return A random number in the given range.
*/
int rand_in_range(int min, int max){
return min + rand() / (RAND_MAX / (max - min + 1) + 1);
}
/**
* @brief Recursively recalculate the height of every node.
* @param node - The currently looked at node.
* @return The height of the node currently looked at.
*/
int rec_height(Node *node){
if(!node) return -1;
node->height = get_int_max(
rec_height(node->left_child),
rec_height(node->right_child)) + 1;
return node->height;
}
/**
* @brief Recursively check if the tree holds the avl property.
* For this to give the correct result, the height of every node
* has to be correct in advance. It is recommended to run rec_height
* on the root, before executing the check_avl_property function.
* @param node - The currently looked at node.
* @return 1 - If the avl property holds, 0 - otherwise.
*/
int check_avl_property(Node *node){
if(!node) return 1;
int l_height = -1;
int r_height = -1;
if(node->left_child) l_height = node->left_child->height;
if(node->right_child) r_height = node->right_child->height;
int bal = r_height - l_height;
int is_avl = ((bal >= -1) && (bal <= 1)) ? 1 : 0;
return is_avl
&& check_avl_property(node->left_child)
&& check_avl_property(node->right_child);
}
int main(int argc, char **argv){
// Create an empty avl tree.
AvlTree *tree = make_tree_empty();
int insert_values[N_INSERT];
int delete_values[N_REMOVE];
srand(time(NULL));
for(int i = 0; i < N_INSERT; i++){
int r = rand_in_range(1, 9999999);
insert_values[i] = r;
key_insert_new(r, tree);
}
/* printf("Inorder traversal: "); */
/* traverse_inorder_console(tree->root); */
printf("\nNumber of nodes: %d\n", tree->number_of_nodes);
printf("Number of levels: %d\n", tree->height + 1);
/* printf("Visual representation:\n"); */
/* visualize(tree); */
// Recalculate the heights and check avl property.
rec_height(tree->root);
if(check_avl_property(tree->root)){
printf("AVL property satisfied.\n");
}else{
printf("AVL property violated.\n");
}
for(int i = 0; i < N_INSERT; i++){
if(!has(tree, insert_values[i])){
printf("Did not find key %d despite having added it!\n",
insert_values[i]);
}
}
for(int i = 0; i < N_REMOVE; i++){
int r = rand_in_range(0, N_INSERT - 1);
delete_values[i] = insert_values[r];
key_delete(delete_values[i], tree);
}
for(int i = 0; i < N_REMOVE; i++){
if(has(tree, delete_values[i])){
printf("Found key %d in tree, despite having removed it!\n",
delete_values[i]);
}
}
/* printf("Inorder traversal: "); */
/* traverse_inorder_console(tree->root); */
printf("\nNumber of nodes: %d\n", tree->number_of_nodes);
printf("Number of levels: %d\n", tree->height + 1);
/* printf("Visual representation:\n"); */
/* visualize(tree); */
// Recalculate the heights and check avl property.
rec_height(tree->root);
if(check_avl_property(tree->root)){
printf("AVL property satisfied.\n");
}else{
printf("AVL property violated.\n");
}
/*
* --------------
* -- Cleanup. --
* --------------
*/
// Remove all nodes from the tree.
for(int i = 0; i < N_INSERT; i++){
key_delete(insert_values[i], tree);
}
// Remove the tree.
free(tree);
tree = NULL;
return 0;
}