Advertisement
Guest User

y_bintree

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