avl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. .TH AVL 2
  2. .SH NAME
  3. mkavltree, insertavl, lookupavl, deleteavl, avlwalk, avlnext, avlprev, endwalk - AVL tree routines
  4. .SH SYNOPSIS
  5. .EX
  6. #include <u.h>
  7. #include <libc.h>
  8. #include <bio.h>
  9. #include <avl.h>
  10. typedef struct Avl Avl;
  11. struct Avl
  12. {
  13. Avl *p; /* parent */
  14. Avl *n[2]; /* children */
  15. int bal; /* balance bits */
  16. };
  17. Avl *avlnext(Avlwalk *walk);
  18. Avl *avlprev(Avlwalk *walk);
  19. Avlwalk *avlwalk(Avltree *tree);
  20. void deleteavl(Avltree *tree, Avl *key, Avl **oldp);
  21. void endwalk(Avlwalk *walk);
  22. void insertavl(Avltree *tree, Avl *new, Avl **oldp);
  23. Avl *lookupavl(Avltree *tree, Avl *key);
  24. Avltree *mkavltree(int(*cmp)(Avl*, Avl*));
  25. .EE
  26. .SH DESCRIPTION
  27. An AVL tree is a self-balancing binary search tree.
  28. These routines allow creation and maintenance of in-memory AVL trees.
  29. .PP
  30. An empty AVL tree is created by calling
  31. .I mkavltree
  32. with a comparison function as argument.
  33. This function should take two pointers to
  34. .B Avl
  35. objects and return -1, 0 or 1 as the first is
  36. respectively less than,
  37. equal to, or greater than,
  38. the second.
  39. .I Insertavl
  40. adds a
  41. .I new
  42. tree node into
  43. .IR tree .
  44. If
  45. .I oldp
  46. is non-nil upon return,
  47. it points to storage for an old node
  48. with the same key that may now be freed.
  49. .I Lookupavl
  50. returns the
  51. .I tree
  52. node with
  53. .IR key .
  54. .I Deleteavl
  55. removes the node with
  56. .I key
  57. from
  58. .IR tree .
  59. .I oldp
  60. is handled as per
  61. .IR insertavl .
  62. .PP
  63. .I Avlwalk
  64. returns a pointer to a newly-allocated
  65. .B Avlwalk
  66. object.
  67. .I Endwalk
  68. frees such an object.
  69. .I Avlnext
  70. and
  71. .I avlprev
  72. walk the tree associated with
  73. .IR walk ,
  74. returning the next
  75. (respectively, previous)
  76. tree node in the comparison order
  77. defined by the comparison function
  78. associated with the tree associated with
  79. .IR walk .
  80. .SH SOURCE
  81. .B /sys/src/libavl
  82. .SH SEE ALSO
  83. G. M. Adelson-Velsky,
  84. E. M. Landis,
  85. ``An algorithm for the organization of information'',
  86. Soviet Mathematics, Vol. 3, pp. 1256—1263.
  87. .SH DIAGNOSTICS
  88. Functions returning pointers return
  89. .B nil
  90. on error.