Skip to content

Commit 0785e22

Browse files
GorrayLiRbb666
authored andcommitted
[components/lwp]add doxygen comment for lwp avl implementation.
1 parent ef354df commit 0785e22

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

components/lwp/lwp_avl.c

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2020, RT-Thread Development Team
2+
* Copyright (c) 2006-2025 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -10,6 +10,19 @@
1010
#include <rtthread.h>
1111
#include <lwp_avl.h>
1212

13+
/**
14+
* @brief Rebalances an AVL tree after insertion or deletion
15+
*
16+
* @param[in,out] nodeplaces_ptr Pointer to stack of node pointers that need rebalancing
17+
* @param[in] count Number of nodes in the stack that need rebalancing
18+
*
19+
* @note This function performs AVL tree rebalancing by checking and correcting height imbalances
20+
* between left and right subtrees. It handles:
21+
* - Left-left case (single right rotation)
22+
* - Left-right case (double rotation: left then right)
23+
* - Right-right case (single left rotation)
24+
* - Right-left case (double rotation: right then left)
25+
*/
1326
static void lwp_avl_rebalance(struct lwp_avl_struct ***nodeplaces_ptr, int count)
1427
{
1528
for (; count > 0; count--)
@@ -76,6 +89,18 @@ static void lwp_avl_rebalance(struct lwp_avl_struct ***nodeplaces_ptr, int count
7689
}
7790
}
7891

92+
/**
93+
* @brief Removes a node from an AVL tree while maintaining balance
94+
*
95+
* @param[in] node_to_delete The node to be removed from the AVL tree
96+
* @param[in,out] ptree Pointer to the root node pointer of the AVL tree
97+
*
98+
* @note This function removes the specified node from the AVL tree and performs
99+
* necessary rebalancing operations. It handles both cases where the node
100+
* has no left child (simple removal) and where it has a left child (finding
101+
* the rightmost node in the left subtree as replacement).
102+
* It uses a stack to track the removal path for rebalancing.
103+
*/
79104
void lwp_avl_remove(struct lwp_avl_struct *node_to_delete, struct lwp_avl_struct **ptree)
80105
{
81106
avl_key_t key = node_to_delete->avl_key;
@@ -132,6 +157,14 @@ void lwp_avl_remove(struct lwp_avl_struct *node_to_delete, struct lwp_avl_struct
132157
lwp_avl_rebalance(stack_ptr, stack_count);
133158
}
134159

160+
/**
161+
* @brief Inserts a new node into an AVL tree while maintaining balance
162+
*
163+
* @param[in] new_node The new node to be inserted into the AVL tree
164+
* @param[in,out] ptree Pointer to the root node pointer of the AVL tree
165+
*
166+
* @note Uses a stack to track the insertion path for rebalancing
167+
*/
135168
void lwp_avl_insert(struct lwp_avl_struct *new_node, struct lwp_avl_struct **ptree)
136169
{
137170
avl_key_t key = new_node->avl_key;
@@ -158,6 +191,18 @@ void lwp_avl_insert(struct lwp_avl_struct *new_node, struct lwp_avl_struct **ptr
158191
lwp_avl_rebalance(stack_ptr, stack_count);
159192
}
160193

194+
/**
195+
* @brief Finds a node in an AVL tree by key
196+
*
197+
* @param[in] key The key to search for in the AVL tree
198+
* @param[in] ptree Pointer to the root node of the AVL tree
199+
*
200+
* @return struct lwp_avl_struct* Pointer to the found node, or NULL if not found
201+
*
202+
* @note This function searches the AVL tree for a node with the specified key.
203+
* It performs a standard binary search by comparing keys and traversing
204+
* left or right subtrees accordingly.
205+
*/
161206
struct lwp_avl_struct *lwp_avl_find(avl_key_t key, struct lwp_avl_struct *ptree)
162207
{
163208
for (;;)
@@ -176,6 +221,19 @@ struct lwp_avl_struct *lwp_avl_find(avl_key_t key, struct lwp_avl_struct *ptree)
176221
return ptree;
177222
}
178223

224+
/**
225+
* @brief Recursively traverses an AVL tree and applies a function to each node
226+
*
227+
* @param[in] ptree Pointer to the root node of the AVL tree
228+
* @param[in] fun Callback function to apply to each node
229+
* @param[in,out] arg Additional argument passed to the callback function
230+
*
231+
* @return int Returns the last result from the callback function (0 if all nodes processed)
232+
*
233+
* @note This function performs an in-order traversal of the AVL tree, applying the
234+
* provided callback function to each node. The traversal can be stopped early
235+
* if the callback returns a non-zero value.
236+
*/
179237
int lwp_avl_traversal(struct lwp_avl_struct *ptree, int (*fun)(struct lwp_avl_struct *, void *), void *arg)
180238
{
181239
int ret;
@@ -208,6 +266,16 @@ int lwp_avl_traversal(struct lwp_avl_struct *ptree, int (*fun)(struct lwp_avl_st
208266
return ret;
209267
}
210268

269+
/**
270+
* @brief Finds the first (leftmost) node in an AVL tree
271+
*
272+
* @param[in] ptree Pointer to the root node of the AVL tree
273+
*
274+
* @return struct lwp_avl_struct* Pointer to the leftmost node, or NULL if tree is empty
275+
*
276+
* @note This function traverses the AVL tree to find the leftmost node, which represents
277+
* the minimum element in the tree. It's commonly used for ordered traversal starting point.
278+
*/
211279
rt_weak struct lwp_avl_struct* lwp_map_find_first(struct lwp_avl_struct* ptree)
212280
{
213281
if (ptree == AVL_EMPTY)
@@ -224,4 +292,3 @@ rt_weak struct lwp_avl_struct* lwp_map_find_first(struct lwp_avl_struct* ptree)
224292
}
225293
return ptree;
226294
}
227-

components/lwp/lwp_avl.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2020, RT-Thread Development Team
2+
* Copyright (c) 2006-2025 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -24,13 +24,16 @@ extern "C" {
2424
#define avl_maxheight 32
2525
#define heightof(tree) ((tree) == AVL_EMPTY ? 0 : (tree)->avl_height)
2626

27+
/**
28+
* @brief AVL tree node structure for thread ID (tid) or Process ID (pid) management
29+
*/
2730
struct lwp_avl_struct
2831
{
29-
struct lwp_avl_struct *avl_left;
30-
struct lwp_avl_struct *avl_right;
31-
int avl_height;
32-
avl_key_t avl_key;
33-
void *data;
32+
struct lwp_avl_struct *avl_left; /**< Pointer to left child node */
33+
struct lwp_avl_struct *avl_right; /**< Pointer to right child node */
34+
int avl_height; /**< Height of the node in the AVL tree */
35+
avl_key_t avl_key; /**< Key value used for AVL tree node comparison */
36+
void *data; /**< Pointer to associated data */
3437
};
3538

3639
void lwp_avl_remove(struct lwp_avl_struct * node_to_delete, struct lwp_avl_struct ** ptree);

0 commit comments

Comments
 (0)