mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
删除4种遍历函数
This commit is contained in:
parent
d682bed186
commit
768c6586d3
313
src/tree.c
313
src/tree.c
@ -621,319 +621,6 @@ static void tree_destory(struct _tree* self)
|
||||
}
|
||||
}
|
||||
|
||||
static void tree_order(struct _tree* self, bool right_priority)
|
||||
{
|
||||
assert(self != NULL);
|
||||
self->_right_priority = right_priority;
|
||||
}
|
||||
|
||||
static void tree_preorder(struct _tree* self, struct _tree_node* root)
|
||||
{
|
||||
#if 0
|
||||
assert(self != NULL);
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!self->_right_priority)
|
||||
{
|
||||
self->print_obj(root->obj);
|
||||
if (root->left != NULL)
|
||||
{
|
||||
tree_preorder(self, root->left);
|
||||
}
|
||||
if (root->right != NULL)
|
||||
{
|
||||
tree_preorder(self, root->right);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self->print_obj(root->obj);
|
||||
if (root->right != NULL)
|
||||
{
|
||||
tree_preorder(self, root->right);
|
||||
}
|
||||
if (root->left != NULL)
|
||||
{
|
||||
tree_preorder(self, root->left);
|
||||
}
|
||||
}
|
||||
#else
|
||||
assert(self != NULL);
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
struct _tree_node* node = root;
|
||||
|
||||
stack_t stack = stack_new(sizeof(struct _tree_node*));
|
||||
|
||||
if (!self->_right_priority) // left priority
|
||||
{
|
||||
while (!stack->empty(stack) || node != NULL)
|
||||
{
|
||||
if (node != NULL)
|
||||
{
|
||||
self->print_obj(node->obj);
|
||||
|
||||
stack->push(stack, &node);
|
||||
node = node->left;
|
||||
}
|
||||
else
|
||||
{
|
||||
stack->pop(stack, &node);
|
||||
node = node->right;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!stack->empty(stack) || node != NULL)
|
||||
{
|
||||
if (node != NULL)
|
||||
{
|
||||
self->print_obj(node->obj);
|
||||
|
||||
stack->push(stack, &node);
|
||||
node = node->right;
|
||||
}
|
||||
else
|
||||
{
|
||||
stack->pop(stack, &node);
|
||||
node = node->left;
|
||||
}
|
||||
}
|
||||
}
|
||||
stack_free(&stack);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void tree_inorder(struct _tree* self, struct _tree_node* root)
|
||||
{
|
||||
#if 0
|
||||
assert(self != NULL);
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!self->_right_priority)
|
||||
{
|
||||
if (root->left != NULL)
|
||||
{
|
||||
tree_inorder(self, root->left);
|
||||
}
|
||||
self->print_obj(root->obj);
|
||||
if (root->right != NULL)
|
||||
{
|
||||
tree_inorder(self, root->right);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (root->right != NULL)
|
||||
{
|
||||
tree_inorder(self, root->right);
|
||||
}
|
||||
self->print_obj(root->obj);
|
||||
if (root->left != NULL)
|
||||
{
|
||||
tree_inorder(self, root->left);
|
||||
}
|
||||
}
|
||||
#else
|
||||
assert(self != NULL);
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
struct _tree_node* node = root;
|
||||
|
||||
stack_t stack = stack_new(sizeof(struct _tree_node*));
|
||||
|
||||
if (!self->_right_priority) // left priority
|
||||
{
|
||||
while (!stack->empty(stack) || node != NULL)
|
||||
{
|
||||
if (node != NULL)
|
||||
{
|
||||
stack->push(stack, &node);
|
||||
node = node->left;
|
||||
}
|
||||
else
|
||||
{
|
||||
stack->pop(stack, &node);
|
||||
self->print_obj(node->obj);
|
||||
|
||||
node = node->right;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (node != NULL || !stack->empty(stack))
|
||||
{
|
||||
if (node != NULL)
|
||||
{
|
||||
stack->push(stack, &node);
|
||||
node = node->right;
|
||||
}
|
||||
else
|
||||
{
|
||||
stack->pop(stack, &node);
|
||||
self->print_obj(node->obj);
|
||||
|
||||
node = node->left;
|
||||
}
|
||||
}
|
||||
}
|
||||
stack_free(&stack);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void tree_postorder(struct _tree* self, struct _tree_node* root)
|
||||
{
|
||||
#if 0
|
||||
assert(self != NULL);
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!self->_right_priority)
|
||||
{
|
||||
if (root->left != NULL)
|
||||
{
|
||||
tree_postorder(self, root->left);
|
||||
}
|
||||
if (root->right != NULL)
|
||||
{
|
||||
tree_postorder(self, root->right);
|
||||
}
|
||||
self->print_obj(root->obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (root->right != NULL)
|
||||
{
|
||||
tree_postorder(self, root->right);
|
||||
}
|
||||
if (root->left != NULL)
|
||||
{
|
||||
tree_postorder(self, root->left);
|
||||
}
|
||||
self->print_obj(root->obj);
|
||||
}
|
||||
#else
|
||||
assert(self != NULL);
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
struct _tree_node* node = root;
|
||||
|
||||
stack_t stack2 = stack_new(sizeof(struct _tree_node*));
|
||||
|
||||
// because: left:postorder == right:the reverse of preorder
|
||||
stack_t stack = stack_new(sizeof(struct _tree_node*));
|
||||
|
||||
if (!self->_right_priority) // left priority
|
||||
{
|
||||
while (!stack->empty(stack) || node != NULL)
|
||||
{
|
||||
if (node != NULL)
|
||||
{
|
||||
stack2->push(stack2, &node);
|
||||
|
||||
stack->push(stack, &node);
|
||||
node = node->right;
|
||||
}
|
||||
else
|
||||
{
|
||||
stack->pop(stack, &node);
|
||||
node = node->left;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!stack->empty(stack) || node != NULL)
|
||||
{
|
||||
if (node != NULL)
|
||||
{
|
||||
stack2->push(stack2, &node);
|
||||
|
||||
stack->push(stack, &node);
|
||||
node = node->left;
|
||||
}
|
||||
else
|
||||
{
|
||||
stack->pop(stack, &node);
|
||||
node = node->right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (!stack2->empty(stack2))
|
||||
{
|
||||
stack2->pop(stack2, &node);
|
||||
self->print_obj(node->obj);
|
||||
}
|
||||
|
||||
stack_free(&stack);
|
||||
stack_free(&stack2);
|
||||
#endif
|
||||
}
|
||||
|
||||
// traversal breadth
|
||||
static void tree_breadth(struct _tree* self, struct _tree_node* root)
|
||||
{
|
||||
assert(self != NULL);
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
struct _tree_node* node = root;
|
||||
queue_t queue = queue_new(sizeof(struct _tree_node*));
|
||||
|
||||
if (node != NULL)
|
||||
{
|
||||
queue->push(queue, &node);
|
||||
while (!queue->empty(queue))
|
||||
{
|
||||
queue->pop(queue, &node);
|
||||
if (!self->_right_priority)
|
||||
{
|
||||
if (node->left != NULL)
|
||||
{
|
||||
queue->push(queue, &node->left);
|
||||
}
|
||||
if (node->right != NULL)
|
||||
{
|
||||
queue->push(queue, &node->right);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (node->right != NULL)
|
||||
{
|
||||
queue->push(queue, &node->right);
|
||||
}
|
||||
if (node->left != NULL)
|
||||
{
|
||||
queue->push(queue, &node->left);
|
||||
}
|
||||
}
|
||||
self->print_obj(node->obj);
|
||||
}
|
||||
}
|
||||
queue_free(&queue);
|
||||
}
|
||||
|
||||
static struct _tree_node* tree_find_min(struct _tree* self, struct _tree_node* root)
|
||||
{
|
||||
assert(self != NULL);
|
||||
|
Loading…
Reference in New Issue
Block a user