y_bintree.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. </remarks>
  103. \**--------------------------------------------------------------------------**/
  104. #include "internal\y_version"
  105. #include "y_debug"
  106. #include "y_utils"
  107. #define BINTREE_NO_BRANCH -1
  108. #define BINTREE_NOT_FOUND -1
  109. // If this ever changes, update the size reference in y_users.
  110. enum E_BINTREE_TREE
  111. {
  112. E_BINTREE_TREE_VALUE,
  113. E_BINTREE_TREE_LEFT,
  114. E_BINTREE_TREE_RIGHT,
  115. E_BINTREE_TREE_PARENT,
  116. E_BINTREE_TREE_POINTER
  117. }
  118. enum E_BINTREE_INPUT
  119. {
  120. E_BINTREE_INPUT_VALUE,
  121. E_BINTREE_INPUT_POINTER
  122. }
  123. //#define leafs<%1> %1][E_BINTREE_TREE
  124. //#define Bintree:%1[%2] Bintree:%1[%2][E_BINTREE_TREE]
  125. #define BinaryTree:%1<%2> Bintree:%1[%2][E_BINTREE_TREE]
  126. // Update at a later date...
  127. #define Bintree_DisplayOutput(%0) "<bintree output>"
  128. #define Bintree_DisplayInput(%0) "<bintree input>"
  129. /**--------------------------------------------------------------------------**\
  130. <summary>Bintree_Sort</summary>
  131. <param name="input[][E_BINTREE_INPUT]">Data to sort.</param>
  132. <param name="size">Size of data to sort.</param>
  133. <returns>
  134. -
  135. </returns>
  136. <remarks>
  137. Entry point for Bintree_QSort.
  138. </remarks>
  139. \**--------------------------------------------------------------------------**/
  140. #define Bintree_Sort(%1,%2) \
  141. Bintree_QSort((%1), 0, (%2) - 1)
  142. /**--------------------------------------------------------------------------**\
  143. <summary>Bintree_Fill</summary>
  144. <param name="BinaryTree:output<>">Destination for balanced tree.</param>
  145. <param name="data[][E_BINTREE_INPUT]">Source data.</param>
  146. <param name="size">Size of data.</param>
  147. <returns>
  148. Bintree_SortHalf.
  149. </returns>
  150. <remarks>
  151. Entry point for Bintree_SortHalf.
  152. </remarks>
  153. \**--------------------------------------------------------------------------**/
  154. #define Bintree_Fill(%1,%2,%3) \
  155. Bintree_SortHalf((%1), (%2), 0, (%3), 0, BINTREE_NO_BRANCH)
  156. /**--------------------------------------------------------------------------**\
  157. <summary>Bintree_Generate</summary>
  158. <param name="BinaryTree:output<>">Binary tree to store the data in.</param>
  159. <param name="input[][E_BINTREE_INPUT]">Input data to get the data from.</param>
  160. <param name="size">Number of items to sort.</param>
  161. <returns>
  162. -
  163. </returns>
  164. <remarks>
  165. Just calls the sort and fill routines.
  166. </remarks>
  167. \**--------------------------------------------------------------------------**/
  168. stock Bintree_Generate(BinaryTree:output<>, input[][E_BINTREE_INPUT], size)
  169. {
  170. P:3("Bintree_Generate called: %s, %s, %i", Bintree_DisplayOutput(output), Bintree_DisplayInput(input), size);
  171. if (!size)
  172. {
  173. output[0][E_BINTREE_TREE_PARENT] = BINTREE_NO_BRANCH;
  174. output[0][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  175. output[0][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  176. return 0;
  177. }
  178. Bintree_Sort(input, size);
  179. Bintree_Fill(output, input, size);
  180. return 1;
  181. }
  182. /**--------------------------------------------------------------------------**\
  183. <summary>Bintree_Reset</summary>
  184. <param name="BinaryTree:tree<>">Array to reset.</param>
  185. <param name="pointer">Position to reset.</param>
  186. <returns>
  187. -
  188. </returns>
  189. <remarks>
  190. Initialises the array for use.
  191. </remarks>
  192. \**--------------------------------------------------------------------------**/
  193. stock Bintree_Reset(BinaryTree:tree<>, pointer = 0)
  194. {
  195. P:3("Bintree_Reset called: %s, %i", Bintree_DisplayOutput(tree), pointer);
  196. tree[pointer][E_BINTREE_TREE_VALUE] = 0;
  197. tree[pointer][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  198. tree[pointer][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  199. tree[pointer][E_BINTREE_TREE_PARENT] = BINTREE_NO_BRANCH;
  200. tree[pointer][E_BINTREE_TREE_POINTER] = BINTREE_NOT_FOUND;
  201. }
  202. /**--------------------------------------------------------------------------**\
  203. <summary>Bintree_FindValue</summary>
  204. <param name="BinaryTree:tree<>">Tree to find the data in.</param>
  205. <param name="value">Value to search for.</param>
  206. <param name="&cont">Start point.</param>
  207. <param name="&old">The last real leaf.</param>
  208. <returns>
  209. -
  210. </returns>
  211. <remarks>
  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. </remarks>
  219. \**--------------------------------------------------------------------------**/
  220. stock Bintree_FindValue(BinaryTree:tree<>, value, &cont = 0, &old = 0)
  221. {
  222. P:3("Bintree_FindValue called: %s, %i, %i, %i", Bintree_DisplayOutput(tree), value, cont, old);
  223. new
  224. treeValue;
  225. while (cont != BINTREE_NO_BRANCH)
  226. {
  227. P:7("Bintree_FindValue: search %d %d %d %d", cont, old, tree[cont][E_BINTREE_TREE_VALUE], value);
  228. old = cont;
  229. treeValue = tree[old][E_BINTREE_TREE_VALUE];
  230. if (value < treeValue) cont = tree[old][E_BINTREE_TREE_LEFT];
  231. else
  232. {
  233. cont = tree[old][E_BINTREE_TREE_RIGHT];
  234. if (value == treeValue)
  235. {
  236. return tree[old][E_BINTREE_TREE_POINTER];
  237. }
  238. }
  239. }
  240. return BINTREE_NOT_FOUND;
  241. }
  242. /**--------------------------------------------------------------------------**\
  243. <summary>Bintree_QSort</summary>
  244. <param name="numbers[][E_BINTREE_INPUT]">Data to sort.</param>
  245. <param name="left">Start index.</param>
  246. <param name="right">End index.</param>
  247. <returns>
  248. -
  249. </returns>
  250. <remarks>
  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. </remarks>
  254. \**--------------------------------------------------------------------------**/
  255. stock Bintree_QSort(numbers[][E_BINTREE_INPUT], left, right)
  256. {
  257. P:3("Bintree_QSort called: %s, %i, %i", Bintree_DisplayInput(numbers), left, right);
  258. new
  259. pivot = numbers[left][E_BINTREE_INPUT_VALUE],
  260. pointer = numbers[left][E_BINTREE_INPUT_POINTER],
  261. l_hold = left,
  262. r_hold = right;
  263. while (left < right)
  264. {
  265. while ((numbers[right][E_BINTREE_INPUT_VALUE] >= pivot) && (left < right)) right--;
  266. if (left != right)
  267. {
  268. numbers[left][E_BINTREE_INPUT_VALUE] = numbers[right][E_BINTREE_INPUT_VALUE];
  269. numbers[left][E_BINTREE_INPUT_POINTER] = numbers[right][E_BINTREE_INPUT_POINTER];
  270. left++;
  271. }
  272. while ((numbers[left][E_BINTREE_INPUT_VALUE] <= pivot) && (left < right)) left++;
  273. if (left != right)
  274. {
  275. numbers[right][E_BINTREE_INPUT_VALUE] = numbers[left][E_BINTREE_INPUT_VALUE];
  276. numbers[right][E_BINTREE_INPUT_POINTER] = numbers[left][E_BINTREE_INPUT_POINTER];
  277. right--;
  278. }
  279. }
  280. numbers[left][E_BINTREE_INPUT_VALUE] = pivot;
  281. numbers[left][E_BINTREE_INPUT_POINTER] = pointer;
  282. pivot = left;
  283. left = l_hold;
  284. right = r_hold;
  285. if (left < pivot) Bintree_QSort(numbers, left, pivot - 1);
  286. if (right > pivot) Bintree_QSort(numbers, pivot + 1, right);
  287. }
  288. /**--------------------------------------------------------------------------**\
  289. <summary>Bintree_SortHalf</summary>
  290. <param name="BinaryTree:output<>">Destination array.</param>
  291. <param name="data[][E_BINTREE_INPUT]">Source array.</param>
  292. <param name="index">Start index of the source for processing.</param>
  293. <param name="upper">End index of the source for processing.</param>
  294. <param name="offset">Current offset in the destination array for writing.</param>
  295. <returns>
  296. Size of balanced tree.
  297. </returns>
  298. <remarks>
  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. </remarks>
  310. \**--------------------------------------------------------------------------**/
  311. stock Bintree_SortHalf(BinaryTree:output<>, data[][E_BINTREE_INPUT], index, upper, offset, parent)
  312. {
  313. P:3("Bintree_SortHalf called: %s, %s, %i, %i, %i, %i", Bintree_DisplayOutput(output), Bintree_DisplayInput(data), index, upper, offset, parent);
  314. new
  315. num = upper - index;
  316. if (!num) return offset;
  317. if (num == 1)
  318. {
  319. output[offset][E_BINTREE_TREE_VALUE] = data[index][E_BINTREE_INPUT_VALUE];
  320. output[offset][E_BINTREE_TREE_POINTER] = data[index][E_BINTREE_INPUT_POINTER];
  321. output[offset][E_BINTREE_TREE_PARENT] = parent;
  322. output[offset][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  323. output[offset][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  324. }
  325. else if (num == 2)
  326. {
  327. P:3("Bintree_SortHalf: adding %i %i %i", index, data[index][E_BINTREE_INPUT_VALUE], data[index + 1][E_BINTREE_INPUT_VALUE]);
  328. output[offset][E_BINTREE_TREE_VALUE] = data[index][E_BINTREE_INPUT_VALUE];
  329. output[offset][E_BINTREE_TREE_POINTER] = data[index][E_BINTREE_INPUT_POINTER];
  330. output[offset][E_BINTREE_TREE_PARENT] = parent;
  331. output[offset][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  332. output[offset][E_BINTREE_TREE_RIGHT] = offset + 1;
  333. ++offset;
  334. ++index;
  335. output[offset][E_BINTREE_TREE_VALUE] = data[index][E_BINTREE_INPUT_VALUE];
  336. output[offset][E_BINTREE_TREE_POINTER] = data[index][E_BINTREE_INPUT_POINTER];
  337. output[offset][E_BINTREE_TREE_PARENT] = offset - 1;
  338. output[offset][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  339. output[offset][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  340. }
  341. else
  342. {
  343. new
  344. half = num / 2,
  345. off = half + index,
  346. right;
  347. while (off && data[off][E_BINTREE_INPUT_VALUE] == data[off - 1][E_BINTREE_INPUT_VALUE]) off--;
  348. right = Bintree_SortHalf(output, data, index, off, offset + 1, offset);
  349. output[offset][E_BINTREE_TREE_VALUE] = data[off][E_BINTREE_INPUT_VALUE];
  350. output[offset][E_BINTREE_TREE_POINTER] = data[off][E_BINTREE_INPUT_POINTER];
  351. output[offset][E_BINTREE_TREE_PARENT] = parent;
  352. output[offset][E_BINTREE_TREE_LEFT] = offset + 1;
  353. output[offset][E_BINTREE_TREE_RIGHT] = right;
  354. return Bintree_SortHalf(output, data, off + 1, upper, right, offset);
  355. }
  356. return offset + 1;
  357. }
  358. /**--------------------------------------------------------------------------**\
  359. <summary>Bintree_Add</summary>
  360. <param name="BinaryTree:data<>">Array to add to.</param>
  361. <param name="pointer">Pointer to add.</param>
  362. <param name="value">Value to add.</param>
  363. <param name="offset">Location in the array to store the data.</param>
  364. <param name="maxsize">Size of data.</param>
  365. <returns>
  366. Next free location
  367. </returns>
  368. <remarks>
  369. -
  370. native Bintree_Add(BinaryTree:tree<>, pointer, value, offset, maxsize = sizeof (data));
  371. </remarks>
  372. \**--------------------------------------------------------------------------**/
  373. stock Bintree_Add(BinaryTree:data<>, pointer, value, offset, maxsize = sizeof (data))
  374. {
  375. P:3("Bintree_Add called: %s, %i, %i, %i, %i", Bintree_DisplayOutput(data), pointer, value, offset, maxsize);
  376. if (offset >= maxsize) return BINTREE_NOT_FOUND;
  377. if (offset)
  378. {
  379. new
  380. leaf,
  381. old;
  382. while (Bintree_FindValue(data, value, leaf, old) != BINTREE_NOT_FOUND) continue;
  383. //Bintree_Reset(data, offset);
  384. if (value < data[old][E_BINTREE_TREE_VALUE]) data[old][E_BINTREE_TREE_LEFT] = offset;
  385. else data[old][E_BINTREE_TREE_RIGHT] = offset;
  386. data[offset][E_BINTREE_TREE_PARENT] = old;
  387. data[offset][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  388. data[offset][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  389. data[offset][E_BINTREE_TREE_VALUE] = value;
  390. data[offset][E_BINTREE_TREE_POINTER] = pointer;
  391. return offset + 1;
  392. }
  393. else
  394. {
  395. data[0][E_BINTREE_TREE_PARENT] = BINTREE_NO_BRANCH;
  396. data[0][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  397. data[0][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  398. data[0][E_BINTREE_TREE_VALUE] = value;
  399. data[0][E_BINTREE_TREE_POINTER] = pointer;
  400. return 1;
  401. }
  402. }
  403. /**--------------------------------------------------------------------------**\
  404. <summary>Bintree_Delete</summary>
  405. <param name="BinaryTree:tree<>">Data.</param>
  406. <param name="index">Index to remove.</param>
  407. <param name="count">Number of binary tree items.</param>
  408. <returns>
  409. -
  410. </returns>
  411. <remarks>
  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. </remarks>
  418. \**--------------------------------------------------------------------------**/
  419. stock Bintree_Delete(BinaryTree:source<>, index, count)
  420. {
  421. P:3("Bintree_Delete called: %s, %i, %i", Bintree_DisplayOutput(source), index, count);
  422. new
  423. branch,
  424. old = index;
  425. while (TRUE)
  426. {
  427. if ((branch = source[old][E_BINTREE_TREE_LEFT]) != BINTREE_NO_BRANCH) branch = Bintree_FindMax(source, branch);
  428. else if ((branch = source[old][E_BINTREE_TREE_RIGHT]) != BINTREE_NO_BRANCH) branch = Bintree_FindMin(source, branch);
  429. else
  430. {
  431. if ((branch = source[old][E_BINTREE_TREE_PARENT]) != BINTREE_NO_BRANCH)
  432. {
  433. if (source[branch][E_BINTREE_TREE_LEFT] == old) source[branch][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  434. else source[branch][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  435. }
  436. return Bintree_Compress(source, old, count);
  437. }
  438. new
  439. value = source[old][E_BINTREE_TREE_VALUE],
  440. pointer = source[old][E_BINTREE_TREE_POINTER];
  441. source[old][E_BINTREE_TREE_VALUE] = source[branch][E_BINTREE_TREE_VALUE];
  442. source[old][E_BINTREE_TREE_POINTER] = source[branch][E_BINTREE_TREE_POINTER];
  443. source[branch][E_BINTREE_TREE_VALUE] = value;
  444. source[branch][E_BINTREE_TREE_POINTER] = pointer;
  445. old = branch;
  446. }
  447. return BINTREE_NO_BRANCH;
  448. }
  449. /**--------------------------------------------------------------------------**\
  450. <summary>Bintree_Compress</summary>
  451. <param name="BinaryTree:tree<>">Array to compress.</param>
  452. <param name="index">Point to start at.</param>
  453. <param name="count">Number of items total.</param>
  454. <returns>
  455. -
  456. </returns>
  457. <remarks>
  458. -
  459. </remarks>
  460. \**--------------------------------------------------------------------------**/
  461. static stock Bintree_Compress(BinaryTree:data<>, index, count)
  462. {
  463. P:4("Bintree_Compress called: %s, %i, %i", Bintree_DisplayOutput(data), index, count);
  464. new
  465. index2 = index + 1;
  466. while (index < count)
  467. {
  468. new
  469. left = (data[index][E_BINTREE_TREE_LEFT] = data[index2][E_BINTREE_TREE_LEFT]),
  470. right = (data[index][E_BINTREE_TREE_RIGHT] = data[index2][E_BINTREE_TREE_RIGHT]),
  471. parent = (data[index][E_BINTREE_TREE_PARENT] = data[index2][E_BINTREE_TREE_PARENT]);
  472. data[index][E_BINTREE_TREE_VALUE] = data[index2][E_BINTREE_TREE_VALUE];
  473. data[index][E_BINTREE_TREE_POINTER] = data[index2][E_BINTREE_TREE_POINTER];
  474. if (left != BINTREE_NO_BRANCH) data[left][E_BINTREE_TREE_PARENT] = index;
  475. if (right != BINTREE_NO_BRANCH) data[right][E_BINTREE_TREE_PARENT] = index;
  476. if (parent != BINTREE_NO_BRANCH)
  477. {
  478. if (data[parent][E_BINTREE_TREE_LEFT] == index2) data[parent][E_BINTREE_TREE_LEFT] = index;
  479. else if (data[parent][E_BINTREE_TREE_RIGHT] == index2) data[parent][E_BINTREE_TREE_RIGHT] = index;
  480. }
  481. index++;
  482. index2++;
  483. }
  484. return count - 1;
  485. }
  486. /**--------------------------------------------------------------------------**\
  487. <summary>Bintree_FindMin</summary>
  488. <param name="BinaryTree:data<>">Array to search.</param>
  489. <param name="offset">Start of branch to search.</param>
  490. <returns>
  491. -
  492. </returns>
  493. <remarks>
  494. Finds the smallest value on a branch
  495. </remarks>
  496. \**--------------------------------------------------------------------------**/
  497. static stock Bintree_FindMin(BinaryTree:data<>, offset)
  498. {
  499. P:4("Bintree_FindMin called: %s, %i", Bintree_DisplayOutput(data), offset);
  500. new
  501. branch;
  502. while ((branch = data[offset][E_BINTREE_TREE_LEFT]) != BINTREE_NO_BRANCH) offset = branch;
  503. return offset;
  504. }
  505. /**--------------------------------------------------------------------------**\
  506. <summary>Bintree_FindMax</summary>
  507. <param name="BinaryTree:data<>">Array to search.</param>
  508. <param name="offset">Start of branch to search.</param>
  509. <returns>
  510. -
  511. </returns>
  512. <remarks>
  513. Finds the largest value on a branch
  514. </remarks>
  515. \**--------------------------------------------------------------------------**/
  516. static stock Bintree_FindMax(BinaryTree:data<>, offset)
  517. {
  518. P:4("Bintree_FindMax called: %s, %i", Bintree_DisplayOutput(data), offset);
  519. new
  520. branch;
  521. while ((branch = data[offset][E_BINTREE_TREE_RIGHT]) != BINTREE_NO_BRANCH) offset = branch;
  522. return offset;
  523. }
  524. /**--------------------------------------------------------------------------**\
  525. <summary>Bintree_UpdatePointers</summary>
  526. <param name="BinaryTree:data<>">Data to modify.</param>
  527. <param name="offset">Pointer to modify values after.</param>
  528. <param name="mod">Value to modify by.</param>
  529. <returns>
  530. -
  531. </returns>
  532. <remarks>
  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. </remarks>
  536. \**--------------------------------------------------------------------------**/
  537. stock Bintree_UpdatePointers(BinaryTree:data<>, offset, size, mod = -1)
  538. {
  539. P:3("Bintree_UpdatePointers called: %s, %i, %i, %i", Bintree_DisplayOutput(data), offset, size, mod);
  540. for (new i = 0; i < size; i++)
  541. {
  542. if (data[i][E_BINTREE_TREE_POINTER] > offset) data[i][E_BINTREE_TREE_POINTER] += mod;
  543. }
  544. }