为什么会死在这里函数!

This commit is contained in:
建峰 2024-06-23 16:03:14 +08:00
parent 595e46c375
commit 2f3f1bdf37
4 changed files with 76 additions and 6 deletions

4
.vscode/launch.json vendored
View File

@ -10,7 +10,7 @@
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/build/release/bin/test.exe", "program": "${workspaceFolder}/build/release/bin/test.exe",
"args": [], "args": [],
"stopAtEntry": false, "stopAtEntry": true,
"cwd": "${fileDirname}", "cwd": "${fileDirname}",
"environment": [], "environment": [],
"externalConsole": false, "externalConsole": false,
@ -28,7 +28,7 @@
} }
], ],
"preLaunchTask": "build", "preLaunchTask": "build",
"miDebuggerPath": "D:/Qt/Tools/mingw1120_64/bin/gdb.exe" "miDebuggerPath": "D:/Lang/mingw64/bin/gdb.exe"
} }
] ]
} }

View File

@ -87,7 +87,6 @@ struct _tree_node
uint32_t color; uint32_t color;
}; };
}; };
typedef struct _tree_node* tree_node_t;
struct _tree struct _tree
{ {
@ -103,6 +102,8 @@ struct _tree
bool (*delete)(struct _tree* self, void* obj); bool (*delete)(struct _tree* self, void* obj);
bool (*peek)(struct _tree* self, void** obj); bool (*peek)(struct _tree* self, void** obj);
struct _tree_node* (*find)(struct _tree* self, void* obj);
bool (*clear)(struct _tree* self); bool (*clear)(struct _tree* self);
bool (*empty)(struct _tree* self); bool (*empty)(struct _tree* self);
uint32_t (*size)(struct _tree* self); uint32_t (*size)(struct _tree* self);

View File

@ -1402,7 +1402,7 @@ static bool tree_rebalance(ptree_t head, ptree_node_t tree)
#endif #endif
static tree_node_t tree_node_new(struct _tree* self, void* obj) static struct _tree_node * tree_node_new(struct _tree* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
@ -1450,6 +1450,47 @@ static bool tree_node_free(struct _tree_node* node)
return true; return true;
} }
/**
* @brief
*
* @param self
* @param obj
*/
struct _tree_node * tree_find_pos(struct _tree* self, void* obj)
{
printf("222\n");
assert(self != NULL);
assert(self->compare != NULL);
struct _tree_node* root = self->_root;
while(root != NULL)
{
if(self->compare(obj, root->obj) == 0)
{
break;
}
else if(self->compare(obj, root->obj) < 0)
{
if(root->left == NULL)
{
break;
}
root = root->left;
}
else
{
if(root->right == NULL)
{
break;
}
root = root->right;
}
}
return root;
}
bool tree_avl_insert(struct _tree* self, void* obj) bool tree_avl_insert(struct _tree* self, void* obj)
{ {
assert(self != NULL); assert(self != NULL);
@ -1470,7 +1511,10 @@ bool tree_avl_insert(struct _tree* self, void* obj)
else else
{ {
// insert the node // insert the node
struct _tree_node* root = self->_root; struct _tree_node* root = NULL;
printf("111\n");
root = tree_find_pos(self, root->obj);
printf("444\n");
if(self->compare(obj, root->obj) < 0) if(self->compare(obj, root->obj) < 0)
{ {
root->left = node; root->left = node;
@ -1498,6 +1542,28 @@ bool tree_avl_delete(struct _tree* self, void* obj)
} }
struct _tree_node * tree_avl_find(struct _tree* self, void* obj)
{
assert(self != NULL);
struct _tree_node* root = self->_root;
while(root != NULL)
{
if(self->compare(obj, root->obj) == 0)
{
return root;
}
else if(self->compare(obj, root->obj) < 0)
{
root = root->left;
}
else
{
root = root->right;
}
}
return NULL;
}
bool tree_clear(struct _tree* self) bool tree_clear(struct _tree* self)
{ {
assert(self != NULL); assert(self != NULL);
@ -1521,6 +1587,7 @@ void tree_destory(struct _tree* self)
{ {
assert(self != NULL); assert(self != NULL);
self->clear(self); self->clear(self);
self->_root = NULL;
} }
void tree_avl_preorder(struct _tree* self, struct _tree_node* root) void tree_avl_preorder(struct _tree* self, struct _tree_node* root)
@ -1573,6 +1640,8 @@ bool tree_avl_init(struct _tree *self, uint32_t obj_size)
self->postorder = tree_avl_postorder; self->postorder = tree_avl_postorder;
self->breadth = tree_avl_breadth; self->breadth = tree_avl_breadth;
self->_root = NULL;
return true; return true;
} }

View File

@ -281,7 +281,7 @@ void test_tree_num(void)
printf("\n\n----- test_queue_num -----\n"); printf("\n\n----- test_queue_num -----\n");
printf("----- after push-----\n"); printf("----- insert -----\n");
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
tree->insert(tree, &data[i]); tree->insert(tree, &data[i]);