| 59 | static sTree * sedgewickized_splay (int i, sTree * t); |
| 60 | |
| 61 | sTree * splay (key_type i, sTree * t) { |
| 62 | /* Simple top down splay, not requiring i to be in the sTree t. */ |
| 63 | /* What it does is described above. */ |
| 64 | sTree N, *l, *r, *y; |
| 65 | if (t == NULL) return t; |
| 66 | N.left = N.right = NULL; |
| 67 | l = r = &N; |
| 68 | long l_size=0, r_size=0; |
| 69 | |
| 70 | for (;;) { |
| 71 | if (key_cmp(i, t->key) < 0){ |
| 72 | if (t->left == NULL) break; |
| 73 | if (key_cmp(i, t->left->key) < 0) { |
| 74 | y = t->left; /* rotate right */ |
| 75 | t->left = y->right; |
| 76 | y->right = t; |
| 77 | t->value = node_value(t->left) + node_value(t->right) + 1; |
| 78 | t = y; |
| 79 | if (t->left == NULL) break; |
| 80 | } |
| 81 | r->left = t; /* link right */ |
| 82 | r = t; |
| 83 | t = t->left; |
| 84 | r_size += 1+node_value(r->right); |
| 85 | } else if (key_cmp(i, t->key) > 0) { |
| 86 | if (t->right == NULL) break; |
| 87 | if (key_cmp(i, t->right->key) > 0) { |
| 88 | y = t->right; /* rotate left */ |
| 89 | t->right = y->left; |
| 90 | y->left = t; |
| 91 | t->value = node_value(t->left) + node_value(t->right) + 1; |
| 92 | t = y; |
| 93 | if (t->right == NULL) break; |
| 94 | } |
| 95 | l->right = t; /* link left */ |
| 96 | l = t; |
| 97 | t = t->right; |
| 98 | l_size += 1+node_value(l->left); |
| 99 | } else { |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // TODO: there should be a better way to do this!!!!!!!!! |
| 105 | |
| 106 | l_size += node_value(t->left); /* Now l_size and r_size are the sizes of */ |
| 107 | r_size += node_value(t->right); /* the left and right sTrees we just built.*/ |
| 108 | t->value = l_size + r_size + 1; |
| 109 | |
| 110 | l->right = r->left = NULL; |
| 111 | |
| 112 | /* The following two loops correct the size fields of the right path */ |
| 113 | /* from the left child of the root and the right path from the left */ |
| 114 | /* child of the root. */ |
| 115 | for (y = N.right; y != NULL; y = y->right) { |
| 116 | y->value = l_size; |
| 117 | l_size -= 1+node_value(y->left); |
| 118 | } |
no test coverage detected