avltree.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. #include "avltree.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. // #define AVL_DEBUG
  5. #ifdef AVL_DEBUG
  6. #define avl_debug(...) printf(__VA_ARGS__)
  7. #else
  8. #define avl_debug(...)
  9. #endif
  10. static void __avl_rotate_right(struct avl_node *node, struct avl_root *root);
  11. static void __avl_rotate_left(struct avl_node *node, struct avl_root *root);
  12. static int __avl_balance_right(struct avl_node *node, struct avl_root *root);
  13. static int __avl_balance_left(struct avl_node *node, struct avl_root *root);
  14. static int __left_hand_insert_track_back(struct avl_node *node, struct avl_root *root);
  15. static int __right_hand_insert_track_back(struct avl_node *node, struct avl_root *root);
  16. #define __left_hand_delete_track_back(node, root) __right_hand_insert_track_back(node, root)
  17. #define __right_hand_delete_track_back(node, root) __left_hand_insert_track_back(node, root)
  18. /**
  19. * @brief __avl_rotate_right
  20. * 对 node 节点进行单右旋处理
  21. * @param node 二叉树节点
  22. * @param root 二叉树根
  23. * @return
  24. */
  25. static void __avl_rotate_right(struct avl_node *node, struct avl_root *root)
  26. {
  27. // 对以*node为根的二叉树作右旋转处理,处理之后node指向新的树根结点
  28. // 即旋转处理之前的左子树的根结点
  29. struct avl_node *left = node->avl_left;
  30. struct avl_node *parent = avl_parent(node);
  31. if ((node->avl_left = left->avl_right))
  32. avl_set_parent(left->avl_right, node);
  33. left->avl_right = node;
  34. avl_set_parent(left, parent);
  35. if (parent)
  36. {
  37. if (node == parent->avl_right)
  38. parent->avl_right = left;
  39. else
  40. parent->avl_left = left;
  41. }
  42. else
  43. root->avl_node = left;
  44. avl_set_parent(node, left);
  45. }
  46. /**
  47. * @brief __avl_rotate_left
  48. * 对 node 节点进行单左旋处理
  49. * @param node 二叉树节点
  50. * @param root 二叉树根
  51. * @return
  52. */
  53. static void __avl_rotate_left(struct avl_node *node, struct avl_root *root)
  54. {
  55. struct avl_node *right = node->avl_right;
  56. struct avl_node *parent = avl_parent(node);
  57. if ((node->avl_right = right->avl_left))
  58. avl_set_parent(right->avl_left, node);
  59. right->avl_left = node;
  60. avl_set_parent(right, parent);
  61. if (parent)
  62. {
  63. if (node == parent->avl_left)
  64. parent->avl_left = right;
  65. else
  66. parent->avl_right = right;
  67. }
  68. else
  69. root->avl_node = right;
  70. avl_set_parent(node, right);
  71. }
  72. /**
  73. * @brief __avl_balance_left
  74. * 当 node 节点左子树高于右子树, 对 node 节点进行左平衡处理并更新平衡因子
  75. * @param node 二叉树节点
  76. * @param root 二叉树根
  77. * @return 对于原 node 所在位置,经过平衡处理使树的高度降低了返回 -1,否则返回0
  78. */
  79. static int __avl_balance_left(struct avl_node *node, struct avl_root *root)
  80. {
  81. int retl = 0;
  82. int left_right_child_scale;
  83. struct avl_node *left_child = node->avl_left;
  84. struct avl_node *left_left_child;
  85. struct avl_node *left_right_child;
  86. if (left_child)
  87. {
  88. left_left_child = left_child->avl_left;
  89. left_right_child = left_child->avl_right;
  90. switch (avl_scale(left_child))
  91. {
  92. case AVL_BALANCED: // 只有在删除的时候会出现这种情况,情况非常复杂,需小心处理
  93. left_right_child_scale = avl_scale(left_right_child);
  94. __avl_rotate_left(node->avl_left, root); // 对*node的左子树作左平衡处理
  95. __avl_rotate_right(node, root); // 对*node作右平衡处理
  96. avl_set_tilt_left(left_child);
  97. avl_set_tilt_left(left_right_child);
  98. if (left_right_child_scale == AVL_BALANCED)
  99. avl_set_balanced(node);
  100. else if (left_right_child_scale == AVL_TILT_LEFT)
  101. avl_set_tilt_right(node);
  102. else
  103. {
  104. int left_left_child_scale = avl_scale(left_left_child);
  105. avl_set_balanced(node);
  106. retl = __avl_balance_left(left_child, root); // 这种情况需递归,并需要根据递归结果更新平衡因子
  107. if ((retl < 0) || (left_left_child_scale != AVL_BALANCED))
  108. avl_set_balanced(left_right_child);
  109. }
  110. avl_debug("L_R\r\n");
  111. break;
  112. case AVL_TILT_LEFT:
  113. __avl_rotate_right(node, root);
  114. avl_set_balanced(node);
  115. avl_set_balanced(left_child);
  116. retl = -1; // 高度变低了
  117. avl_debug("R\r\n");
  118. break;
  119. case AVL_TILT_RIGHT:
  120. __avl_rotate_left(node->avl_left, root); // 对*node的左子树作左平衡处理
  121. __avl_rotate_right(node, root); // 对*node作右平衡处理
  122. switch (avl_scale(left_right_child))
  123. {
  124. case AVL_BALANCED:
  125. avl_set_balanced(node);
  126. avl_set_balanced(left_child);
  127. break;
  128. case AVL_TILT_RIGHT:
  129. avl_set_balanced(node);
  130. avl_set_tilt_left(left_child);
  131. break;
  132. case AVL_TILT_LEFT:
  133. avl_set_balanced(left_child);
  134. avl_set_tilt_right(node);
  135. break;
  136. }
  137. avl_set_balanced(left_right_child);
  138. retl = -1; // 高度变低
  139. avl_debug("L_R\r\n");
  140. break;
  141. }
  142. }
  143. return retl;
  144. }
  145. /**
  146. * @brief __avl_balance_right
  147. * 当 node 节点右子树高于左子树, 对 node 节点进行左平衡处理并更新平衡因子
  148. * @param node 二叉树节点
  149. * @param root 二叉树根
  150. * @return 对于原 node 所在树的位置,经过平衡处理使树的高度降低了返回 -1,否则返回0
  151. */
  152. static int __avl_balance_right(struct avl_node *node, struct avl_root *root)
  153. {
  154. int ret = 0;
  155. int right_left_child_scale;
  156. struct avl_node *right_child = node->avl_right;
  157. struct avl_node *right_left_child = right_child->avl_left;
  158. struct avl_node *right_right_child = right_child->avl_right;
  159. if (right_child)
  160. {
  161. switch (avl_scale(right_child))
  162. {
  163. case AVL_BALANCED: // 删除的时候会出现这种情况,需要特别注意
  164. right_left_child_scale = avl_scale(right_left_child);
  165. __avl_rotate_right(node->avl_right, root);
  166. __avl_rotate_left(node, root);
  167. avl_set_tilt_right(right_left_child);
  168. avl_set_tilt_right(right_child);
  169. if (right_left_child_scale == AVL_BALANCED)
  170. avl_set_balanced(node);
  171. else if (right_left_child_scale == AVL_TILT_RIGHT)
  172. avl_set_tilt_left(node);
  173. else
  174. {
  175. int right_right_child_scale = avl_scale(right_right_child);
  176. avl_set_balanced(node);
  177. ret = __avl_balance_right(right_child, root); // 需递归一次
  178. if ((ret < 0) || (right_right_child_scale != AVL_BALANCED))
  179. avl_set_balanced(right_left_child);
  180. }
  181. avl_debug("R_L\r\n");
  182. break;
  183. case AVL_TILT_RIGHT:
  184. avl_debug("L\r\n");
  185. __avl_rotate_left(node, root);
  186. avl_set_balanced(node);
  187. avl_set_balanced(right_child);
  188. ret = -1; // 高度变低了//
  189. break;
  190. case AVL_TILT_LEFT:
  191. __avl_rotate_right(node->avl_right, root);
  192. __avl_rotate_left(node, root);
  193. avl_debug("R_L\r\n");
  194. switch (avl_scale(right_left_child)) // 旋转后要更新平衡因子
  195. {
  196. case AVL_TILT_LEFT:
  197. avl_set_tilt_right(right_child);
  198. avl_set_balanced(node);
  199. break;
  200. case AVL_BALANCED:
  201. avl_set_balanced(node);
  202. avl_set_balanced(right_child);
  203. break;
  204. case AVL_TILT_RIGHT:
  205. avl_set_balanced(right_child);
  206. avl_set_tilt_left(node);
  207. break;
  208. }
  209. avl_set_balanced(right_left_child);
  210. ret = -1; //
  211. break;
  212. }
  213. }
  214. return ret;
  215. }
  216. /**
  217. * @brief __left_hand_insert_track_back
  218. * 在 node 节点的左子树进行了插入,更新平衡因子,如失衡则作平衡处理
  219. * @param node 二叉树节点
  220. * @param root 二叉树根
  221. * @return 对于原 node 所在树的位置,经过平衡处理使树的高度降低了返回 -1,否则返回0
  222. */
  223. static int __left_hand_insert_track_back(struct avl_node *node, struct avl_root *root)
  224. {
  225. switch (avl_scale(node))
  226. {
  227. case AVL_BALANCED:
  228. avl_set_tilt_left(node);
  229. return 0;
  230. case AVL_TILT_RIGHT:
  231. avl_set_balanced(node);
  232. return -1;
  233. case AVL_TILT_LEFT:
  234. return __avl_balance_left(node, root);
  235. }
  236. return 0;
  237. }
  238. /**
  239. * @brief __left_hand_insert_track_back
  240. * 在 node 节点的右子树进行了插入,更新平衡因子,如失衡则作平衡处理
  241. * @param node 二叉树节点
  242. * @param root 二叉树根
  243. * @return 对于原 node 所在树的位置,插入并平衡后高度降低了返回 -1,否则返回0
  244. */
  245. static int __right_hand_insert_track_back(struct avl_node *node, struct avl_root *root)
  246. {
  247. switch (avl_scale(node))
  248. {
  249. case AVL_BALANCED:
  250. avl_set_tilt_right(node); // 父节点右倾
  251. return 0; // 以 node 为根的树高度被改变,但未失衡
  252. case AVL_TILT_LEFT:
  253. avl_set_balanced(node); // 父节点平衡
  254. return -1; // 以 node 为根的树高度不改变
  255. case AVL_TILT_RIGHT: // 以 node 为根的树已失衡,作平衡处理
  256. return __avl_balance_right(node, root); //
  257. }
  258. return 0;
  259. }
  260. void avl_insert(
  261. struct avl_root *root,
  262. struct avl_node *insertnode,
  263. struct avl_node *parent,
  264. struct avl_node **avl_link)
  265. {
  266. int taller = 1;
  267. struct avl_node *gparent = NULL;
  268. uint8_t parent_gparent_path = 0;
  269. uint8_t backtrack_path = 0;
  270. insertnode->avl_parent = (unsigned long)parent;
  271. insertnode->avl_left = insertnode->avl_right = NULL;
  272. *avl_link = insertnode;
  273. if (root->avl_node == insertnode)
  274. return;
  275. if (AVL_BALANCED != avl_scale(parent))
  276. {
  277. avl_set_balanced(parent); // 父节点平衡
  278. return; // 树没长高返回
  279. }
  280. backtrack_path = (insertnode == parent->avl_left) ? AVL_TILT_LEFT : AVL_TILT_RIGHT;
  281. // 树长高了,需要回溯平衡
  282. while (taller && parent)
  283. {
  284. // 回溯平衡过程会改变树的结构,先记录祖父节点和对应的回溯路径方向
  285. if ((gparent = avl_parent(parent))) // 先赋值再判断
  286. parent_gparent_path = (parent == gparent->avl_right) ? AVL_TILT_RIGHT : AVL_TILT_LEFT;
  287. if (backtrack_path == AVL_TILT_RIGHT)
  288. taller += __right_hand_insert_track_back(parent, root);
  289. else
  290. taller += __left_hand_insert_track_back(parent, root);
  291. backtrack_path = parent_gparent_path; // 回溯
  292. parent = gparent;
  293. }
  294. }
  295. #if 1
  296. void avl_delete(struct avl_root *root, struct avl_node *node)
  297. {
  298. struct avl_node *child, *parent;
  299. struct avl_node *gparent = NULL;
  300. int lower = 1;
  301. uint8_t parent_gparent_path = 0;
  302. uint8_t backtrack_path = 0;
  303. if (!node->avl_left) // 如果被删节点不存在左子树
  304. {
  305. child = node->avl_right; // 把右子树接入父节点中
  306. parent = avl_parent(node);
  307. if (child)
  308. avl_set_parent(child, parent);
  309. if (parent)
  310. {
  311. if (parent->avl_left == node)
  312. {
  313. parent->avl_left = child;
  314. backtrack_path = AVL_TILT_LEFT;
  315. }
  316. else
  317. {
  318. parent->avl_right = child;
  319. backtrack_path = AVL_TILT_RIGHT;
  320. }
  321. }
  322. else
  323. {
  324. root->avl_node = child;
  325. if (child)
  326. avl_set_balanced(child);
  327. return;
  328. }
  329. }
  330. else if (!node->avl_right) // 如果被删节点存在左子树,不存在右子树
  331. {
  332. child = node->avl_left;
  333. parent = avl_parent(node);
  334. avl_set_parent(child, parent);
  335. if (parent)
  336. {
  337. if (parent->avl_left == node)
  338. {
  339. parent->avl_left = child;
  340. backtrack_path = AVL_TILT_LEFT;
  341. }
  342. else
  343. {
  344. parent->avl_right = child;
  345. backtrack_path = AVL_TILT_RIGHT;
  346. }
  347. }
  348. else
  349. {
  350. root->avl_node = child;
  351. if (child)
  352. avl_set_balanced(child);
  353. return;
  354. }
  355. }
  356. else // 被删节点即存在左子树,也存在右子树
  357. {
  358. struct avl_node *old = node, *left;
  359. node = node->avl_right;
  360. while ((left = node->avl_left) != NULL)
  361. node = left; // 找到右子树下最小的替代点
  362. if (avl_parent(old)) // 旧点所在父节点
  363. {
  364. if (avl_parent(old)->avl_left == old)
  365. avl_parent(old)->avl_left = node;
  366. else
  367. avl_parent(old)->avl_right = node;
  368. }
  369. else
  370. root->avl_node = node;
  371. child = node->avl_right; // 最小的替代点的右节点
  372. parent = avl_parent(node); // 最小的替代点的父节点
  373. if (parent == old) // 要删除的节点的右子树没有左子树,替代点(1)没有右节点,(2)存在一个右节点
  374. {
  375. backtrack_path = AVL_TILT_RIGHT;
  376. parent = node;
  377. node->avl_parent = old->avl_parent;
  378. node->avl_left = old->avl_left;
  379. avl_set_parent(old->avl_left, node);
  380. }
  381. else // 要删除的节点的右子树有左子树
  382. {
  383. backtrack_path = AVL_TILT_LEFT;
  384. parent->avl_left = child; // 最小的替代点的右节点的父节点
  385. node->avl_right = old->avl_right;
  386. avl_set_parent(old->avl_right, node);
  387. node->avl_parent = old->avl_parent;
  388. node->avl_left = old->avl_left;
  389. avl_set_parent(old->avl_left, node);
  390. if (child)
  391. avl_set_parent(child, parent); // 要删除的节点的右子树的左子树末尾点存在右节点
  392. }
  393. }
  394. // 树低了,需要回溯平衡,直到回溯到根节点
  395. while (lower && parent)
  396. {
  397. if ((gparent = avl_parent(parent))) //(parent && (gparent = avl_parent(parent)))//先赋值再判断
  398. parent_gparent_path = (parent == gparent->avl_right) ? AVL_TILT_RIGHT : AVL_TILT_LEFT;
  399. if (backtrack_path == AVL_TILT_RIGHT) // 经过回溯调整会改变树的结构,所以先记录 gparent 和回溯路径
  400. lower = __right_hand_delete_track_back(parent, root);
  401. else
  402. lower = __left_hand_delete_track_back(parent, root);
  403. backtrack_path = parent_gparent_path;
  404. parent = gparent;
  405. }
  406. }
  407. #endif
  408. /*
  409. * This function returns the first node (in sort oright_left_childer) of the tree.
  410. */
  411. struct avl_node *avl_first(const struct avl_root *root)
  412. {
  413. struct avl_node *n;
  414. n = root->avl_node;
  415. if (!n)
  416. return NULL;
  417. while (n->avl_left)
  418. n = n->avl_left;
  419. return n;
  420. }
  421. struct avl_node *avl_last(const struct avl_root *root)
  422. {
  423. struct avl_node *n;
  424. n = root->avl_node;
  425. if (!n)
  426. return NULL;
  427. while (n->avl_right)
  428. n = n->avl_right;
  429. return n;
  430. }
  431. struct avl_node *avl_next(const struct avl_node *node)
  432. {
  433. struct avl_node *parent;
  434. if (avl_parent(node) == node)
  435. return NULL;
  436. /* If we have a right-hand child, go down and then left as far
  437. as we can. */
  438. if (node->avl_right)
  439. {
  440. node = node->avl_right;
  441. while (node->avl_left)
  442. node = node->avl_left;
  443. return (struct avl_node *)node;
  444. }
  445. /* No right-hand children. Everything down and left is
  446. smaller than us, so any 'next' node must be in the general
  447. direction of our parent. Go up the tree; any time the
  448. ancestor is a right-hand child of its parent, keep going
  449. up. First time it's a left-hand child of its parent, said
  450. parent is our 'next' node. */
  451. while ((parent = avl_parent(node)) && node == parent->avl_right)
  452. node = parent;
  453. return parent;
  454. }
  455. struct avl_node *avl_prev(const struct avl_node *node)
  456. {
  457. struct avl_node *parent;
  458. if (avl_parent(node) == node)
  459. return NULL;
  460. /* If we have a left-hand child, go down and then right as far
  461. as we can. */
  462. if (node->avl_left)
  463. {
  464. node = node->avl_left;
  465. while (node->avl_right)
  466. node = node->avl_right;
  467. return (struct avl_node *)node;
  468. }
  469. /* No left-hand children. Go up till we find an ancestor which
  470. is a right-hand child of its parent */
  471. while ((parent = avl_parent(node)) && node == parent->avl_left)
  472. node = parent;
  473. return parent;
  474. }
  475. void avl_replace_node(struct avl_node *victim, struct avl_node *new,
  476. struct avl_root *root)
  477. {
  478. struct avl_node *parent = avl_parent(victim);
  479. /* Set the surrounding nodes to point to the replacement */
  480. if (parent)
  481. {
  482. if (victim == parent->avl_left)
  483. parent->avl_left = new;
  484. else
  485. parent->avl_right = new;
  486. }
  487. else
  488. {
  489. root->avl_node = new;
  490. }
  491. if (victim->avl_left)
  492. avl_set_parent(victim->avl_left, new);
  493. if (victim->avl_right)
  494. avl_set_parent(victim->avl_right, new);
  495. /* Copy the pointers/colour from the victim to the replacement */
  496. *new = *victim;
  497. }