Last active
December 18, 2015 07:19
-
-
Save wnoizumi/5745843 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct avltree_node *avltree_insert(struct avltree_node *node, struct avltree *tree){ | |
struct avltree_node *key, *parent, *unbalanced; | |
int is_left; | |
key = do_lookup(node, tree, &parent, &unbalanced, &is_left); | |
if (key) | |
return key; | |
INIT_NODE(node); | |
if (!parent) { | |
tree->root = node; | |
tree->first = tree->last = node; | |
tree->height++; | |
return NULL; | |
} | |
if (is_left) { | |
if (parent == tree->first) | |
tree->first = node; | |
set_parent(parent, node); | |
set_child(node, parent, is_left); | |
for (;;) { | |
if (parent->left == node) | |
dec_balance(parent); | |
else | |
inc_balance(parent); | |
if (parent == unbalanced) | |
break; | |
node = parent; | |
parent = get_parent(parent); | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void avltree_remove(struct avltree_node *node, struct avltree *tree) | |
{ | |
struct avltree_node *parent = get_parent(node); | |
struct avltree_node *left = node->left; | |
struct avltree_node *right = node->right; | |
struct avltree_node *next; | |
int is_left = is_left; | |
if (node == tree->first) | |
tree->first = avltree_next(node); | |
if (node == tree->last) | |
tree->last = avltree_prev(node); | |
if (!left) | |
next = right; | |
else if (!right) | |
next = left; | |
else | |
next = get_first(right); | |
if (parent) { | |
is_left = parent->left == node; | |
set_child(next, parent, is_left); | |
} else | |
tree->root = next; | |
if (left && right) { | |
set_balance(get_balance(node), next); | |
next->left = left; | |
set_parent(next, left); | |
if (next != right) { | |
parent = get_parent(next); | |
set_parent(get_parent(node), next); | |
node = next->right; | |
parent->left = node; | |
is_left = 1; | |
next->right = right; | |
set_parent(next, right); | |
} else { | |
set_parent(parent, next); | |
parent = next; | |
node = parent->right; is_left = 0; | |
} | |
assert(parent != NULL); | |
} else | |
node = next; | |
if (node) | |
set_parent(parent, node); | |
… | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
fp = fopen(file_input, "r"); | |
... | |
for (int k = 1; k <= 2*M_arcos; k += 2) | |
{ | |
fscanf(fp, "%s %d %d %d", line_parse, &i, &j, &w); | |
I_arco[k] = i; | |
J_arco[k] = j; | |
I_arco[k+1] = j; | |
J_arco[k+1] = i; | |
MAdj[i][j] = 1; | |
MAdj[j][i] = 1; | |
Dist[i][j] = w; | |
Dist[j][i] = w; | |
DistInfinita += (Dist[i][j] + Dist[j][i]); | |
} | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(int argc, char *argv[]) | |
{ | |
int v = 1; // vertice de origem para o algoritmo comecar o processamento | |
... | |
pred[s] = s; // “s” sempre será 1 aqui | |
... | |
for (v=1; v<=Dim; v++) | |
{ | |
pred[v] = -1; | |
key[v] = DistInfinita; | |
} | |
... | |
for (v = 1; v <= Dim; v++) | |
{ | |
if (s != v) // soh o primeiro nao | |
{ | |
h.insert(v, key[v]); | |
} | |
} | |
… | |
... | |
for(l=1; l<=CardP[s]; l++) | |
{ | |
v = LisAdjP[s][l]; | |
if (key[v] > Dist[s][v]) | |
{ | |
int found = h.remove(&v, &key[v]); | |
if (found == 0) | |
continue; | |
key[v] = Dist[s][v]; | |
h.insert(v, key[v]); | |
pred[v] = s; | |
} | |
} | |
… | |
... | |
for (k = 2; k <= Dim; k++) | |
{ | |
h.deletemin(&vmin, &dmin); | |
for(l=1; l<=CardP[vmin]; l++) | |
{ | |
v = LisAdjP[vmin][l]; | |
if ( key[v] > Dist[vmin][v]) | |
{ | |
int found = h.remove(&v, &key[v]); | |
if (found == 0) | |
continue; | |
key[v] = Dist[vmin][v]; | |
h.insert(v, key[v]); | |
pred[v] = vmin; | |
} | |
} | |
... | |
} | |
... | |
... | |
int total_dist = 0; | |
for (v=1; v<=Dim; v++) { | |
debug_printf("Aresta: %d %d %d\n",v ,pred[v], Dist[v][pred[v]]); | |
if (Dist[v][pred[v]] != DistInfinita) | |
total_dist += Dist[v][pred[v]]; | |
} | |
debug_printf("Total distance: %d\n", total_dist); | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(l=1; l<=CardP[s]; l++) | |
{ | |
v = LisAdjP[s][l]; | |
if (key[v] > Dist[s][v]) | |
{ | |
int found = h.remove(&v, &key[v]); | |
if (found == 0) continue; | |
key[v] = Dist[s][v]; | |
h.insert(v, key[v]); | |
pred[v] = s; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment