* @brief Inserts a new node into an AVL tree while maintaining balance * * @param[in] new_node The new node to be inserted into the AVL tree * @param[in,out] ptree Pointer to the root node pointer of the AVL tree * * @note Uses a stack to track the insertion path for rebalancing */
| 166 | * @note Uses a stack to track the insertion path for rebalancing |
| 167 | */ |
| 168 | void lwp_avl_insert(struct lwp_avl_struct *new_node, struct lwp_avl_struct **ptree) |
| 169 | { |
| 170 | avl_key_t key = new_node->avl_key; |
| 171 | struct lwp_avl_struct **nodeplace = ptree; |
| 172 | struct lwp_avl_struct **stack[avl_maxheight]; |
| 173 | int stack_count = 0; |
| 174 | struct lwp_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */ |
| 175 | for (;;) |
| 176 | { |
| 177 | struct lwp_avl_struct *node = *nodeplace; |
| 178 | if (node == AVL_EMPTY) |
| 179 | break; |
| 180 | *stack_ptr++ = nodeplace; |
| 181 | stack_count++; |
| 182 | if (key < node->avl_key) |
| 183 | nodeplace = &node->avl_left; |
| 184 | else |
| 185 | nodeplace = &node->avl_right; |
| 186 | } |
| 187 | new_node->avl_left = AVL_EMPTY; |
| 188 | new_node->avl_right = AVL_EMPTY; |
| 189 | new_node->avl_height = 1; |
| 190 | *nodeplace = new_node; |
| 191 | lwp_avl_rebalance(stack_ptr, stack_count); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @brief Finds a node in an AVL tree by key |
no test coverage detected