123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- #include "private.h"
- lub_bintree_node_t *lub_bintree_splay(const lub_bintree_t * this,
- lub_bintree_node_t * t, const void *key)
- {
-
-
- lub_bintree_node_t N, *leftTreeMax, *rightTreeMin, *y;
- int comp;
- if (t == NULL)
- return t;
- N.left = N.right = NULL;
- leftTreeMax = rightTreeMin = &N;
- for (;;) {
- comp = lub_bintree_compare(this, t, key);
- if (comp > 0) {
- if (t->left == NULL)
- break;
- if (lub_bintree_compare(this, t->left, key) > 0) {
- y = t->left;
- t->left = y->right;
- y->right = t;
- t = y;
- if (t->left == NULL)
- break;
- }
- rightTreeMin->left = t;
- rightTreeMin = t;
- t = t->left;
- } else if (comp < 0) {
- if (t->right == NULL)
- break;
- if (lub_bintree_compare(this, t->right, key) < 0) {
- y = t->right;
- t->right = y->left;
- y->left = t;
- t = y;
- if (t->right == NULL)
- break;
- }
- leftTreeMax->right = t;
- leftTreeMax = t;
- t = t->right;
- } else {
- break;
- }
- }
- leftTreeMax->right = t->left;
- rightTreeMin->left = t->right;
- t->left = N.right;
- t->right = N.left;
-
- return t;
- }
|