| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 2023 Hannes von Haugwitz |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include "tree.h" |
| 23 | #include "util.h" |
| 24 | |
| 25 | struct tree_node { |
| 26 | void *key; |
| 27 | void *data; |
| 28 | |
| 29 | long unsigned level; |
| 30 | |
| 31 | struct tree_node *parent; |
| 32 | struct tree_node *left; |
| 33 | struct tree_node *right; |
| 34 | }; |
| 35 | |
| 36 | static tree_node *tree_new_node(void *key, tree_node *parent) { |
| 37 | tree_node *n = checked_malloc(sizeof(tree_node)); |
| 38 | n->key = key; |
| 39 | n->data = NULL; |
| 40 | |
| 41 | n->level = 1; |
| 42 | |
| 43 | n->parent = parent; |
| 44 | n->left = NULL; |
| 45 | n->right = NULL; |
| 46 | |
| 47 | return n; |
| 48 | } |
| 49 | |
| 50 | /* implements an AA tree, a simplified red-black tree */ |
| 51 | static tree_node *_insert(tree_node *n, tree_node *parent, void *key, void *data, tree_cmp_f compare) { |
| 52 | if (n == NULL) { |
| 53 | n = tree_new_node(key, parent); |
| 54 | n->data = data; |
| 55 | return n; |
| 56 | } |
| 57 | int c = compare(key, n->key); |
| 58 | if (c < 0) { |
| 59 | n->left = _insert(n->left, n, key, data, compare); |
| 60 | } else if (c > 0) { |
| 61 | n->right = _insert(n->right, n, key, data, compare); |
| 62 | } |
| 63 | if (n != NULL && n->left != NULL && n->level == (n->left)->level) { |
| 64 | tree_node *l = n->left; |
| 65 | n->left = l->right; |
| 66 | if (n->left) { |
| 67 | (n->left)->parent = n; |
| 68 | } |
| 69 | l->right = n; |
| 70 | l->parent = n->parent; |
| 71 | n->parent = l; |
| 72 | n = l; |
| 73 | } |
| 74 | if (n != NULL && n->right != NULL && (n->right)->right != NULL |
| 75 | && n->level == ((n->right)->right)->level) { |
| 76 | tree_node *r = n->right; |
| 77 | n->right = r->left; |
| 78 | if (n->right) { |
| 79 | (n->right)->parent = n; |
| 80 | } |
| 81 | r->left = n; |
| 82 | r->parent = n->parent; |
| 83 | n->parent = r; |
| 84 | r->level++; |
| 85 | n = r; |
| 86 | } |
| 87 | return n; |
| 88 | } |
| 89 | |
| 90 | tree_node *tree_insert(tree_node *root, void *key, void * data, tree_cmp_f compare) { |
| 91 | root = _insert(root, NULL, key, data, compare); |
| 92 | root->parent = NULL; |
| 93 | return root; |
| 94 | } |
| 95 | |
| 96 | void *tree_search(tree_node *root, void *key, int (*compare) (const void*, const void*)) { |
| 97 | if (root == NULL) { |
| 98 | return NULL; |
| 99 | } |
| 100 | tree_node *n = root; |
| 101 | |
| 102 | int c; |
| 103 | while (n != NULL && (c = compare(key, n->key)) != 0) { |
| 104 | if (c < 0) { |
| 105 | n = n->left; |
| 106 | } else { |
| 107 | n = n->right; |
| 108 | } |
| 109 | } |
| 110 | return n?n->data:NULL; |
| 111 | } |
| 112 | |
| 113 | tree_node *tree_walk_first(tree_node *root) { |
| 114 | tree_node *n = root; |
| 115 | if (n) { |
| 116 | if (n->left) { |
| 117 | while (n->left != NULL) { |
| 118 | n = n->left; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | return n; |
| 123 | } |
| 124 | |
| 125 | tree_node *tree_walk_next(tree_node *m) { |
| 126 | if (m != NULL) { |
| 127 | if (m->right != NULL) { |
| 128 | m = m->right; |
| 129 | while (m->left) { |
| 130 | m = m->left; |
| 131 | } |
| 132 | } else { |
| 133 | while (m->parent != NULL && (m->parent)->right == m) { |
| 134 | m = m->parent; |
| 135 | } |
| 136 | m = m->parent; |
| 137 | } |
| 138 | } |
| 139 | return m; |
| 140 | } |
| 141 | |
| 142 | void *tree_get_data(tree_node *n) { |
| 143 | return n->data; |
| 144 | } |