Co-Authored-By: Jdhggg <111557398+Jdhggg@users.noreply.github.com>
This commit is contained in:
jdh 2025-04-29 18:01:09 +08:00
parent 4fdf297d56
commit f7a523b8ca
6 changed files with 175 additions and 91 deletions

1
.gitignore vendored
View File

@ -64,3 +64,4 @@ Project2.vcxproj.filters
*.vcxproj
Project2.vcxproj
Project2.vcxproj.filters
main.c

59
binary_tree.c Normal file
View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "binary_tree.h"
tree_node *init_binary_tree(elem_type value)
{
tree_node *node = (tree_node *)malloc(sizeof(tree_node));
if (node == NULL)
{
printf("error: malloc failed from:init_binary_tree\n");
}
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
}
// 插入节点
void insert_tree_node(tree_node *node,tree_node *node_new)
{
if (node == NULL)
{
node ->left = node_new;
return;
}
tree_node *temp = node;
node ->left = node_new;
}
// 删除节点
// 遍历树节点 bfs
void traverse_tree(tree_node *roots)
{
tree_node *queue[3] = {NULL,NULL,NULL};
int front = 0;
int rear = 0;
if (roots == NULL)
{
printf("error: roots is NULL\n");
return;
}
while(roots)
{
tree_node *temp[2] = {roots->left,roots->right};
for (int i = 0;i<2;i++)
{
if (temp[i] != NULL)
{
queue[rear] = temp[i];
rear = (rear + 1) % 3;
printf("%d ",temp[i]->value);
}
}
roots = queue[front];
front = (front+1) % 3;
}
}

18
binary_tree.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef BINARY_TREE
#define BINARY_TREE
#define elem_type int
typedef struct tree_node
{
elem_type value;
struct tree_node *left;
struct tree_node *right;
}tree_node;
// 初始化树节点
tree_node *init_binary_tree(elem_type value);
// 插入节点
void insert_tree_node(tree_node *node,tree_node *node_new);
// 删除节点
void delete_node(tree_node *node);
// 遍历树节点
void traverse_tree(tree_node *roots);
#endif // DEBUG

View File

