y_bintree.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. #if defined _INC_y_bintree
  105. #endinput
  106. #endif
  107. #define _INC_y_bintree
  108. #include "..\YSI_Internal\y_version"
  109. #include "..\YSI_Core\y_debug"
  110. #include "..\YSI_Core\y_utils"
  111. #define BINTREE_NO_BRANCH -1
  112. #define BINTREE_NOT_FOUND -1
  113. // If this ever changes, update the size reference in y_users.
  114. enum E_BINTREE_TREE
  115. {
  116. E_BINTREE_TREE_VALUE,
  117. E_BINTREE_TREE_LEFT,
  118. E_BINTREE_TREE_RIGHT,
  119. E_BINTREE_TREE_PARENT,
  120. E_BINTREE_TREE_POINTER
  121. }
  122. enum E_BINTREE_INPUT
  123. {
  124. E_BINTREE_INPUT_VALUE,
  125. E_BINTREE_INPUT_POINTER
  126. }
  127. //#define leafs<%1> %1][E_BINTREE_TREE
  128. //#define Bintree:%1[%2] Bintree:%1[%2][E_BINTREE_TREE]
  129. #define BinaryTree:%1<%2> Bintree:%1[%2][E_BINTREE_TREE]
  130. #define BinaryInput:%1<%2> %1[%2][E_BINTREE_INPUT]
  131. #if defined YSI_TESTS
  132. #include "..\YSI_Core\y_testing"
  133. #include "y_bintree/tests"
  134. #endif
  135. // Update at a later date...
  136. #define Bintree_DisplayOutput(%0) "<bintree output>"
  137. #define Bintree_DisplayInput(%0) "<bintree input>"
  138. /**--------------------------------------------------------------------------**\
  139. <summary>Bintree_Sort</summary>
  140. <param name="input[][E_BINTREE_INPUT]">Data to sort.</param>
  141. <param name="size">Size of data to sort.</param>
  142. <returns>
  143. -
  144. </returns>
  145. <remarks>
  146. Entry point for Bintree_QSort.
  147. </remarks>
  148. \**--------------------------------------------------------------------------**/
  149. #define Bintree_Sort(%1,%2) \
  150. Bintree_QSort((%1), 0, (%2) - 1)
  151. /**--------------------------------------------------------------------------**\
  152. <summary>Bintree_Fill</summary>
  153. <param name="BinaryTree:output<>">Destination for balanced tree.</param>
  154. <param name="data[][E_BINTREE_INPUT]">Source data.</param>
  155. <param name="size">Size of data.</param>
  156. <returns>
  157. Bintree_SortHalf.
  158. </returns>
  159. <remarks>
  160. Entry point for Bintree_SortHalf.
  161. </remarks>
  162. \**--------------------------------------------------------------------------**/
  163. #define Bintree_Fill(%1,%2,%3) \
  164. Bintree_SortHalf((%1), (%2), 0, (%3), 0, BINTREE_NO_BRANCH)
  165. /**--------------------------------------------------------------------------**\
  166. <summary>Bintree_Generate</summary>
  167. <param name="BinaryTree:output<>">Binary tree to store the data in.</param>
  168. <param name="input[][E_BINTREE_INPUT]">Input data to get the data from.</param>
  169. <param name="size">Number of items to sort.</param>
  170. <returns>
  171. -
  172. </returns>
  173. <remarks>
  174. Just calls the sort and fill routines.
  175. </remarks>
  176. \**--------------------------------------------------------------------------**/
  177. stock Bintree_Generate(BinaryTree:output<>, input[][E_BINTREE_INPUT], size = sizeof (input))
  178. {
  179. P:3("Bintree_Generate called: %s, %s, %i", Bintree_DisplayOutput(output), Bintree_DisplayInput(input), size);
  180. if (!size)
  181. {
  182. output[0][E_BINTREE_TREE_PARENT] = BINTREE_NO_BRANCH;
  183. output[0][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  184. output[0][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  185. return 0;
  186. }
  187. Bintree_Sort(input, size);
  188. Bintree_Fill(output, input, size);
  189. return 1;
  190. }
  191. /**--------------------------------------------------------------------------**\
  192. <summary>Bintree_Reset</summary>
  193. <param name="BinaryTree:tree<>">Array to reset.</param>
  194. <param name="pointer">Position to reset.</param>
  195. <returns>
  196. -
  197. </returns>
  198. <remarks>
  199. Initialises the array for use.
  200. </remarks>
  201. \**--------------------------------------------------------------------------**/
  202. stock Bintree_Reset(BinaryTree:tree<>, pointer = 0)
  203. {
  204. P:3("Bintree_Reset called: %s, %i", Bintree_DisplayOutput(tree), pointer);
  205. tree[pointer][E_BINTREE_TREE_VALUE] = 0;
  206. tree[pointer][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  207. tree[pointer][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  208. tree[pointer][E_BINTREE_TREE_PARENT] = BINTREE_NO_BRANCH;
  209. tree[pointer][E_BINTREE_TREE_POINTER] = BINTREE_NOT_FOUND;
  210. }
  211. /**--------------------------------------------------------------------------**\
  212. <summary>Bintree_FindValue</summary>
  213. <param name="BinaryTree:tree<>">Tree to find the data in.</param>
  214. <param name="value">Value to search for.</param>
  215. <param name="&cont">Start point.</param>
  216. <param name="&old">The last real leaf.</param>
  217. <returns>
  218. -
  219. </returns>
  220. <remarks>
  221. Itterates through the array following the various paths till it locates
  222. the value provided or reaches a dead end. If the current value is greater
  223. than the search value, the search goes left, otherwise right.
  224. If cont is not -1 the search will start from the data pointed to by the
  225. data pointed to by conts' right path, this is to allow collisions to be
  226. passed over if you want a subsequent one.
  227. </remarks>
  228. \**--------------------------------------------------------------------------**/
  229. stock Bintree_FindValue(BinaryTree:tree<>, value, &cont = 0, &old = 0)
  230. {
  231. P:3("Bintree_FindValue called: %s, %i, %i, %i", Bintree_DisplayOutput(tree), value, cont, old);
  232. new
  233. treeValue;
  234. while (cont != BINTREE_NO_BRANCH)
  235. {
  236. P:7("Bintree_FindValue: search %d %d %d %d", cont, old, tree[cont][E_BINTREE_TREE_VALUE], value);
  237. old = cont;
  238. treeValue = tree[old][E_BINTREE_TREE_VALUE];
  239. if (value < treeValue) cont = tree[old][E_BINTREE_TREE_LEFT];
  240. else
  241. {
  242. cont = tree[old][E_BINTREE_TREE_RIGHT];
  243. if (value == treeValue)
  244. {
  245. return tree[old][E_BINTREE_TREE_POINTER];
  246. }
  247. }
  248. }
  249. return BINTREE_NOT_FOUND;
  250. }
  251. /**--------------------------------------------------------------------------**\
  252. <summary>Bintree_QSort</summary>
  253. <param name="numbers[][E_BINTREE_INPUT]">Data to sort.</param>
  254. <param name="left">Start index.</param>
  255. <param name="right">End index.</param>
  256. <returns>
  257. -
  258. </returns>
  259. <remarks>
  260. Custom version of QSort (see YSI_misc) allows for E_BINTREE_INPUT data
  261. types, preserving the relative pointers for the sorted data.
  262. </remarks>
  263. \**--------------------------------------------------------------------------**/
  264. stock Bintree_QSort(numbers[][E_BINTREE_INPUT], left, right)
  265. {
  266. P:3("Bintree_QSort called: %s, %i, %i", Bintree_DisplayInput(numbers), left, right);
  267. new
  268. pivot = numbers[left][E_BINTREE_INPUT_VALUE],
  269. pointer = numbers[left][E_BINTREE_INPUT_POINTER],
  270. l_hold = left,
  271. r_hold = right;
  272. while (left < right)
  273. {
  274. while ((numbers[right][E_BINTREE_INPUT_VALUE] >= pivot) && (left < right)) right--;
  275. if (left != right)
  276. {
  277. numbers[left][E_BINTREE_INPUT_VALUE] = numbers[right][E_BINTREE_INPUT_VALUE];
  278. numbers[left][E_BINTREE_INPUT_POINTER] = numbers[right][E_BINTREE_INPUT_POINTER];
  279. left++;
  280. }
  281. while ((numbers[left][E_BINTREE_INPUT_VALUE] <= pivot) && (left < right)) left++;
  282. if (left != right)
  283. {
  284. numbers[right][E_BINTREE_INPUT_VALUE] = numbers[left][E_BINTREE_INPUT_VALUE];
  285. numbers[right][E_BINTREE_INPUT_POINTER] = numbers[left][E_BINTREE_INPUT_POINTER];
  286. right--;
  287. }
  288. }
  289. numbers[left][E_BINTREE_INPUT_VALUE] = pivot;
  290. numbers[left][E_BINTREE_INPUT_POINTER] = pointer;
  291. pivot = left;
  292. left = l_hold;
  293. right = r_hold;
  294. if (left < pivot) Bintree_QSort(numbers, left, pivot - 1);
  295. if (right > pivot) Bintree_QSort(numbers, pivot + 1, right);
  296. }
  297. /**--------------------------------------------------------------------------**\
  298. <summary>Bintree_SortHalf</summary>
  299. <param name="BinaryTree:output<>">Destination array.</param>
  300. <param name="data[][E_BINTREE_INPUT]">Source array.</param>
  301. <param name="index">Start index of the source for processing.</param>
  302. <param name="upper">End index of the source for processing.</param>
  303. <param name="offset">Current offset in the destination array for writing.</param>
  304. <returns>
  305. Size of balanced tree.
  306. </returns>
  307. <remarks>
  308. Recursively calls itself. Bisects the passed array and passed each half
  309. back to itself, with the middle value of each half being the left and
  310. right branches of the middle value of the passed array (which isn't
  311. included in either bisected half). This is itterative so those are again
  312. split and again split. If the passed array is only one or two elements
  313. big the behaviour is set and hardcoded.
  314. Equal values SHOULD branch right, the code is designed for this however
  315. the generation is not fully tested (it mostly branches right but adjacent
  316. after bisecting values haven't been tested).
  317. Based on code written for PHP by me.
  318. </remarks>
  319. \**--------------------------------------------------------------------------**/
  320. stock Bintree_SortHalf(BinaryTree:output<>, data[][E_BINTREE_INPUT], index, upper, offset, parent)
  321. {
  322. P:3("Bintree_SortHalf called: %s, %s, %i, %i, %i, %i", Bintree_DisplayOutput(output), Bintree_DisplayInput(data), index, upper, offset, parent);
  323. new
  324. num = upper - index;
  325. if (!num) return offset;
  326. if (num == 1)
  327. {
  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] = BINTREE_NO_BRANCH;
  333. }
  334. else if (num == 2)
  335. {
  336. P:3("Bintree_SortHalf: adding %i %i %i", index, data[index][E_BINTREE_INPUT_VALUE], data[index + 1][E_BINTREE_INPUT_VALUE]);
  337. output[offset][E_BINTREE_TREE_VALUE] = data[index][E_BINTREE_INPUT_VALUE];
  338. output[offset][E_BINTREE_TREE_POINTER] = data[index][E_BINTREE_INPUT_POINTER];
  339. output[offset][E_BINTREE_TREE_PARENT] = parent;
  340. output[offset][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  341. output[offset][E_BINTREE_TREE_RIGHT] = offset + 1;
  342. ++offset;
  343. ++index;
  344. output[offset][E_BINTREE_TREE_VALUE] = data[index][E_BINTREE_INPUT_VALUE];
  345. output[offset][E_BINTREE_TREE_POINTER] = data[index][E_BINTREE_INPUT_POINTER];
  346. output[offset][E_BINTREE_TREE_PARENT] = offset - 1;
  347. output[offset][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  348. output[offset][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  349. }
  350. else
  351. {
  352. new
  353. half = num / 2,
  354. off = half + index,
  355. right;
  356. while (off && data[off][E_BINTREE_INPUT_VALUE] == data[off - 1][E_BINTREE_INPUT_VALUE]) off--;
  357. right = Bintree_SortHalf(output, data, index, off, offset + 1, offset);
  358. output[offset][E_BINTREE_TREE_VALUE] = data[off][E_BINTREE_INPUT_VALUE];
  359. output[offset][E_BINTREE_TREE_POINTER] = data[off][E_BINTREE_INPUT_POINTER];
  360. output[offset][E_BINTREE_TREE_PARENT] = parent;
  361. output[offset][E_BINTREE_TREE_LEFT] = offset + 1;
  362. output[offset][E_BINTREE_TREE_RIGHT] = right;
  363. return Bintree_SortHalf(output, data, off + 1, upper, right, offset);
  364. }
  365. return offset + 1;
  366. }
  367. /**--------------------------------------------------------------------------**\
  368. <summary>Bintree_Add</summary>
  369. <param name="BinaryTree:data<>">Array to add to.</param>
  370. <param name="pointer">Pointer to add.</param>
  371. <param name="value">Value to add.</param>
  372. <param name="offset">Location in the array to store the data.</param>
  373. <param name="maxsize">Size of data.</param>
  374. <returns>
  375. Next free location
  376. </returns>
  377. <remarks>
  378. -
  379. native Bintree_Add(BinaryTree:tree<>, pointer, value, offset, maxsize = sizeof (data));
  380. </remarks>
  381. \**--------------------------------------------------------------------------**/
  382. stock Bintree_Add(BinaryTree:data<>, pointer, value, offset, maxsize = sizeof (data))
  383. {
  384. P:3("Bintree_Add called: %s, %i, %i, %i, %i", Bintree_DisplayOutput(data), pointer, value, offset, maxsize);
  385. if (offset >= maxsize) return BINTREE_NOT_FOUND;
  386. if (offset)
  387. {
  388. new
  389. leaf,
  390. old;
  391. while (Bintree_FindValue(data, value, leaf, old) != BINTREE_NOT_FOUND) continue;
  392. //Bintree_Reset(data, offset);
  393. if (value < data[old][E_BINTREE_TREE_VALUE]) data[old][E_BINTREE_TREE_LEFT] = offset;
  394. else data[old][E_BINTREE_TREE_RIGHT] = offset;
  395. data[offset][E_BINTREE_TREE_PARENT] = old;
  396. data[offset][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  397. data[offset][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  398. data[offset][E_BINTREE_TREE_VALUE] = value;
  399. data[offset][E_BINTREE_TREE_POINTER] = pointer;
  400. return offset + 1;
  401. }
  402. else
  403. {
  404. data[0][E_BINTREE_TREE_PARENT] = BINTREE_NO_BRANCH;
  405. data[0][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  406. data[0][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  407. data[0][E_BINTREE_TREE_VALUE] = value;
  408. data[0][E_BINTREE_TREE_POINTER] = pointer;
  409. return 1;
  410. }
  411. }
  412. /**--------------------------------------------------------------------------**\
  413. <summary>Bintree_Delete</summary>
  414. <param name="BinaryTree:tree<>">Data.</param>
  415. <param name="index">Index to remove.</param>
  416. <param name="count">Number of binary tree items.</param>
  417. <returns>
  418. -
  419. </returns>
  420. <remarks>
  421. The left branch is usually larger due to the division
  422. method used so we start there. Even though right is
  423. >= and left is only < in even sized arrays the greater
  424. chunk (unless there's only 2 items) goes left.
  425. Called itteratively to ensure branches are maintained.
  426. </remarks>
  427. \**--------------------------------------------------------------------------**/
  428. stock Bintree_Delete(BinaryTree:source<>, index, count)
  429. {
  430. P:3("Bintree_Delete called: %s, %i, %i", Bintree_DisplayOutput(source), index, count);
  431. new
  432. branch,
  433. old = index;
  434. while (TRUE)
  435. {
  436. if ((branch = source[old][E_BINTREE_TREE_LEFT]) != BINTREE_NO_BRANCH) branch = Bintree_FindMax(source, branch);
  437. else if ((branch = source[old][E_BINTREE_TREE_RIGHT]) != BINTREE_NO_BRANCH) branch = Bintree_FindMin(source, branch);
  438. else
  439. {
  440. if ((branch = source[old][E_BINTREE_TREE_PARENT]) != BINTREE_NO_BRANCH)
  441. {
  442. if (source[branch][E_BINTREE_TREE_LEFT] == old) source[branch][E_BINTREE_TREE_LEFT] = BINTREE_NO_BRANCH;
  443. else source[branch][E_BINTREE_TREE_RIGHT] = BINTREE_NO_BRANCH;
  444. }
  445. return Bintree_Compress(source, old, count);
  446. }
  447. new
  448. value = source[old][E_BINTREE_TREE_VALUE],
  449. pointer = source[old][E_BINTREE_TREE_POINTER];
  450. source[old][E_BINTREE_TREE_VALUE] = source[branch][E_BINTREE_TREE_VALUE];
  451. source[old][E_BINTREE_TREE_POINTER] = source[branch][E_BINTREE_TREE_POINTER];
  452. source[branch][E_BINTREE_TREE_VALUE] = value;
  453. source[branch][E_BINTREE_TREE_POINTER] = pointer;
  454. old = branch;
  455. }
  456. return BINTREE_NO_BRANCH;
  457. }
  458. /**--------------------------------------------------------------------------**\
  459. <summary>Bintree_Compress</summary>
  460. <param name="BinaryTree:tree<>">Array to compress.</param>
  461. <param name="index">Point to start at.</param>
  462. <param name="count">Number of items total.</param>
  463. <returns>
  464. -
  465. </returns>
  466. <remarks>
  467. -
  468. </remarks>
  469. \**--------------------------------------------------------------------------**/
  470. static stock Bintree_Compress(BinaryTree:data<>, index, count)
  471. {
  472. P:4("Bintree_Compress called: %s, %i, %i", Bintree_DisplayOutput(data), index, count);
  473. new
  474. index2 = index + 1;
  475. while (index < count)
  476. {
  477. new
  478. left = (data[index][E_BINTREE_TREE_LEFT] = data[index2][E_BINTREE_TREE_LEFT]),
  479. right = (data[index][E_BINTREE_TREE_RIGHT] = data[index2][E_BINTREE_TREE_RIGHT]),
  480. parent = (data[index][E_BINTREE_TREE_PARENT] = data[index2][E_BINTREE_TREE_PARENT]);
  481. data[index][E_BINTREE_TREE_VALUE] = data[index2][E_BINTREE_TREE_VALUE];
  482. data[index][E_BINTREE_TREE_POINTER] = data[index2][E_BINTREE_TREE_POINTER];
  483. if (left != BINTREE_NO_BRANCH) data[left][E_BINTREE_TREE_PARENT] = index;
  484. if (right != BINTREE_NO_BRANCH) data[right][E_BINTREE_TREE_PARENT] = index;
  485. if (parent != BINTREE_NO_BRANCH)
  486. {
  487. if (data[parent][E_BINTREE_TREE_LEFT] == index2) data[parent][E_BINTREE_TREE_LEFT] = index;
  488. else if (data[parent][E_BINTREE_TREE_RIGHT] == index2) data[parent][E_BINTREE_TREE_RIGHT] = index;
  489. }
  490. index++;
  491. index2++;
  492. }
  493. return count - 1;
  494. }
  495. /**--------------------------------------------------------------------------**\
  496. <summary>Bintree_FindMin</summary>
  497. <param name="BinaryTree:data<>">Array to search.</param>
  498. <param name="offset">Start of branch to search.</param>
  499. <returns>
  500. -
  501. </returns>
  502. <remarks>
  503. Finds the smallest value on a branch
  504. </remarks>
  505. \**--------------------------------------------------------------------------**/
  506. static stock Bintree_FindMin(BinaryTree:data<>, offset)
  507. {
  508. P:4("Bintree_FindMin called: %s, %i", Bintree_DisplayOutput(data), offset);
  509. new
  510. branch;
  511. while ((branch = data[offset][E_BINTREE_TREE_LEFT]) != BINTREE_NO_BRANCH) offset = branch;
  512. return offset;
  513. }
  514. /**--------------------------------------------------------------------------**\
  515. <summary>Bintree_FindMax</summary>
  516. <param name="BinaryTree:data<>">Array to search.</param>
  517. <param name="offset">Start of branch to search.</param>
  518. <returns>
  519. -
  520. </returns>
  521. <remarks>
  522. Finds the largest value on a branch
  523. </remarks>
  524. \**--------------------------------------------------------------------------**/
  525. static stock Bintree_FindMax(BinaryTree:data<>, offset)
  526. {
  527. P:4("Bintree_FindMax called: %s, %i", Bintree_DisplayOutput(data), offset);
  528. new
  529. branch;
  530. while ((branch = data[offset][E_BINTREE_TREE_RIGHT]) != BINTREE_NO_BRANCH) offset = branch;
  531. return offset;
  532. }
  533. /**--------------------------------------------------------------------------**\
  534. <summary>Bintree_UpdatePointers</summary>
  535. <param name="BinaryTree:data<>">Data to modify.</param>
  536. <param name="offset">Pointer to modify values after.</param>
  537. <param name="mod">Value to modify by.</param>
  538. <returns>
  539. -
  540. </returns>
  541. <remarks>
  542. Used for updating pointers when the target data has been modifed (i.e. a
  543. value has been removed from the array and the array shifted).
  544. </remarks>
  545. \**--------------------------------------------------------------------------**/
  546. stock Bintree_UpdatePointers(BinaryTree:data<>, offset, size, mod = -1)
  547. {
  548. P:3("Bintree_UpdatePointers called: %s, %i, %i, %i", Bintree_DisplayOutput(data), offset, size, mod);
  549. for (new i = 0; i < size; i++)
  550. {
  551. if (data[i][E_BINTREE_TREE_POINTER] > offset) data[i][E_BINTREE_TREE_POINTER] += mod;
  552. }
  553. }