3 * Iteration for AVL trees
5 * (c) 2024 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Xyla, a library of binary trees.
12 * Xyla is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Lesser General Public License as published by the
14 * Free Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * Xyla is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20 * License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with Xyla. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
31 /*----- Main code ---------------------------------------------------------*/
33 void xyla_avl_inititer(const struct xyla_bt_node *root,
34 struct xyla_avl_iter *it)
36 /* Initialize the iterator IT to start returning nodes in left-to-right
37 * order from the tree rooted at ROOT.
43 XYLA_BT_STEPITER(rc, root, it->stack, it->sp, XYLA_AVL_HTMAX, left);
47 void *xyla_avl_next(struct xyla_avl_iter *it)
49 /* Advance the iterator IT, returning the next node, or null if the
50 * iteration is complete.
52 * Inserting or removing elements invalidates the iterator. As a special
53 * exception, it is permitted to free all of the nodes by iterating over
54 * the tree in the obvious manner:
56 * xyla_avl_inititer(tree, &it);
58 * node = xyla_avl_next(&it); if (!node) break;
62 * It's probably better to use `xyla_bt_severfirst' for this task.
65 const struct xyla_bt_node *node;
69 node = it->stack[--sp];
70 XYLA_BT_STEPITER(rc, node->right, it->stack, sp, XYLA_AVL_HTMAX, left);
72 it->sp = sp; return (UNCONST(void, node));
75 void xyla_avl_initriter(const struct xyla_bt_node *root,
76 struct xyla_avl_riter *rit)
78 /* Initialize the iterator RIT to start returning nodes in right-to-left
79 * order from the tree rooted at ROOT.
85 XYLA_BT_STEPITER(rc, root, rit->stack, rit->sp, XYLA_AVL_HTMAX, right);
89 void *xyla_avl_prev(struct xyla_avl_riter *rit)
91 /* Advance the iterator RIT, returning the previous node, or null if the
92 * iteration is complete.
94 * Inserting or removing elements invalidates the iterator. As a special
95 * exception, it is permitted to free all of the nodes by iterating over
96 * the tree in the obvious manner:
98 * xyla_avl_initriter(tree, &rit);
100 * node = xyla_avl_prev(&it); if (!node) break;
104 * It's probably better to use `xyla_bt_severfirst' for this task.
107 const struct xyla_bt_node *node;
108 int rc, sp = rit->sp;
111 node = rit->stack[--sp];
112 XYLA_BT_STEPITER(rc, node->left, rit->stack, sp, XYLA_AVL_HTMAX, right);
114 rit->sp = sp; return (UNCONST(void, node));
117 /*----- That's all, folks -------------------------------------------------*/