@ -2,13 +2,13 @@
#include <stdlib.h>
#include "linked_list_stack.h"
// 初始栈
// <EFBFBD><EFBFBD>ʼջ
stack_linked* init_stack_linked(void)
{
stack_linked* s = (stack_linked*)malloc(sizeof(stack_linked));
if (s == NULL)
{
printf("内存分配失败!\n");
printf("<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>\n");
return NULL;
}
s->top = NULL;
@ -16,13 +16,13 @@ stack_linked* init_stack_linked(void)
return s;
}
// 入栈
// <EFBFBD><EFBFBD>ջ
void push_stack_linked(stack_linked* s, elem_type value)
{
stack_node* node = (stack_node*)malloc(sizeof(stack_node));
if (node == NULL)
{
printf("内存分配失败!\n");
printf("<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>\n");
return NULL;
}
node->value = value;
@ -31,7 +31,7 @@ void push_stack_linked(stack_linked* s, elem_type value)
s->size++;
}
// 出栈
// <EFBFBD><EFBFBD>ջ
int pop_stack_linked(stack_linked* s)
{
int flog = 0;

View File

@ -31,4 +31,4 @@ int empty_link_queue(link_queue* q);
int print_link_queue(link_queue* q);
#endif
#endif

176
main.c
View File

@ -1,90 +1,84 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "binary_tree.h"
int main(void) {
int main(void)
{
// sq_list* L = init_sq_list();
// sq_list* N = init_sq_list();
// for (int i = 1; i <= 3; i++)
//{
// N->data[i - 1] = i+10;
// }
// for (int i = 1; i <= 10; i++)
//{
// L->data[i - 1] = i;
// }
// N->length = 3;
// L->length = 10;
// print_sq_list(N);
// print_sq_list(L);
// mer_ge_sq_list(L,N);
// print_sq_list(L);
// printf("L->length:[%d]\n", L->length);
/*sq_list* L = init_sq_list();
sq_list* N = init_sq_list();
for (int i = 1; i <= 3; i++)
{
N->data[i - 1] = i+10;
}
for (int i = 1; i <= 10; i++)
{
L->data[i - 1] = i;
}
N->length = 3;
L->length = 10;
print_sq_list(N);
print_sq_list(L);
mer_ge_sq_list(L,N);
print_sq_list(L);
printf("L->length:[%d]\n", L->length);*/
//sq_list* L = init_sq_list();
//sq_list* N = init_sq_list();
//for (int i = 1; i <= 3; i++)
//{
// N->data[i - 1] = i+10;
//}
//for (int i = 1; i <= 10; i++)
//{
// L->data[i - 1] = i;
//}
//N->length = 3;
//L->length = 10;
//print_sq_list(N);
//print_sq_list(L);
//mer_ge_sq_list(L,N);
//print_sq_list(L);
//printf("L->length:[%d]\n", L->length);
/*sq_list* L = init_sq_list();
sq_list* N = init_sq_list();
for (int i = 1; i <= 3; i++)
{
N->data[i - 1] = i+10;
}
for (int i = 1; i <= 10; i++)
{
L->data[i - 1] = i;
}
N->length = 3;
L->length = 10;
print_sq_list(N);
print_sq_list(L);
mer_ge_sq_list(L,N);
print_sq_list(L);
printf("L->length:[%d]\n", L->length);*/
/* array_stack* stack = init_array_stack();
push_array_stack(stack, 1);
push_array_stack(stack, 2);
push_array_stack(stack, 3);*/
/* array_stack* stack = init_array_stack();
push_array_stack(stack, 1);
push_array_stack(stack, 2);
push_array_stack(stack, 3);*/
/* array_queue *q = init_array_queue();
push_array_queue(q,0);
push_array_queue(q,1);
push_array_queue(q,2);
print_array_queue(q);
printf("---------------------\n");
pop_array_queue(q);
print_array_queue(q);
printf("----------\n");
push_array_queue(q, 666);
print_array_queue(q);*/
// node* q1 = init_node(1);
// node* q2 = init_node(2);
// node* q3 = init_node(3);
// node* q4 = init_node(4);
// q1->next = q2;
// q2->next = q3;
// q3->next = q4;
////address_node *n = find_node(q1, 3);
//// printf("%d\n %p\n", n->n,n->p
////int e = get_node_list(q1);
// node* q2_3 = init_node(0);
// insert_node(q2, q2_3);
// print_node_list(q1);
// delete_node(q2);
// print_node_list(q1);
/* array_queue *q = init_array_queue();
push_array_queue(q,0);
push_array_queue(q,1);
push_array_queue(q,2);
print_array_queue(q);
printf("---------------------\n");
pop_array_queue(q);
print_array_queue(q);
printf("----------\n");
push_array_queue(q, 666);
print_array_queue(q);*/
//node* q1 = init_node(1);
//node* q2 = init_node(2);
//node* q3 = init_node(3);
//node* q4 = init_node(4);
//q1->next = q2;
//q2->next = q3;
//q3->next = q4;
////address_node *n = find_node(q1, 3);
//// printf("%d\n %p\n", n->n,n->p
////int e = get_node_list(q1);
//node* q2_3 = init_node(0);
//insert_node(q2, q2_3);
//print_node_list(q1);
//delete_node(q2);
//print_node_list(q1);
/* stack_linked* head = init_stack_linked();
push_stack_linked(head, 1);
push_stack_linked(head, 2);
push_stack_linked(head, 3);
print_linked(head);
pop_stack_linked(head);
print_linked(head);*/
/* stack_linked* head = init_stack_linked();
push_stack_linked(head, 1);
push_stack_linked(head, 2);
push_stack_linked(head, 3);
print_linked(head);
pop_stack_linked(head);
print_linked(head);*/
// queue_test();
/*array_queue *q = init_array_queue();
@ -106,10 +100,22 @@ int main(void)
printf("____________________\n");
pop_link_queue(q);
print_link_queue(q);*/
printf("Hello World!\n");
system("pause");
return 0;
tree_node *root = init_binary_tree(1);
tree_node *node1 = init_binary_tree(2);
tree_node *node2 = init_binary_tree(3);
tree_node *node3 = init_binary_tree(4);
tree_node *node4 = init_binary_tree(5);
tree_node *node5 = init_binary_tree(6);
tree_node *node6 = init_binary_tree(7);
root->left = node1;
root->right = node2;
node1->left = node3;
node1->right = node4;
node2->left = node5;
node2->right = node6;
traverse_tree(root);
printf("Hello World!\n");
system("pause");
return 0;
}