y_bintree.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*----------------------------------------------------------------------------*\
  2. ===================================
  3. Y Sever Includes - Binary Tree Core
  4. ===================================
  5. Description:
  6. Provides functions to generate balanced binary search trees for efficient
  7. searching of large arrays by value. Left branch is less than, right branch
  8. is greater than or equal to for multiple matching values.
  9. Legal:
  10. Version: MPL 1.1
  11. The contents of this file are subject to the Mozilla Public License Version
  12. 1.1 (the "License"); you may not use this file except in compliance with
  13. the License. You may obtain a copy of the License at
  14. http://www.mozilla.org/MPL/
  15. Software distributed under the License is distributed on an "AS IS" basis,
  16. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  17. for the specific language governing rights and limitations under the
  18. License.
  19. The Original Code is the YSI binary tree include.
  20. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  21. Portions created by the Initial Developer are Copyright (C) 2011
  22. the Initial Developer. All Rights Reserved.
  23. Contributors:
  24. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  25. Thanks:
  26. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  27. ZeeX - Very productive conversations.
  28. koolk - IsPlayerinAreaEx code.
  29. TheAlpha - Danish translation.
  30. breadfish - German translation.
  31. Fireburn - Dutch translation.
  32. yom - French translation.
  33. 50p - Polish translation.
  34. Zamaroht - Spanish translation.
  35. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  36. for me to strive to better.
  37. Pixels^ - Running XScripters where the idea was born.
  38. Matite - Pestering me to release it and using it.
  39. Very special thanks to:
  40. Thiadmer - PAWN, whose limits continue to amaze me!
  41. Kye/Kalcor - SA:MP.
  42. SA:MP Team past, present and future - SA:MP.
  43. Version:
  44. 0.1.3
  45. Changelog:
  46. 12/08/07:
  47. Fixed a bug with empty trees.
  48. 14/04/07:
  49. Updated header documentation with more than changelog.
  50. 10/04/07:
  51. Added parents for easy deletion.
  52. Added node deletion code.
  53. 08/04/07:
  54. Added Bintree_Add()
  55. 24/03/07:
  56. First version.
  57. Functions:
  58. Public:
  59. -
  60. Core:
  61. Bintree_QSort - Custom implementaion of QSort to keep pointers.
  62. Bintree_SortHalf - Itteratively balances halves of an array.
  63. Stock:
  64. Bintree_Generate - Generates a balanced binary tree from given input.
  65. Bintree_Reset - Resets a position in a tree.
  66. Bintree_FindValue - Finds the pointer for a value in the tree.
  67. Bintree_Add - Adds an item to a generated tree.
  68. Bintree_Delete - Removes an item from a tree.
  69. Bintree_UpdatePointers - Updates the pointers after a target change.
  70. Static:
  71. Bintree_Compress - Removes space from an altered tree.
  72. Bintree_FindMin - Finds the smallest value on a branch.
  73. Bintree_FindMax - Finds the largest value on a branch.
  74. Inline:
  75. Bintree_Sort - Entry point for Bintree_QSort.
  76. Bintree_Fill - Entry point for Bintree_SortHalf.
  77. API:
  78. -
  79. Callbacks:
  80. -
  81. Definitions:
  82. BINTREE_NO_BRANCH - Nowhere to go from the number in required direction.
  83. BINTREE_NOT_FOUND - Failure return.
  84. Enums:
  85. E_BINTREE_TREE - Structure of a leaf of a binary tree.
  86. E_BINTREE_INPUT - Structure of an array of data to be added to a tree.
  87. Macros:
  88. -
  89. Tags:
  90. Bintree - Binary tree type.
  91. Variables:
  92. Global:
  93. -
  94. Static:
  95. -
  96. Commands:
  97. -
  98. Compile options:
  99. -
  100. Operators:
  101. -
  102. \*----------------------------------------------------------------------------*/
  103. #include "internal\y_version"
  104. #include "y_debug"
  105. #include "y_utils"
  106. #define BINTREE_NO_BRANCH -1
  107. #define BINTREE_NOT_FOUND -1
  108. // If this ever changes, update the size reference in y_users.
  109. enum E_BINTREE_TREE
  110. {
  111. E_BINTREE_TREE_VALUE,
  112. E_BINTREE_TREE_LEFT,
  113. E_BINTREE_TREE_RIGHT,
  114. E_BINTREE_TREE_PARENT,
  115. E_BINTREE_TREE_POINTER
  116. }
  117. enum E_BINTREE_INPUT
  118. {
  119. E_BINTREE_INPUT_VALUE,
  120. E_BINTREE_INPUT_POINTER
  121. }
  122. //#define leafs<%1> %1][E_BINTREE_TREE
  123. //#define Bintree:%1[%2] Bintree:%1[%2][E_BINTREE_TREE]
  124. #define BinaryTree:%1<%2> Bintree:%1[%2][E_BINTREE_TREE]
  125. // Update at a later date...
  126. #define Bintree_DisplayOutput(%0) "<bintree output>"
  127. #define Bintree_DisplayInput(%0) "<bintree input>"
  128. /*----------------------------------------------------------------------------*\
  129. Function:
  130. Bintree_Sort
  131. Params:
  132. input[][E_BINTREE_INPUT] - Data to sort.
  133. size - Size of data to sort.
  134. Return:
  135. -
  136. Notes:
  137. Entry point for Bintree_QSort.
  138. \*----------------------------------------------------------------------------*/
  139. #define Bintree_Sort(%1,%2) \
  140. Bintree_QSort((%1), 0, (%2) - 1)
  141. /*----------------------------------------------------------------------------*\
  142. Function:
  143. Bintree_Fill
  144. Params:
  145. BinaryTree:output<> - Destination for balanced tree.
  146. data[][E_BINTREE_INPUT] - Source data.
  147. size - Size of data.
  148. Return:
  149. Bintree_SortHalf.
  150. Notes:
  151. Entry point for Bintree_SortHalf.
  152. \*----------------------------------------------------------------------------*/
  153. #define Bintree_Fill(%1,%2,%3) \
  154. Bintree_SortHalf((%1), (%2), 0, (%3), 0, BINTREE_NO_BRANCH)
  155. /*----------------------------------------------------------------------------*\
  156. Function:
  157. Bintree_Generate
  158. Params:
  159. BinaryTree:output<> - Binary tree to store the data in.
  160. input[][E_BINTREE_INPUT] - Input data to get the data from.
  161. size - Number of items to sort.
  162. Return:
  163. -
  164. Notes:
  165. Just calls the sort and fill routines.
  166. \*----------------------------------------------------------------------------*/
  167. stock Bintree_Generate(BinaryTree:output<>, input[][E_BINTREE_INPUT], size)
  168. {
  169. P:3("Bintree_Generate called: %s, %s, %i", Bintree_DisplayOutput(output), Bintree_DisplayInput(input), size);
  170. if (!size)
  171. {
  172. output[0][E_BINTREE_TREE_PARENT] = BINTREE_NO_BRANCH;
  173. output[0][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  174. output[0][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  175. return 0;
  176. }
  177. Bintree_Sort(input, size);
  178. Bintree_Fill(output, input, size);
  179. return 1;
  180. }
  181. /*----------------------------------------------------------------------------*\
  182. Function:
  183. Bintree_Reset
  184. Params:
  185. BinaryTree:tree<> - Array to reset.
  186. pointer - Position to reset.
  187. Return:
  188. -
  189. Notes:
  190. Initialises the array for use.
  191. \*----------------------------------------------------------------------------*/
  192. stock Bintree_Reset(BinaryTree:tree<>, pointer = 0)
  193. {
  194. P:3("Bintree_Reset called: %s, %i", Bintree_DisplayOutput(tree), pointer);
  195. tree[pointer][E_BINTREE_TREE_VALUE] = 0;
  196. tree[pointer][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  197. tree[pointer][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  198. tree[pointer][E_BINTREE_TREE_PARENT] = BINTREE_NO_BRANCH;
  199. tree[pointer][E_BINTREE_TREE_POINTER] = BINTREE_NOT_FOUND;
  200. }
  201. /*----------------------------------------------------------------------------*\
  202. Function:
  203. Bintree_FindValue
  204. Params:
  205. BinaryTree:tree<> - Tree to find the data in.
  206. value - Value to search for.
  207. &cont - Start point.
  208. &old - The last real leaf.
  209. Return:
  210. -
  211. Notes:
  212. Itterates through the array following the various paths till it locates
  213. the value provided or reaches a dead end. If the current value is greater
  214. than the search value, the search goes left, otherwise right.
  215. If cont is not -1 the search will start from the data pointed to by the
  216. data pointed to by conts' right path, this is to allow collisions to be
  217. passed over if you want a subsequent one.
  218. \*----------------------------------------------------------------------------*/
  219. stock Bintree_FindValue(BinaryTree:tree<>, value, &cont = 0, &old = 0)
  220. {
  221. P:3("Bintree_FindValue called: %s, %i, %i, %i", Bintree_DisplayOutput(tree), value, cont, old);
  222. new
  223. treeValue;
  224. while (cont != BINTREE_NO_BRANCH)
  225. {
  226. P:7("Bintree_FindValue: search %d %d %d %d", cont, old, tree[cont][E_BINTREE_TREE_VALUE], value);
  227. old = cont;
  228. treeValue = tree[old][E_BINTREE_TREE_VALUE];
  229. if (value < treeValue) cont = tree[old][E_BINTREE_TREE_LEFT];
  230. else
  231. {
  232. cont = tree[old][E_BINTREE_TREE_RIGHT];
  233. if (value == treeValue)
  234. {
  235. return tree[old][E_BINTREE_TREE_POINTER];
  236. }
  237. }
  238. }
  239. return BINTREE_NOT_FOUND;
  240. }
  241. /*----------------------------------------------------------------------------*\
  242. Function:
  243. Bintree_QSort
  244. Params:
  245. numbers[][E_BINTREE_INPUT] - Data to sort.
  246. left - Start index.
  247. right - End index.
  248. Return:
  249. -
  250. Notes:
  251. Custom version of QSort (see YSI_misc) allows for E_BINTREE_INPUT data
  252. types, preserving the relative pointers for the sorted data.
  253. \*----------------------------------------------------------------------------*/
  254. stock Bintree_QSort(numbers[][E_BINTREE_INPUT], left, right)
  255. {
  256. P:3("Bintree_QSort called: %s, %i, %i", Bintree_DisplayInput(numbers), left, right);
  257. new
  258. pivot = numbers[left][E_BINTREE_INPUT_VALUE],
  259. pointer = numbers[left][E_BINTREE_INPUT_POINTER],
  260. l_hold = left,
  261. r_hold = right;
  262. while (left < right)
  263. {
  264. while ((numbers[right][E_BINTREE_INPUT_VALUE] >= pivot) && (left < right)) right--;
  265. if (left != right)
  266. {
  267. numbers[left][E_BINTREE_INPUT_VALUE] = numbers[right][E_BINTREE_INPUT_VALUE];
  268. numbers[left][E_BINTREE_INPUT_POINTER] = numbers[right][E_BINTREE_INPUT_POINTER];
  269. left++;
  270. }
  271. while ((numbers[left][E_BINTREE_INPUT_VALUE] <= pivot) && (left < right)) left++;
  272. if (left != right)
  273. {
  274. numbers[right][E_BINTREE_INPUT_VALUE] = numbers[left][E_BINTREE_INPUT_VALUE];
  275. numbers[right][E_BINTREE_INPUT_POINTER] = numbers[left][E_BINTREE_INPUT_POINTER];
  276. right--;
  277. }
  278. }
  279. numbers[left][E_BINTREE_INPUT_VALUE] = pivot;
  280. numbers[left][E_BINTREE_INPUT_POINTER] = pointer;
  281. pivot = left;
  282. left = l_hold;
  283. right = r_hold;
  284. if (left < pivot) Bintree_QSort(numbers, left, pivot - 1);
  285. if (right > pivot) Bintree_QSort(numbers, pivot + 1, right);
  286. }
  287. /*----------------------------------------------------------------------------*\
  288. Function:
  289. Bintree_SortHalf
  290. Params:
  291. BinaryTree:output<> - Destination array.
  292. data[][E_BINTREE_INPUT] - Source array.
  293. index - Start index of the source for processing.
  294. upper - End index of the source for processing.
  295. offset - Current offset in the destination array for writing.
  296. Return:
  297. Size of balanced tree.
  298. Notes:
  299. Recursively calls itself. Bisects the passed array and passed each half
  300. back to itself, with the middle value of each half being the left and
  301. right branches of the middle value of the passed array (which isn't
  302. included in either bisected half). This is itterative so those are again
  303. split and again split. If the passed array is only one or two elements
  304. big the behaviour is set and hardcoded.
  305. Equal values SHOULD branch right, the code is designed for this however
  306. the generation is not fully tested (it mostly branches right but adjacent
  307. after bisecting values haven't been tested).
  308. Based on code written for PHP by me.
  309. \*----------------------------------------------------------------------------*/
  310. stock Bintree_SortHalf(BinaryTree:output<>, data[][E_BINTREE_INPUT], index, upper, offset, parent)
  311. {
  312. P:3("Bintree_SortHalf called: %s, %s, %i, %i, %i, %i", Bintree_DisplayOutput(output), Bintree_DisplayInput(data), index, upper, offset, parent);
  313. new
  314. num = upper - index;
  315. if (!num) return offset;
  316. if (num == 1)
  317. {
  318. output[offset][E_BINTREE_TREE_VALUE] = data[index][E_BINTREE_INPUT_VALUE];
  319. output[offset][E_BINTREE_TREE_POINTER] = data[index][E_BINTREE_INPUT_POINTER];
  320. output[offset][E_BINTREE_TREE_PARENT] = parent;
  321. output[offset][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  322. output[offset][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  323. }
  324. else if (num == 2)
  325. {
  326. P:3("Bintree_SortHalf: adding %i %i %i", index, data[index][E_BINTREE_INPUT_VALUE], data[index + 1][E_BINTREE_INPUT_VALUE]);
  327. output[offset][E_BINTREE_TREE_VALUE] = data[index][E_BINTREE_INPUT_VALUE];
  328. output[offset][E_BINTREE_TREE_POINTER] = data[index][E_BINTREE_INPUT_POINTER];
  329. output[offset][E_BINTREE_TREE_PARENT] = parent;
  330. output[offset][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  331. output[offset][E_BINTREE_TREE_RIGHT] = offset + 1;
  332. ++offset;
  333. ++index;
  334. output[offset][E_BINTREE_TREE_VALUE] = data[index][E_BINTREE_INPUT_VALUE];
  335. output[offset][E_BINTREE_TREE_POINTER] = data[index][E_BINTREE_INPUT_POINTER];
  336. output[offset][E_BINTREE_TREE_PARENT] = offset - 1;
  337. output[offset][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  338. output[offset][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  339. }
  340. else
  341. {
  342. new
  343. half = num / 2,
  344. off = half + index,
  345. right;
  346. while (off && data[off][E_BINTREE_INPUT_VALUE] == data[off - 1][E_BINTREE_INPUT_VALUE]) off--;
  347. right = Bintree_SortHalf(output, data, index, off, offset + 1, offset);
  348. output[offset][E_BINTREE_TREE_VALUE] = data[off][E_BINTREE_INPUT_VALUE];
  349. output[offset][E_BINTREE_TREE_POINTER] = data[off][E_BINTREE_INPUT_POINTER];
  350. output[offset][E_BINTREE_TREE_PARENT] = parent;
  351. output[offset][E_BINTREE_TREE_LEFT] = offset + 1;
  352. output[offset][E_BINTREE_TREE_RIGHT] = right;
  353. return Bintree_SortHalf(output, data, off + 1, upper, right, offset);
  354. }
  355. return offset + 1;
  356. }
  357. /*----------------------------------------------------------------------------*\
  358. Function:
  359. Bintree_Add
  360. Params:
  361. BinaryTree:data<> - Array to add to.
  362. pointer - Pointer to add.
  363. value - Value to add.
  364. offset - Location in the array to store the data.
  365. maxsize - Size of data.
  366. Return:
  367. Next free location
  368. Notes:
  369. -
  370. native Bintree_Add(BinaryTree:tree<>, pointer, value, offset, maxsize = sizeof (data));
  371. \*----------------------------------------------------------------------------*/
  372. stock Bintree_Add(BinaryTree:data<>, pointer, value, offset, maxsize = sizeof (data))
  373. {
  374. P:3("Bintree_Add called: %s, %i, %i, %i, %i", Bintree_DisplayOutput(data), pointer, value, offset, maxsize);
  375. if (offset >= maxsize) return BINTREE_NOT_FOUND;
  376. if (offset)
  377. {
  378. new
  379. leaf,
  380. old;
  381. while (Bintree_FindValue(data, value, leaf, old) != BINTREE_NOT_FOUND) continue;
  382. //Bintree_Reset(data, offset);
  383. if (value < data[old][E_BINTREE_TREE_VALUE]) data[old][E_BINTREE_TREE_LEFT] = offset;
  384. else data[old][E_BINTREE_TREE_RIGHT] = offset;
  385. data[offset][E_BINTREE_TREE_PARENT] = old;
  386. data[offset][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  387. data[offset][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  388. data[offset][E_BINTREE_TREE_VALUE] = value;
  389. data[offset][E_BINTREE_TREE_POINTER] = pointer;
  390. return offset + 1;
  391. }
  392. else
  393. {
  394. data[0][E_BINTREE_TREE_PARENT] = BINTREE_NO_BRANCH;
  395. data[0][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  396. data[0][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  397. data[0][E_BINTREE_TREE_VALUE] = value;
  398. data[0][E_BINTREE_TREE_POINTER] = pointer;
  399. return 1;
  400. }
  401. }
  402. /*----------------------------------------------------------------------------*\
  403. Function:
  404. Bintree_Delete
  405. Params:
  406. BinaryTree:tree<> - Data.
  407. index - Index to remove.
  408. count - Number of binary tree items.
  409. Return:
  410. -
  411. Notes:
  412. The left branch is usually larger due to the division
  413. method used so we start there. Even though right is
  414. >= and left is only < in even sized arrays the greater
  415. chunk (unless there's only 2 items) goes left.
  416. Called itteratively to ensure branches are maintained.
  417. \*----------------------------------------------------------------------------*/
  418. stock Bintree_Delete(BinaryTree:source<>, index, count)
  419. {
  420. P:3("Bintree_Delete called: %s, %i, %i", Bintree_DisplayOutput(source), index, count);
  421. new
  422. branch,
  423. old = index;
  424. while (TRUE)
  425. {
  426. if ((branch = source[old][E_BINTREE_TREE_LEFT]) != BINTREE_NO_BRANCH) branch = Bintree_FindMax(source, branch);
  427. else if ((branch = source[old][E_BINTREE_TREE_RIGHT]) != BINTREE_NO_BRANCH) branch = Bintree_FindMin(source, branch);
  428. else
  429. {
  430. if ((branch = source[old][E_BINTREE_TREE_PARENT]) != BINTREE_NO_BRANCH)
  431. {
  432. if (source[branch][E_BINTREE_TREE_LEFT] == old) source[branch][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  433. else source[branch][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  434. }
  435. return Bintree_Compress(source, old, count);
  436. }
  437. new
  438. value = source[old][E_BINTREE_TREE_VALUE],
  439. pointer = source[old][E_BINTREE_TREE_POINTER];
  440. source[old][E_BINTREE_TREE_VALUE] = source[branch][E_BINTREE_TREE_VALUE];
  441. source[old][E_BINTREE_TREE_POINTER] = source[branch][E_BINTREE_TREE_POINTER];
  442. source[branch][E_BINTREE_TREE_VALUE] = value;
  443. source[branch][E_BINTREE_TREE_POINTER] = pointer;
  444. old = branch;
  445. }
  446. return BINTREE_NO_BRANCH;
  447. }
  448. /*----------------------------------------------------------------------------*\
  449. Function:
  450. Bintree_Compress
  451. Params:
  452. BinaryTree:tree<> - Array to compress.
  453. index - Point to start at.
  454. count - Number of items total.
  455. Return:
  456. -
  457. Notes:
  458. -
  459. \*----------------------------------------------------------------------------*/
  460. static stock Bintree_Compress(BinaryTree:data<>, index, count)
  461. {
  462. P:4("Bintree_Compress called: %s, %i, %i", Bintree_DisplayOutput(data), index, count);
  463. new
  464. index2 = index + 1;
  465. while (index < count)
  466. {
  467. new
  468. left = (data[index][E_BINTREE_TREE_LEFT] = data[index2][E_BINTREE_TREE_LEFT]),
  469. right = (data[index][E_BINTREE_TREE_RIGHT] = data[index2][E_BINTREE_TREE_RIGHT]),
  470. parent = (data[index][E_BINTREE_TREE_PARENT] = data[index2][E_BINTREE_TREE_PARENT]);
  471. data[index][E_BINTREE_TREE_VALUE] = data[index2][E_BINTREE_TREE_VALUE];
  472. data[index][E_BINTREE_TREE_POINTER] = data[index2][E_BINTREE_TREE_POINTER];
  473. if (left != BINTREE_NO_BRANCH) data[left][E_BINTREE_TREE_PARENT] = index;
  474. if (right != BINTREE_NO_BRANCH) data[right][E_BINTREE_TREE_PARENT] = index;
  475. if (parent != BINTREE_NO_BRANCH)
  476. {
  477. if (data[parent][E_BINTREE_TREE_LEFT] == index2) data[parent][E_BINTREE_TREE_LEFT] = index;
  478. else if (data[parent][E_BINTREE_TREE_RIGHT] == index2) data[parent][E_BINTREE_TREE_RIGHT] = index;
  479. }
  480. index++;
  481. index2++;
  482. }
  483. return count - 1;
  484. }
  485. /*----------------------------------------------------------------------------*\
  486. Function:
  487. Bintree_FindMin
  488. Params:
  489. BinaryTree:data<> - Array to search.
  490. offset - Start of branch to search.
  491. Return:
  492. -
  493. Notes:
  494. Finds the smallest value on a branch
  495. \*----------------------------------------------------------------------------*/
  496. static stock Bintree_FindMin(BinaryTree:data<>, offset)
  497. {
  498. P:4("Bintree_FindMin called: %s, %i", Bintree_DisplayOutput(data), offset);
  499. new
  500. branch;
  501. while ((branch = data[offset][E_BINTREE_TREE_LEFT]) != BINTREE_NO_BRANCH) offset = branch;
  502. return offset;
  503. }
  504. /*----------------------------------------------------------------------------*\
  505. Function:
  506. Bintree_FindMax
  507. Params:
  508. BinaryTree:data<> - Array to search.
  509. offset - Start of branch to search.
  510. Return:
  511. -
  512. Notes:
  513. Finds the largest value on a branch
  514. \*----------------------------------------------------------------------------*/
  515. static stock Bintree_FindMax(BinaryTree:data<>, offset)
  516. {
  517. P:4("Bintree_FindMax called: %s, %i", Bintree_DisplayOutput(data), offset);
  518. new
  519. branch;
  520. while ((branch = data[offset][E_BINTREE_TREE_RIGHT]) != BINTREE_NO_BRANCH) offset = branch;
  521. return offset;
  522. }
  523. /*----------------------------------------------------------------------------*\
  524. Function:
  525. Bintree_UpdatePointers
  526. Params:
  527. BinaryTree:data<> - Data to modify.
  528. offset - Pointer to modify values after.
  529. mod - Value to modify by.
  530. Return:
  531. -
  532. Notes:
  533. Used for updating pointers when the target data has been modifed (i.e. a
  534. value has been removed from the array and the array shifted).
  535. \*----------------------------------------------------------------------------*/
  536. stock Bintree_UpdatePointers(BinaryTree:data<>, offset, size, mod = -1)
  537. {
  538. P:3("Bintree_UpdatePointers called: %s, %i, %i, %i", Bintree_DisplayOutput(data), offset, size, mod);
  539. for (new i = 0; i < size; i++)
  540. {
  541. if (data[i][E_BINTREE_TREE_POINTER] > offset) data[i][E_BINTREE_TREE_POINTER] += mod;
  542. }
  543. }