Tree.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2020 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial implementation and documentation
  15. *******************************************************************************/
  16. /** @file
  17. * \brief functions which apply to tree structures.
  18. *
  19. * These trees can hold data of any sort, pointed to by the content pointer of the
  20. * Node structure.
  21. * */
  22. #define TREE_C /* so that malloc/free/realloc aren't redefined by Heap.h */
  23. #include "Tree.h"
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include "Heap.h"
  28. int isRed(Node* aNode);
  29. int isBlack(Node* aNode);
  30. /*int TreeWalk(Node* curnode, int depth);*/
  31. /*int TreeMaxDepth(Tree *aTree);*/
  32. void TreeRotate(Tree* aTree, Node* curnode, int direction, int index);
  33. Node* TreeBAASub(Tree* aTree, Node* curnode, int which, int index);
  34. void TreeBalanceAfterAdd(Tree* aTree, Node* curnode, int index);
  35. void* TreeAddByIndex(Tree* aTree, void* content, size_t size, int index);
  36. Node* TreeFindIndex1(Tree* aTree, void* key, int index, int value);
  37. Node* TreeFindContentIndex(Tree* aTree, void* key, int index);
  38. Node* TreeMinimum(Node* curnode);
  39. Node* TreeSuccessor(Node* curnode);
  40. Node* TreeNextElementIndex(Tree* aTree, Node* curnode, int index);
  41. Node* TreeBARSub(Tree* aTree, Node* curnode, int which, int index);
  42. void TreeBalanceAfterRemove(Tree* aTree, Node* curnode, int index);
  43. void* TreeRemoveIndex(Tree* aTree, void* content, int index);
  44. void TreeInitializeNoMalloc(Tree* aTree, int(*compare)(void*, void*, int))
  45. {
  46. memset(aTree, '\0', sizeof(Tree));
  47. aTree->heap_tracking = 1;
  48. aTree->index[0].compare = compare;
  49. aTree->indexes = 1;
  50. }
  51. /**
  52. * Allocates and initializes a new tree structure.
  53. * @return a pointer to the new tree structure
  54. */
  55. Tree* TreeInitialize(int(*compare)(void*, void*, int))
  56. {
  57. #if defined(UNIT_TESTS) || defined(NO_HEAP_TRACKING)
  58. Tree* newt = malloc(sizeof(Tree));
  59. #else
  60. Tree* newt = mymalloc(__FILE__, __LINE__, sizeof(Tree));
  61. #endif
  62. if (newt)
  63. TreeInitializeNoMalloc(newt, compare);
  64. return newt;
  65. }
  66. void TreeAddIndex(Tree* aTree, int(*compare)(void*, void*, int))
  67. {
  68. aTree->index[aTree->indexes].compare = compare;
  69. ++(aTree->indexes);
  70. }
  71. void TreeFree(Tree* aTree)
  72. {
  73. #if defined(UNIT_TESTS) || defined(NO_HEAP_TRACKING)
  74. free(aTree);
  75. #else
  76. (aTree->heap_tracking) ? myfree(__FILE__, __LINE__, aTree) : free(aTree);
  77. #endif
  78. }
  79. #define LEFT 0
  80. #define RIGHT 1
  81. #if !defined(max)
  82. #define max(a, b) (a > b) ? a : b;
  83. #endif
  84. int isRed(Node* aNode)
  85. {
  86. return (aNode != NULL) && (aNode->red);
  87. }
  88. int isBlack(Node* aNode)
  89. {
  90. return (aNode == NULL) || (aNode->red == 0);
  91. }
  92. #if 0
  93. int TreeWalk(Node* curnode, int depth)
  94. {
  95. if (curnode)
  96. {
  97. int left = TreeWalk(curnode->child[LEFT], depth+1);
  98. int right = TreeWalk(curnode->child[RIGHT], depth+1);
  99. depth = max(left, right);
  100. if (curnode->red)
  101. {
  102. /*if (isRed(curnode->child[LEFT]) || isRed(curnode->child[RIGHT]))
  103. {
  104. printf("red/black tree violation %p\n", curnode->content);
  105. exit(-99);
  106. }*/;
  107. }
  108. }
  109. return depth;
  110. }
  111. int TreeMaxDepth(Tree *aTree)
  112. {
  113. int rc = TreeWalk(aTree->index[0].root, 0);
  114. /*if (aTree->root->red)
  115. {
  116. printf("root node should not be red %p\n", aTree->root->content);
  117. exit(-99);
  118. }*/
  119. return rc;
  120. }
  121. #endif
  122. void TreeRotate(Tree* aTree, Node* curnode, int direction, int index)
  123. {
  124. Node* other = curnode->child[!direction];
  125. curnode->child[!direction] = other->child[direction];
  126. if (other->child[direction] != NULL)
  127. other->child[direction]->parent = curnode;
  128. other->parent = curnode->parent;
  129. if (curnode->parent == NULL)
  130. aTree->index[index].root = other;
  131. else if (curnode == curnode->parent->child[direction])
  132. curnode->parent->child[direction] = other;
  133. else
  134. curnode->parent->child[!direction] = other;
  135. other->child[direction] = curnode;
  136. curnode->parent = other;
  137. }
  138. Node* TreeBAASub(Tree* aTree, Node* curnode, int which, int index)
  139. {
  140. Node* uncle = curnode->parent->parent->child[which];
  141. if (isRed(uncle))
  142. {
  143. curnode->parent->red = uncle->red = 0;
  144. curnode = curnode->parent->parent;
  145. curnode->red = 1;
  146. }
  147. else
  148. {
  149. if (curnode == curnode->parent->child[which])
  150. {
  151. curnode = curnode->parent;
  152. TreeRotate(aTree, curnode, !which, index);
  153. }
  154. curnode->parent->red = 0;
  155. curnode->parent->parent->red = 1;
  156. TreeRotate(aTree, curnode->parent->parent, which, index);
  157. }
  158. return curnode;
  159. }
  160. void TreeBalanceAfterAdd(Tree* aTree, Node* curnode, int index)
  161. {
  162. while (curnode && isRed(curnode->parent) && curnode->parent->parent)
  163. {
  164. if (curnode->parent == curnode->parent->parent->child[LEFT])
  165. curnode = TreeBAASub(aTree, curnode, RIGHT, index);
  166. else
  167. curnode = TreeBAASub(aTree, curnode, LEFT, index);
  168. }
  169. aTree->index[index].root->red = 0;
  170. }
  171. /**
  172. * Add an item to a tree
  173. * @param aTree the list to which the item is to be added
  174. * @param content the list item content itself
  175. * @param size the size of the element
  176. */
  177. void* TreeAddByIndex(Tree* aTree, void* content, size_t size, int index)
  178. {
  179. Node* curparent = NULL;
  180. Node* curnode = aTree->index[index].root;
  181. Node* newel = NULL;
  182. int left = 0;
  183. int result = 1;
  184. void* rc = NULL;
  185. while (curnode)
  186. {
  187. result = aTree->index[index].compare(curnode->content, content, 1);
  188. left = (result > 0);
  189. if (result == 0)
  190. break;
  191. else
  192. {
  193. curparent = curnode;
  194. curnode = curnode->child[left];
  195. }
  196. }
  197. if (result == 0)
  198. {
  199. if (aTree->allow_duplicates)
  200. goto exit; /* exit(-99); */
  201. else
  202. {
  203. newel = curnode;
  204. if (index == 0)
  205. aTree->size += (size - curnode->size);
  206. }
  207. }
  208. else
  209. {
  210. #if defined(UNIT_TESTS) || defined(NO_HEAP_TRACKING)
  211. newel = malloc(sizeof(Node));
  212. #else
  213. newel = (aTree->heap_tracking) ? mymalloc(__FILE__, __LINE__, sizeof(Node)) : malloc(sizeof(Node));
  214. #endif
  215. if (newel == NULL)
  216. goto exit;
  217. memset(newel, '\0', sizeof(Node));
  218. if (curparent)
  219. curparent->child[left] = newel;
  220. else
  221. aTree->index[index].root = newel;
  222. newel->parent = curparent;
  223. newel->red = 1;
  224. if (index == 0)
  225. {
  226. ++(aTree->count);
  227. aTree->size += size;
  228. }
  229. }
  230. newel->content = content;
  231. newel->size = size;
  232. rc = newel->content;
  233. TreeBalanceAfterAdd(aTree, newel, index);
  234. exit:
  235. return rc;
  236. }
  237. void* TreeAdd(Tree* aTree, void* content, size_t size)
  238. {
  239. void* rc = NULL;
  240. int i;
  241. for (i = 0; i < aTree->indexes; ++i)
  242. rc = TreeAddByIndex(aTree, content, size, i);
  243. return rc;
  244. }
  245. Node* TreeFindIndex1(Tree* aTree, void* key, int index, int value)
  246. {
  247. int result = 0;
  248. Node* curnode = aTree->index[index].root;
  249. while (curnode)
  250. {
  251. result = aTree->index[index].compare(curnode->content, key, value);
  252. if (result == 0)
  253. break;
  254. else
  255. curnode = curnode->child[result > 0];
  256. }
  257. return curnode;
  258. }
  259. Node* TreeFindIndex(Tree* aTree, void* key, int index)
  260. {
  261. return TreeFindIndex1(aTree, key, index, 0);
  262. }
  263. Node* TreeFindContentIndex(Tree* aTree, void* key, int index)
  264. {
  265. return TreeFindIndex1(aTree, key, index, 1);
  266. }
  267. Node* TreeFind(Tree* aTree, void* key)
  268. {
  269. return TreeFindIndex(aTree, key, 0);
  270. }
  271. Node* TreeMinimum(Node* curnode)
  272. {
  273. if (curnode)
  274. while (curnode->child[LEFT])
  275. curnode = curnode->child[LEFT];
  276. return curnode;
  277. }
  278. Node* TreeSuccessor(Node* curnode)
  279. {
  280. if (curnode->child[RIGHT])
  281. curnode = TreeMinimum(curnode->child[RIGHT]);
  282. else
  283. {
  284. Node* curparent = curnode->parent;
  285. while (curparent && curnode == curparent->child[RIGHT])
  286. {
  287. curnode = curparent;
  288. curparent = curparent->parent;
  289. }
  290. curnode = curparent;
  291. }
  292. return curnode;
  293. }
  294. Node* TreeNextElementIndex(Tree* aTree, Node* curnode, int index)
  295. {
  296. if (curnode == NULL)
  297. curnode = TreeMinimum(aTree->index[index].root);
  298. else
  299. curnode = TreeSuccessor(curnode);
  300. return curnode;
  301. }
  302. Node* TreeNextElement(Tree* aTree, Node* curnode)
  303. {
  304. return TreeNextElementIndex(aTree, curnode, 0);
  305. }
  306. Node* TreeBARSub(Tree* aTree, Node* curnode, int which, int index)
  307. {
  308. Node* sibling = curnode->parent->child[which];
  309. if (isRed(sibling))
  310. {
  311. sibling->red = 0;
  312. curnode->parent->red = 1;
  313. TreeRotate(aTree, curnode->parent, !which, index);
  314. sibling = curnode->parent->child[which];
  315. }
  316. if (!sibling)
  317. curnode = curnode->parent;
  318. else if (isBlack(sibling->child[!which]) && isBlack(sibling->child[which]))
  319. {
  320. sibling->red = 1;
  321. curnode = curnode->parent;
  322. }
  323. else
  324. {
  325. if (isBlack(sibling->child[which]))
  326. {
  327. sibling->child[!which]->red = 0;
  328. sibling->red = 1;
  329. TreeRotate(aTree, sibling, which, index);
  330. sibling = curnode->parent->child[which];
  331. }
  332. sibling->red = curnode->parent->red;
  333. curnode->parent->red = 0;
  334. sibling->child[which]->red = 0;
  335. TreeRotate(aTree, curnode->parent, !which, index);
  336. curnode = aTree->index[index].root;
  337. }
  338. return curnode;
  339. }
  340. void TreeBalanceAfterRemove(Tree* aTree, Node* curnode, int index)
  341. {
  342. while (curnode != aTree->index[index].root && isBlack(curnode))
  343. {
  344. /* curnode->content == NULL must equal curnode == NULL */
  345. if (((curnode->content) ? curnode : NULL) == curnode->parent->child[LEFT])
  346. curnode = TreeBARSub(aTree, curnode, RIGHT, index);
  347. else
  348. curnode = TreeBARSub(aTree, curnode, LEFT, index);
  349. }
  350. curnode->red = 0;
  351. }
  352. /**
  353. * Remove an item from a tree
  354. * @param aTree the list to which the item is to be added
  355. * @param curnode the list item content itself
  356. */
  357. void* TreeRemoveNodeIndex(Tree* aTree, Node* curnode, int index)
  358. {
  359. Node* redundant = curnode;
  360. Node* curchild = NULL;
  361. size_t size = curnode->size;
  362. void* content = curnode->content;
  363. /* if the node to remove has 0 or 1 children, it can be removed without involving another node */
  364. if (curnode->child[LEFT] && curnode->child[RIGHT]) /* 2 children */
  365. redundant = TreeSuccessor(curnode); /* now redundant must have at most one child */
  366. curchild = redundant->child[(redundant->child[LEFT] != NULL) ? LEFT : RIGHT];
  367. if (curchild) /* we could have no children at all */
  368. curchild->parent = redundant->parent;
  369. if (redundant->parent == NULL)
  370. aTree->index[index].root = curchild;
  371. else
  372. {
  373. if (redundant == redundant->parent->child[LEFT])
  374. redundant->parent->child[LEFT] = curchild;
  375. else
  376. redundant->parent->child[RIGHT] = curchild;
  377. }
  378. if (redundant != curnode)
  379. {
  380. curnode->content = redundant->content;
  381. curnode->size = redundant->size;
  382. }
  383. if (isBlack(redundant))
  384. {
  385. if (curchild == NULL)
  386. {
  387. if (redundant->parent)
  388. {
  389. Node temp;
  390. memset(&temp, '\0', sizeof(Node));
  391. temp.parent = redundant->parent;
  392. temp.red = 0;
  393. TreeBalanceAfterRemove(aTree, &temp, index);
  394. }
  395. }
  396. else
  397. TreeBalanceAfterRemove(aTree, curchild, index);
  398. }
  399. #if defined(UNIT_TESTS) || defined(NO_HEAP_TRACKING)
  400. free(redundant);
  401. #else
  402. (aTree->heap_tracking) ? myfree(__FILE__, __LINE__, redundant) : free(redundant);
  403. #endif
  404. if (index == 0)
  405. {
  406. aTree->size -= size;
  407. --(aTree->count);
  408. }
  409. return content;
  410. }
  411. /**
  412. * Remove an item from a tree
  413. * @param aTree the list to which the item is to be added
  414. * @param curnode the list item content itself
  415. */
  416. void* TreeRemoveIndex(Tree* aTree, void* content, int index)
  417. {
  418. Node* curnode = TreeFindContentIndex(aTree, content, index);
  419. if (curnode == NULL)
  420. return NULL;
  421. return TreeRemoveNodeIndex(aTree, curnode, index);
  422. }
  423. void* TreeRemove(Tree* aTree, void* content)
  424. {
  425. int i;
  426. void* rc = NULL;
  427. for (i = 0; i < aTree->indexes; ++i)
  428. rc = TreeRemoveIndex(aTree, content, i);
  429. return rc;
  430. }
  431. void* TreeRemoveKeyIndex(Tree* aTree, void* key, int index)
  432. {
  433. Node* curnode = TreeFindIndex(aTree, key, index);
  434. void* content = NULL;
  435. int i;
  436. if (curnode == NULL)
  437. return NULL;
  438. content = TreeRemoveNodeIndex(aTree, curnode, index);
  439. for (i = 0; i < aTree->indexes; ++i)
  440. {
  441. if (i != index)
  442. content = TreeRemoveIndex(aTree, content, i);
  443. }
  444. return content;
  445. }
  446. void* TreeRemoveKey(Tree* aTree, void* key)
  447. {
  448. return TreeRemoveKeyIndex(aTree, key, 0);
  449. }
  450. int TreeIntCompare(void* a, void* b, int content)
  451. {
  452. int i = *((int*)a);
  453. int j = *((int*)b);
  454. /* printf("comparing %d %d\n", *((int*)a), *((int*)b)); */
  455. return (i > j) ? -1 : (i == j) ? 0 : 1;
  456. }
  457. int TreePtrCompare(void* a, void* b, int content)
  458. {
  459. return (a > b) ? -1 : (a == b) ? 0 : 1;
  460. }
  461. int TreeStringCompare(void* a, void* b, int content)
  462. {
  463. return strcmp((char*)a, (char*)b);
  464. }
  465. #if defined(UNIT_TESTS)
  466. int check(Tree *t)
  467. {
  468. Node* curnode = NULL;
  469. int rc = 0;
  470. curnode = TreeNextElement(t, curnode);
  471. while (curnode)
  472. {
  473. Node* prevnode = curnode;
  474. curnode = TreeNextElement(t, curnode);
  475. if (prevnode && curnode && (*(int*)(curnode->content) < *(int*)(prevnode->content)))
  476. {
  477. printf("out of order %d < %d\n", *(int*)(curnode->content), *(int*)(prevnode->content));
  478. rc = 99;
  479. }
  480. }
  481. return rc;
  482. }
  483. int traverse(Tree *t, int lookfor)
  484. {
  485. Node* curnode = NULL;
  486. int rc = 0;
  487. printf("Traversing\n");
  488. curnode = TreeNextElement(t, curnode);
  489. /* printf("content int %d\n", *(int*)(curnode->content)); */
  490. while (curnode)
  491. {
  492. Node* prevnode = curnode;
  493. curnode = TreeNextElement(t, curnode);
  494. /* if (curnode)
  495. printf("content int %d\n", *(int*)(curnode->content)); */
  496. if (prevnode && curnode && (*(int*)(curnode->content) < *(int*)(prevnode->content)))
  497. {
  498. printf("out of order %d < %d\n", *(int*)(curnode->content), *(int*)(prevnode->content));
  499. }
  500. if (curnode && (lookfor == *(int*)(curnode->content)))
  501. printf("missing item %d actually found\n", lookfor);
  502. }
  503. printf("End traverse %d\n", rc);
  504. return rc;
  505. }
  506. int test(int limit)
  507. {
  508. int i, *ip, *todelete;
  509. Node* current = NULL;
  510. Tree* t = TreeInitialize(TreeIntCompare);
  511. int rc = 0;
  512. printf("Tree initialized\n");
  513. srand(time(NULL));
  514. ip = malloc(sizeof(int));
  515. *ip = 2;
  516. TreeAdd(t, (void*)ip, sizeof(int));
  517. check(t);
  518. i = 2;
  519. void* result = TreeRemove(t, (void*)&i);
  520. if (result)
  521. free(result);
  522. int actual[limit];
  523. for (i = 0; i < limit; i++)
  524. {
  525. void* replaced = NULL;
  526. ip = malloc(sizeof(int));
  527. *ip = rand();
  528. replaced = TreeAdd(t, (void*)ip, sizeof(int));
  529. if (replaced) /* duplicate */
  530. {
  531. free(replaced);
  532. actual[i] = -1;
  533. }
  534. else
  535. actual[i] = *ip;
  536. if (i==5)
  537. todelete = ip;
  538. printf("Tree element added %d\n", *ip);
  539. if (1 % 1000 == 0)
  540. {
  541. rc = check(t);
  542. printf("%d elements, check result %d\n", i+1, rc);
  543. if (rc != 0)
  544. return 88;
  545. }
  546. }
  547. check(t);
  548. for (i = 0; i < limit; i++)
  549. {
  550. int parm = actual[i];
  551. if (parm == -1)
  552. continue;
  553. Node* found = TreeFind(t, (void*)&parm);
  554. if (found)
  555. printf("Tree find %d %d\n", parm, *(int*)(found->content));
  556. else
  557. {
  558. printf("%d not found\n", parm);
  559. traverse(t, parm);
  560. return -2;
  561. }
  562. }
  563. check(t);
  564. for (i = limit -1; i >= 0; i--)
  565. {
  566. int parm = actual[i];
  567. void *found;
  568. if (parm == -1) /* skip duplicate */
  569. continue;
  570. found = TreeRemove(t, (void*)&parm);
  571. if (found)
  572. {
  573. printf("%d Tree remove %d %d\n", i, parm, *(int*)(found));
  574. free(found);
  575. }
  576. else
  577. {
  578. int count = 0;
  579. printf("%d %d not found\n", i, parm);
  580. traverse(t, parm);
  581. for (i = 0; i < limit; i++)
  582. if (actual[i] == parm)
  583. ++count;
  584. printf("%d occurs %d times\n", parm, count);
  585. return -2;
  586. }
  587. if (i % 1000 == 0)
  588. {
  589. rc = check(t);
  590. printf("%d elements, check result %d\n", i+1, rc);
  591. if (rc != 0)
  592. return 88;
  593. }
  594. }
  595. printf("finished\n");
  596. return 0;
  597. }
  598. int main(int argc, char *argv[])
  599. {
  600. int rc = 0;
  601. while (rc == 0)
  602. rc = test(999999);
  603. }
  604. #endif