avl.c 6.6 KB

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