/uwm/branches/BRANCH-0.2/uwm/tree.c

# · C · 134 lines · 87 code · 17 blank · 30 comment · 16 complexity · e330a727e0ad794072158c4939b5f221 MD5 · raw file

  1. /*** TREES - c-Module for handling binary tree structures
  2. 1998 by Christian Ruppert ***/
  3. /* ########################################################################
  4. distributed with uwm - THE ude WINDOW MANAGER
  5. ########################################################################
  6. Copyright (c) : Christian Ruppert
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ######################################################################## */
  19. /* this implements a very simple binary tree for use with uwm's keyboard
  20. (and eventually other) configuration. it's mainly optimized on small
  21. data structure creation code, little data structure memory overhead and
  22. fast lookup. it is not adequate for treatment of huge trees or other
  23. things it has not been designed for. */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <stdlib.h>
  28. #include "tree.h"
  29. Tree *TreeRotL(Tree *tree)
  30. {
  31. if(tree->less) {
  32. Tree *s;
  33. s = tree->less;
  34. tree->less = s->greater;
  35. s->greater = tree;
  36. return s;
  37. } else return tree;
  38. }
  39. Tree *TreeRotR(Tree *tree)
  40. {
  41. if(tree->greater) {
  42. Tree *s;
  43. s = tree->greater;
  44. tree->greater = s->less;
  45. s->less = tree;
  46. return s;
  47. } else return tree;
  48. }
  49. Tree *DoTreeEquilibrate(Tree *t, int i)
  50. {
  51. int k, l;
  52. k = abs(i)/2;
  53. l = k - 1 + (abs(i) % 2);
  54. if(i > 1) {
  55. int j;
  56. for(j = 0; j < k; j++) t = TreeRotL(t);
  57. } else if(i < -1) {
  58. int j;
  59. for(j = 0; j < l; j++) t = TreeRotR(t);
  60. } else {
  61. return t;
  62. }
  63. t->greater = DoTreeEquilibrate(t->greater, -k);
  64. t->less = DoTreeEquilibrate(t->less, l);
  65. return t;
  66. }
  67. TreePot *CreateTree(NodeList *list, gelfunc order)
  68. {
  69. int i;
  70. Node *n;
  71. TreePot *tree;
  72. if(!(tree = malloc(sizeof(TreePot)))) {
  73. return NULL;
  74. }
  75. tree->order = order;
  76. tree->root = NULL;
  77. SortNodeList(list, order);
  78. n = NULL;
  79. i = 0;
  80. while(n = NodeNext(list, n)) { /* transform NodeList into linear tree */
  81. Tree *t;
  82. if(!(t = malloc(sizeof(Tree)))) {
  83. while(tree->root) {
  84. Tree *o;
  85. o = tree->root->less;
  86. free(tree->root);
  87. tree->root = o;
  88. }
  89. free(tree);
  90. return NULL;
  91. }
  92. t->less = tree->root;
  93. t->greater = NULL;
  94. t->data = n->data;
  95. i++;
  96. tree->root = t;
  97. }
  98. tree->root = DoTreeEquilibrate(tree->root, i);
  99. return tree;
  100. }
  101. Tree *GetFromTree(TreePot *tree, void *key)
  102. {
  103. Tree *t;
  104. int gel;
  105. t = tree->root;
  106. while(t && (gel = tree->order(t->data, key))) {
  107. t = (gel > 0) ? t->less : t->greater;
  108. }
  109. return t;
  110. }