up
Co-Authored-By: Jdhggg <111557398+Jdhggg@users.noreply.github.com>
This commit is contained in:
parent
4fdf297d56
commit
f7a523b8ca
1
.gitignore
vendored
1
.gitignore
vendored
@ -64,3 +64,4 @@ Project2.vcxproj.filters
|
|||||||
*.vcxproj
|
*.vcxproj
|
||||||
Project2.vcxproj
|
Project2.vcxproj
|
||||||
Project2.vcxproj.filters
|
Project2.vcxproj.filters
|
||||||
|
main.c
|
||||||
|
59
binary_tree.c
Normal file
59
binary_tree.c
Normal 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
18
binary_tree.h
Normal 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
|
@ -2,13 +2,13 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "linked_list_stack.h"
|
#include "linked_list_stack.h"
|
||||||
|
|
||||||
// 初始栈
|
// <EFBFBD><EFBFBD>ʼջ
|
||||||
stack_linked* init_stack_linked(void)
|
stack_linked* init_stack_linked(void)
|
||||||
{
|
{
|
||||||
stack_linked* s = (stack_linked*)malloc(sizeof(stack_linked));
|
stack_linked* s = (stack_linked*)malloc(sizeof(stack_linked));
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
{
|
{
|
||||||
printf("内存分配失败!\n");
|
printf("<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
s->top = NULL;
|
s->top = NULL;
|
||||||
@ -16,13 +16,13 @@ stack_linked* init_stack_linked(void)
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 入栈
|
// <EFBFBD><EFBFBD>ջ
|
||||||
void push_stack_linked(stack_linked* s, elem_type value)
|
void push_stack_linked(stack_linked* s, elem_type value)
|
||||||
{
|
{
|
||||||
stack_node* node = (stack_node*)malloc(sizeof(stack_node));
|
stack_node* node = (stack_node*)malloc(sizeof(stack_node));
|
||||||
if (node == NULL)
|
if (node == NULL)
|
||||||
{
|
{
|
||||||
printf("内存分配失败!\n");
|
printf("<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
node->value = value;
|
node->value = value;
|
||||||
@ -31,7 +31,7 @@ void push_stack_linked(stack_linked* s, elem_type value)
|
|||||||
s->size++;
|
s->size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 出栈
|
// <EFBFBD><EFBFBD>ջ
|
||||||
int pop_stack_linked(stack_linked* s)
|
int pop_stack_linked(stack_linked* s)
|
||||||
{
|
{
|
||||||
int flog = 0;
|
int flog = 0;
|
||||||
|
76
main.c
76
main.c
@ -1,27 +1,26 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.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++)
|
||||||
//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;
|
// N->data[i - 1] = i+10;
|
||||||
//}
|
// }
|
||||||
//for (int i = 1; i <= 10; i++)
|
// for (int i = 1; i <= 10; i++)
|
||||||
//{
|
//{
|
||||||
// L->data[i - 1] = i;
|
// L->data[i - 1] = i;
|
||||||
//}
|
// }
|
||||||
//N->length = 3;
|
// N->length = 3;
|
||||||
//L->length = 10;
|
// L->length = 10;
|
||||||
//print_sq_list(N);
|
// print_sq_list(N);
|
||||||
//print_sq_list(L);
|
// print_sq_list(L);
|
||||||
//mer_ge_sq_list(L,N);
|
// mer_ge_sq_list(L,N);
|
||||||
//print_sq_list(L);
|
// print_sq_list(L);
|
||||||
//printf("L->length:[%d]\n", L->length);
|
// printf("L->length:[%d]\n", L->length);
|
||||||
/*sq_list* L = init_sq_list();
|
/*sq_list* L = init_sq_list();
|
||||||
sq_list* N = init_sq_list();
|
sq_list* N = init_sq_list();
|
||||||
for (int i = 1; i <= 3; i++)
|
for (int i = 1; i <= 3; i++)
|
||||||
@ -40,14 +39,11 @@ int main(void)
|
|||||||
print_sq_list(L);
|
print_sq_list(L);
|
||||||
printf("L->length:[%d]\n", L->length);*/
|
printf("L->length:[%d]\n", L->length);*/
|
||||||
|
|
||||||
|
|
||||||
/* array_stack* stack = init_array_stack();
|
/* array_stack* stack = init_array_stack();
|
||||||
push_array_stack(stack, 1);
|
push_array_stack(stack, 1);
|
||||||
push_array_stack(stack, 2);
|
push_array_stack(stack, 2);
|
||||||
push_array_stack(stack, 3);*/
|
push_array_stack(stack, 3);*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* array_queue *q = init_array_queue();
|
/* array_queue *q = init_array_queue();
|
||||||
push_array_queue(q,0);
|
push_array_queue(q,0);
|
||||||
push_array_queue(q,1);
|
push_array_queue(q,1);
|
||||||
@ -60,22 +56,21 @@ int main(void)
|
|||||||
push_array_queue(q, 666);
|
push_array_queue(q, 666);
|
||||||
print_array_queue(q);*/
|
print_array_queue(q);*/
|
||||||
|
|
||||||
//node* q1 = init_node(1);
|
// node* q1 = init_node(1);
|
||||||
//node* q2 = init_node(2);
|
// node* q2 = init_node(2);
|
||||||
//node* q3 = init_node(3);
|
// node* q3 = init_node(3);
|
||||||
//node* q4 = init_node(4);
|
// node* q4 = init_node(4);
|
||||||
//q1->next = q2;
|
// q1->next = q2;
|
||||||
//q2->next = q3;
|
// q2->next = q3;
|
||||||
//q3->next = q4;
|
// q3->next = q4;
|
||||||
////address_node *n = find_node(q1, 3);
|
////address_node *n = find_node(q1, 3);
|
||||||
//// printf("%d\n %p\n", n->n,n->p
|
//// printf("%d\n %p\n", n->n,n->p
|
||||||
////int e = get_node_list(q1);
|
////int e = get_node_list(q1);
|
||||||
//node* q2_3 = init_node(0);
|
// node* q2_3 = init_node(0);
|
||||||
//insert_node(q2, q2_3);
|
// insert_node(q2, q2_3);
|
||||||
//print_node_list(q1);
|
// print_node_list(q1);
|
||||||
//delete_node(q2);
|
// delete_node(q2);
|
||||||
//print_node_list(q1);
|
// print_node_list(q1);
|
||||||
|
|
||||||
|
|
||||||
/* stack_linked* head = init_stack_linked();
|
/* stack_linked* head = init_stack_linked();
|
||||||
push_stack_linked(head, 1);
|
push_stack_linked(head, 1);
|
||||||
@ -85,7 +80,6 @@ int main(void)
|
|||||||
pop_stack_linked(head);
|
pop_stack_linked(head);
|
||||||
print_linked(head);*/
|
print_linked(head);*/
|
||||||
|
|
||||||
|
|
||||||
// queue_test();
|
// queue_test();
|
||||||
/*array_queue *q = init_array_queue();
|
/*array_queue *q = init_array_queue();
|
||||||
push_array_queue(q, 1);
|
push_array_queue(q, 1);
|
||||||
@ -107,8 +101,20 @@ int main(void)
|
|||||||
pop_link_queue(q);
|
pop_link_queue(q);
|
||||||
print_link_queue(q);*/
|
print_link_queue(q);*/
|
||||||
|
|
||||||
|
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");
|
printf("Hello World!\n");
|
||||||
system("pause");
|
system("pause");
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user