annotate chromeister/src/alignmentFunctions.c @ 0:7fdf47a0bae8 draft

Uploaded
author alvarofaure
date Wed, 12 Dec 2018 07:18:40 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
1 #include <stdio.h>
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
2 #include <stdlib.h>
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
3 #include <string.h>
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
4 #include <ctype.h>
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
5 #include <inttypes.h>
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
6 #include <math.h>
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
7 #include <float.h>
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
8 #include "structs.h"
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
9 #include "alignmentFunctions.h"
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
10 #include "commonFunctions.h"
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
11
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
12
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
13
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
14 int64_t compare_letters(unsigned char a, unsigned char b){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
15 if(a != (unsigned char) 'N' && a != (unsigned char) '>') return (a == b) ? POINT : -POINT;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
16 return -POINT;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
17 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
18
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
19 llpos * getNewLocationllpos(Mempool_l * mp, uint64_t * n_pools_used){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
20
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
21 if(mp[*n_pools_used].current == POOL_SIZE){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
22 *n_pools_used += 1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
23 if(*n_pools_used == MAX_MEM_POOLS) terror("Reached max pools");
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
24 init_mem_pool_llpos(&mp[*n_pools_used]);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
25
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
26 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
27
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
28 llpos * new_pos = mp[*n_pools_used].base + mp[*n_pools_used].current;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
29 mp[*n_pools_used].current++;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
30
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
31
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
32 return new_pos;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
33 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
34
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
35 void init_mem_pool_llpos(Mempool_l * mp){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
36 mp->base = (llpos *) calloc(POOL_SIZE, sizeof(llpos));
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
37 if(mp->base == NULL) terror("Could not request memory pool");
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
38 mp->current = 0;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
39 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
40
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
41 AVLTree * getNewLocationAVLTree(Mempool_AVL * mp, uint64_t * n_pools_used, uint64_t key){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
42
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
43 if(mp[*n_pools_used].current == POOL_SIZE){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
44 *n_pools_used += 1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
45 if(*n_pools_used == MAX_MEM_POOLS) terror("Reached max pools");
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
46 init_mem_pool_AVL(&mp[*n_pools_used]);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
47
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
48 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
49
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
50 AVLTree * new_pos = mp[*n_pools_used].base + mp[*n_pools_used].current;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
51 mp[*n_pools_used].current++;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
52
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
53 new_pos->key = key;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
54 new_pos->count = 1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
55 new_pos->height = 1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
56
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
57 return new_pos;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
58 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
59
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
60 void init_mem_pool_AVL(Mempool_AVL * mp){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
61 mp->base = (AVLTree *) calloc(POOL_SIZE, sizeof(AVLTree));
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
62 if(mp->base == NULL) terror("Could not request memory pool");
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
63 mp->current = 0;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
64 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
65
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
66
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
67
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
68 /*
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
69 // An AVL tree node
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
70 typedef struct AVL_Node{
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
71 uint64_t key;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
72 struct AVL_Node * left;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
73 struct AVL_Node * right;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
74 uint64_t height;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
75 llpos * next;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
76 } AVLTree;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
77 */
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
78
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
79 // A utility function to get height of the tree
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
80
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
81 uint64_t height(AVLTree * N){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
82 if (N == NULL)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
83 return 0;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
84 return N->height;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
85 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
86
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
87 /* Substituted by (x == NULL) ? (0) : (x->height) */
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
88
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
89 /* Helper function that allocates a new node with the given key and
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
90 NULL left and right pointers. */
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
91
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
92 /* This one is substituted by AVLTree * getNewLocationAVLTree(Mempool_AVL * mp, uint64_t * n_pools_used, uint64_t key) */
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
93
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
94 // A utility function to right rotate subtree rooted with y
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
95 // See the diagram given above.
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
96 AVLTree * right_rotate(AVLTree * y){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
97 AVLTree * x = y->left;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
98 AVLTree * T2 = x->right;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
99
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
100 // Perform rotation
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
101 x->right = y;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
102 y->left = T2;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
103
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
104 // Update heights
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
105 //x->height = MAX((x == NULL) ? (0) : (x->left->height), (x == NULL) ? (0) : (x->right->height))+1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
106 //y->height = MAX((y == NULL) ? (0) : (y->left->height), (y == NULL) ? (0) : (y->right->height))+1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
107 // Update heights
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
108 y->height = MAX(height(y->left), height(y->right))+1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
109 x->height = MAX(height(x->left), height(x->right))+1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
110
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
111 // Return new root
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
112 return x;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
113 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
114
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
115 // A utility function to left rotate subtree rooted with x
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
116 // See the diagram given above.
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
117 AVLTree * left_rotate(AVLTree * x){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
118 AVLTree * y = x->right;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
119 AVLTree * T2 = y->left;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
120
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
121 // Perform rotation
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
122 y->left = x;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
123 x->right = T2;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
124
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
125 // Update heights
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
126 //x->height = MAX((x == NULL) ? (0) : (x->left->height), (x == NULL) ? (0) : (x->right->height))+1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
127 //y->height = MAX((y == NULL) ? (0) : (y->left->height), (y == NULL) ? (0) : (y->right->height))+1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
128 x->height = MAX(height(x->left), height(x->right))+1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
129 y->height = MAX(height(y->left), height(y->right))+1;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
130
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
131 // Return new root
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
132 return y;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
133 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
134
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
135 // Get Balance factor of node N
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
136
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
137 int64_t get_balance(AVLTree * N){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
138 if (N == NULL)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
139 return 0;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
140 return height(N->left) - height(N->right);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
141 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
142
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
143 /* Substituted by (node == NULL) ? (0) : ((int64_t) node->left->height - (int64_t) node->right->height) */
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
144
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
145 AVLTree * find_AVLTree(AVLTree * node, uint64_t key){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
146 AVLTree * found = NULL;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
147 if(node == NULL) return NULL;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
148
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
149 if (key < node->key) {
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
150 found = find_AVLTree(node->left, key);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
151 } else if (key > node->key) {
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
152 found = find_AVLTree(node->right, key);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
153 } else {
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
154 return node;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
155 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
156 return found;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
157 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
158
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
159 llpos * find_AVLTree_llpos(AVLTree * node, uint64_t key){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
160 llpos * aux = NULL;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
161 if(node == NULL) return NULL;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
162
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
163 if (key < node->key) {
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
164 aux = find_AVLTree_llpos(node->left, key);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
165 } else if (key > node->key) {
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
166 aux = find_AVLTree_llpos(node->right, key);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
167 } else {
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
168 return node->next;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
169 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
170 return aux;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
171 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
172
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
173 // Recursive function to insert key in subtree rooted
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
174 // with node and returns new root of subtree.
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
175 AVLTree * insert_AVLTree(AVLTree * node, uint64_t key, Mempool_AVL * mp, uint64_t * n_pools_used, uint64_t pos, Mempool_l * mp_l, uint64_t * n_pools_used_l){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
176 /* 1. Perform the normal BST insertion */
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
177 if (node == NULL){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
178
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
179 AVLTree * n_node = getNewLocationAVLTree(mp, n_pools_used, key);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
180 llpos * aux = getNewLocationllpos(mp_l, n_pools_used_l);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
181 aux->pos = pos;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
182 n_node->next = aux;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
183 return n_node;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
184 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
185
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
186 if (key < node->key) {
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
187 node->left = insert_AVLTree(node->left, key, mp, n_pools_used, pos, mp_l, n_pools_used_l);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
188 } else if (key > node->key) {
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
189 node->right = insert_AVLTree(node->right, key, mp, n_pools_used, pos, mp_l, n_pools_used_l);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
190 } else {
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
191 // Equal keys are inserted as a linked list
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
192 if(node->count == 1){ // DO NOT INSERT MORE THAN 2. 1 is good 2 is repetition
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
193 llpos * aux = getNewLocationllpos(mp_l, n_pools_used_l);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
194 aux->pos = pos;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
195 aux->next = node->next;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
196 node->next = aux;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
197 ++(node->count);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
198 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
199
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
200 return node;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
201 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
202
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
203 /* 2. Update height of this ancestor node */
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
204 //node->height = 1 + MAX((node->left == NULL) ? (0) : (node->left->height), (node->right == NULL) ? (0) : (node->right->height));
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
205 node->height = 1 + MAX(height(node->left), height(node->right));
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
206
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
207 /* 3. Get the balance factor of this ancestor
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
208 node to check whether this node became
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
209 unbalanced */
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
210 //int64_t balance = (node->left == NULL || node->right == NULL) ? (0) : ((int64_t) node->left->height - (int64_t) node->right->height);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
211 int64_t balance = get_balance(node);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
212
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
213 // If this node becomes unbalanced, then
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
214 // there are 4 cases
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
215
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
216 // Left Left Case
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
217 if (balance > 1 && key < node->left->key)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
218 return right_rotate(node);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
219
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
220 // Right Right Case
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
221 if (balance < -1 && key > node->right->key)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
222 return left_rotate(node);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
223
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
224 // Left Right Case
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
225 if (balance > 1 && key > node->left->key)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
226 {
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
227 node->left = left_rotate(node->left);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
228 return right_rotate(node);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
229 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
230
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
231 // Right Left Case
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
232 if (balance < -1 && key < node->right->key)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
233 {
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
234 node->right = right_rotate(node->right);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
235 return left_rotate(node);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
236 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
237
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
238 /* return the (unchanged) node pointer */
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
239 return node;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
240 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
241
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
242 // A utility function to print preorder traversal
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
243 // of the tree.
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
244 // The function also prints height of every node
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
245
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
246 void pre_order(AVLTree * root){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
247 if(root != NULL){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
248 printf("%"PRIu64" ", root->key);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
249 llpos * aux = root->next;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
250 while(aux != NULL){ printf("#%"PRIu64", ", aux->pos); aux = aux->next; }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
251 pre_order(root->left);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
252 pre_order(root->right);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
253 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
254 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
255
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
256
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
257 uint64_t sum_of_all_tree(AVLTree * root){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
258 uint64_t mysum = 0;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
259 if(root != NULL){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
260
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
261 mysum = root->count;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
262 mysum += sum_of_all_tree(root->left);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
263 mysum += sum_of_all_tree(root->right);
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
264 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
265 return mysum;
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
266 }