Skip to content

Commit 3d3d91f

Browse files
committed
Add binary tree
1 parent 7f5e773 commit 3d3d91f

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

tree/binary/binary-tree.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
/*
3+
* @file binary-tree.c
4+
*
5+
* @author Akagi201
6+
* @date 2014/02/16
7+
*
8+
* binary tree C file
9+
*
10+
*/
11+
12+
#include "binary-tree.h"
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
16+
/*
17+
* @brief input pre-order -> make binary tree
18+
*
19+
* pre-order, space
20+
*
21+
* @param[in] tree: binary tree root node
22+
* @param[out] null
23+
* @return null
24+
* @sa
25+
* @note
26+
* @warning
27+
*/
28+
void make_tree(struct btnode **tree){
29+
char element;
30+
scanf("%c", &element);
31+
32+
if ((' ' == element)) {
33+
*tree = NULL;
34+
}else{
35+
if ((*tree = (struct btnode *)malloc(sizeof(struct btnode))) == NULL) {
36+
printf("make_tree malloc failed.\n");
37+
exit(1);
38+
}
39+
40+
(*tree)->data = element;
41+
42+
printf("make_tree succeed with element:%c.\n", element);
43+
make_tree(&((*tree)->left));
44+
make_tree(&((*tree)->right));
45+
}
46+
47+
return;
48+
}
49+
50+
void pre_order(struct btnode *tree){
51+
if (tree != NULL) {
52+
printf("visited: %c.\n", tree->data);
53+
pre_order(tree->left);
54+
pre_order(tree->right);
55+
}
56+
57+
return;
58+
}
59+
60+
void in_order(struct btnode *tree){
61+
if (tree != NULL) {
62+
in_order(tree->left);
63+
printf("visited: %c.\n", tree->data);
64+
in_order(tree->right);
65+
}
66+
67+
return;
68+
}
69+
70+
void post_order(struct btnode *tree){
71+
if (tree != NULL) {
72+
post_order(tree->left);
73+
post_order(tree->right);
74+
printf("visited: %c.\n", tree->data);
75+
}
76+
77+
return;
78+
}
79+
80+
void level_order(struct btnode *tree){
81+
82+
}
83+
84+
int main(void){
85+
struct btnode *tree;
86+
make_tree(&tree);
87+
pre_order(tree);
88+
in_order(tree);
89+
post_order(tree);
90+
level_order(tree);
91+
return 0;
92+
}

tree/binary/binary-tree.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/*
3+
* @file binary-tree.h
4+
*
5+
* @author Akagi201
6+
* @date 2014/02/16
7+
*
8+
* binary tree header file
9+
*
10+
*/
11+
12+
#ifndef BINARY_TREE_H_
13+
#define BINARY_TREE_H_
14+
15+
/*
16+
* @struct btnode
17+
*
18+
* brief definition of binary tree
19+
*
20+
* using pointer
21+
*
22+
* @author Akagi201
23+
* @date 2014/02/16
24+
*/
25+
struct btnode{
26+
char data;
27+
struct btnode *left;
28+
struct btnode *right;
29+
};
30+
31+
void make_tree(struct btnode **tree);
32+
33+
void pre_order(struct btnode *tree);
34+
35+
void in_order(struct btnode *tree);
36+
37+
void post_order(struct btnode *tree);
38+
39+
void level_order(struct btnode *tree);
40+
41+
#endif

0 commit comments

Comments
 (0)