avl.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <avl.h>
  13. /*
  14. * In-memory database stored as self-balancing AVL tree.
  15. * See Lewis & Denenberg, Data Structures and Their Algorithms.
  16. */
  17. static void
  18. singleleft(Avl **tp, Avl *p)
  19. {
  20. int l, r2;
  21. Avl *a, *c;
  22. a = *tp;
  23. c = a->n[1];
  24. r2 = c->bal;
  25. l = (r2 > 0? r2: 0)+1 - a->bal;
  26. if((a->n[1] = c->n[0]) != nil)
  27. a->n[1]->p = a;
  28. if((c->n[0] = a) != nil)
  29. c->n[0]->p = c;
  30. if((*tp = c) != nil)
  31. (*tp)->p = p;
  32. a->bal = -l;
  33. c->bal = r2 - ((l > 0? l: 0)+1);
  34. }
  35. static void
  36. singleright(Avl **tp, Avl *p)
  37. {
  38. int l2, r;
  39. Avl *a, *c;
  40. a = *tp;
  41. c = a->n[0];
  42. l2 = - c->bal;
  43. r = a->bal + ((l2 > 0? l2: 0)+1);
  44. if((a->n[0] = c->n[1]) != nil)
  45. a->n[0]->p = a;
  46. if((c->n[1] = a) != nil)
  47. c->n[1]->p = c;
  48. if((*tp = c) != nil)
  49. (*tp)->p = p;
  50. a->bal = r;
  51. c->bal = ((r > 0? r: 0)+1) - l2;
  52. }
  53. static void
  54. doublerightleft(Avl **tp, Avl *p)
  55. {
  56. singleright(&(*tp)->n[1], *tp);
  57. singleleft(tp, p);
  58. }
  59. static void
  60. doubleleftright(Avl **tp, Avl *p)
  61. {
  62. singleleft(&(*tp)->n[0], *tp);
  63. singleright(tp, p);
  64. }
  65. static void
  66. balance(Avl **tp, Avl *p)
  67. {
  68. switch((*tp)->bal){
  69. case -2:
  70. if((*tp)->n[0]->bal <= 0)
  71. singleright(tp, p);
  72. else if((*tp)->n[0]->bal == 1)
  73. doubleleftright(tp, p);
  74. else
  75. assert(0);
  76. break;
  77. case 2:
  78. if((*tp)->n[1]->bal >= 0)
  79. singleleft(tp, p);
  80. else if((*tp)->n[1]->bal == -1)
  81. doublerightleft(tp, p);
  82. else
  83. assert(0);
  84. break;
  85. }
  86. }
  87. static int
  88. canoncmp(int cmp)
  89. {
  90. if(cmp < 0)
  91. return -1;
  92. else if(cmp > 0)
  93. return 1;
  94. return 0;
  95. }
  96. static int
  97. _insertavl(Avl **tp, Avl *p, Avl *r, int (*cmp)(Avl*,Avl*), Avl **rfree)
  98. {
  99. int i, ob;
  100. if(*tp == nil){
  101. r->bal = 0;
  102. r->n[0] = nil;
  103. r->n[1] = nil;
  104. r->p = p;
  105. *tp = r;
  106. return 1;
  107. }
  108. ob = (*tp)->bal;
  109. if((i = canoncmp(cmp(r, *tp))) != 0){
  110. (*tp)->bal += i * _insertavl(&(*tp)->n[(i+1)/2], *tp, r, cmp,
  111. rfree);
  112. balance(tp, p);
  113. return ob == 0 && (*tp)->bal != 0;
  114. }
  115. /* install new entry */
  116. *rfree = *tp; /* save old node for freeing */
  117. *tp = r; /* insert new node */
  118. **tp = **rfree; /* copy old node's Avl contents */
  119. if(r->n[0]) /* fix node's children's parent pointers */
  120. r->n[0]->p = r;
  121. if(r->n[1])
  122. r->n[1]->p = r;
  123. return 0;
  124. }
  125. static int
  126. successor(Avl **tp, Avl *p, Avl **r)
  127. {
  128. int ob;
  129. if((*tp)->n[0] == nil){
  130. *r = *tp;
  131. *tp = (*r)->n[1];
  132. if(*tp)
  133. (*tp)->p = p;
  134. return -1;
  135. }
  136. ob = (*tp)->bal;
  137. (*tp)->bal -= successor(&(*tp)->n[0], *tp, r);
  138. balance(tp, p);
  139. return -(ob != 0 && (*tp)->bal == 0);
  140. }
  141. static int
  142. _deleteavl(Avl **tp, Avl *p, Avl *rx, int(*cmp)(Avl*,Avl*), Avl **del,
  143. void (*predel)(Avl*, void*), void *arg)
  144. {
  145. int i, ob;
  146. Avl *r, *or;
  147. if(*tp == nil)
  148. return 0;
  149. ob = (*tp)->bal;
  150. if((i=canoncmp(cmp(rx, *tp))) != 0){
  151. (*tp)->bal += i * _deleteavl(&(*tp)->n[(i+1)/2], *tp, rx, cmp,
  152. del, predel, arg);
  153. balance(tp, p);
  154. return -(ob != 0 && (*tp)->bal == 0);
  155. }
  156. if(predel)
  157. (*predel)(*tp, arg);
  158. or = *tp;
  159. if(or->n[i=0] == nil || or->n[i=1] == nil){
  160. *tp = or->n[1-i];
  161. if(*tp)
  162. (*tp)->p = p;
  163. *del = or;
  164. return -1;
  165. }
  166. /* deleting node with two kids, find successor */
  167. or->bal += successor(&or->n[1], or, &r);
  168. r->bal = or->bal;
  169. r->n[0] = or->n[0];
  170. r->n[1] = or->n[1];
  171. *tp = r;
  172. (*tp)->p = p;
  173. /* node has changed; fix children's parent pointers */
  174. if(r->n[0])
  175. r->n[0]->p = r;
  176. if(r->n[1])
  177. r->n[1]->p = r;
  178. *del = or;
  179. balance(tp, p);
  180. return -(ob != 0 && (*tp)->bal == 0);
  181. }
  182. static void
  183. checkparents(Avl *a, Avl *p)
  184. {
  185. if(a == nil)
  186. return;
  187. if(a->p != p)
  188. print("bad parent\n");
  189. checkparents(a->n[0], a);
  190. checkparents(a->n[1], a);
  191. }
  192. struct Avltree
  193. {
  194. Avl *root;
  195. int (*cmp)(Avl*, Avl*);
  196. Avlwalk *walks;
  197. };
  198. struct Avlwalk
  199. {
  200. int started;
  201. int moved;
  202. Avlwalk *next;
  203. Avltree *tree;
  204. Avl *node;
  205. };
  206. Avltree*
  207. mkavltree(int (*cmp)(Avl*, Avl*))
  208. {
  209. Avltree *t;
  210. t = malloc(sizeof *t);
  211. if(t == nil)
  212. return nil;
  213. memset(t, 0, sizeof *t);
  214. t->cmp = cmp;
  215. return t;
  216. }
  217. void
  218. insertavl(Avltree *t, Avl *new, Avl **oldp)
  219. {
  220. *oldp = nil;
  221. _insertavl(&t->root, nil, new, t->cmp, oldp);
  222. }
  223. static Avl*
  224. findpredecessor(Avl *a)
  225. {
  226. if(a == nil)
  227. return nil;
  228. if(a->n[0] != nil){
  229. /* predecessor is rightmost descendant of left child */
  230. for(a = a->n[0]; a->n[1]; a = a->n[1])
  231. ;
  232. return a;
  233. }else{
  234. /* we're at a leaf, successor is a parent we enter from the right */
  235. while(a->p && a->p->n[0] == a)
  236. a = a->p;
  237. return a->p;
  238. }
  239. }
  240. static Avl*
  241. findsuccessor(Avl *a)
  242. {
  243. if(a == nil)
  244. return nil;
  245. if(a->n[1] != nil){
  246. /* successor is leftmost descendant of right child */
  247. for(a = a->n[1]; a->n[0]; a = a->n[0])
  248. ;
  249. return a;
  250. }else{
  251. /* we're at a leaf, successor is a parent we enter from the left going up */
  252. while(a->p && a->p->n[1] == a)
  253. a = a->p;
  254. return a->p;
  255. }
  256. }
  257. static Avl*
  258. _lookupavl(Avl *t, Avl *r, int (*cmp)(Avl*,Avl*), int neighbor)
  259. {
  260. int i;
  261. Avl *p;
  262. p = nil;
  263. if(t == nil)
  264. return nil;
  265. do{
  266. assert(t->p == p);
  267. if((i = canoncmp(cmp(r, t))) == 0)
  268. return t;
  269. p = t;
  270. t = t->n[(i+1)/2];
  271. }while(t);
  272. if(neighbor == 0)
  273. return nil;
  274. if(neighbor < 0)
  275. return i > 0 ? p : findpredecessor(p);
  276. return i < 0 ? p : findsuccessor(p);
  277. }
  278. Avl*
  279. searchavl(Avltree *t, Avl *key, int neighbor)
  280. {
  281. return _lookupavl(t->root, key, t->cmp, neighbor);
  282. }
  283. Avl*
  284. lookupavl(Avltree *t, Avl *key)
  285. {
  286. return _lookupavl(t->root, key, t->cmp, 0);
  287. }
  288. static void
  289. walkdel(Avl *a, void *v)
  290. {
  291. Avl *p;
  292. Avlwalk *w;
  293. Avltree *t;
  294. if(a == nil)
  295. return;
  296. p = findpredecessor(a);
  297. t = v;
  298. for(w = t->walks; w; w = w->next){
  299. if(w->node == a){
  300. /* back pointer to predecessor; not perfect but adequate */
  301. w->moved = 1;
  302. w->node = p;
  303. if(p == nil)
  304. w->started = 0;
  305. }
  306. }
  307. }
  308. void
  309. deleteavl(Avltree *t, Avl *key, Avl **oldp)
  310. {
  311. *oldp = nil;
  312. _deleteavl(&t->root, nil, key, t->cmp, oldp, walkdel, t);
  313. }
  314. Avlwalk*
  315. avlwalk(Avltree *t)
  316. {
  317. Avlwalk *w;
  318. w = malloc(sizeof *w);
  319. if(w == nil)
  320. return nil;
  321. memset(w, 0, sizeof *w);
  322. w->tree = t;
  323. w->next = t->walks;
  324. t->walks = w;
  325. return w;
  326. }
  327. Avl*
  328. avlnext(Avlwalk *w)
  329. {
  330. Avl *a;
  331. if(w->started==0){
  332. for(a = w->tree->root; a && a->n[0]; a = a->n[0])
  333. ;
  334. w->node = a;
  335. w->started = 1;
  336. }else{
  337. a = findsuccessor(w->node);
  338. if(a == w->node)
  339. abort();
  340. w->node = a;
  341. }
  342. return w->node;
  343. }
  344. Avl*
  345. avlprev(Avlwalk *w)
  346. {
  347. Avl *a;
  348. if(w->started == 0){
  349. for(a = w->tree->root; a && a->n[1]; a = a->n[1])
  350. ;
  351. w->node = a;
  352. w->started = 1;
  353. }else if(w->moved){
  354. w->moved = 0;
  355. return w->node;
  356. }else{
  357. a = findpredecessor(w->node);
  358. if(a == w->node)
  359. abort();
  360. w->node = a;
  361. }
  362. return w->node;
  363. }
  364. void
  365. endwalk(Avlwalk *w)
  366. {
  367. Avltree *t;
  368. Avlwalk **l;
  369. t = w->tree;
  370. for(l = &t->walks; *l; l = &(*l)->next){
  371. if(*l == w){
  372. *l = w->next;
  373. break;
  374. }
  375. }
  376. free(w);
  377. }
  378. static void
  379. walkavl(Avl *t, void (*f)(Avl*, void*), void *v)
  380. {
  381. if(t == nil)
  382. return;
  383. walkavl(t->n[0], f, v);
  384. f(t, v);
  385. walkavl(t->n[1], f, v);
  386. }