-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.c
More file actions
89 lines (83 loc) · 1.82 KB
/
BinaryTree.c
File metadata and controls
89 lines (83 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "define.h"
/*
|-------------------------------------------------------------------------------------------------
|二叉树的遍历,理解递归思想的好例子
|-------------------------------------------------------------------------------------------------
*/
/**
* 构建二叉树
* @param node
* @return
*/
PBinaryNode static createBinaryNode(PBinaryNode node)
{
int value;
printf("Input the value\n");
scanf("%d", &value);
if(value == 0) node = NULL;
else{
node = (BinaryNode *)malloc(sizeof(BinaryNode));
node->data = value;
node->lChild = createBinaryNode(node->lChild);
node->rChild = createBinaryNode(node->rChild);
}
return node;
}
/**
* 二叉树的前序遍历
* @param node
* @return
*/
int static frontTraversal(PBinaryNode node)
{
if(node == NULL) return 0;
else{
printf("%d\n", node->data);
frontTraversal(node->lChild);
frontTraversal(node->rChild);
return 0;
}
}
/**
* 二叉树的中序遍历
* @param node
* @return
*/
int static middleTraversal(PBinaryNode node)
{
if(node == NULL) return 0;
else{
middleTraversal(node->lChild);
printf("%d\n", node->data);
middleTraversal(node->rChild);
return 0;
}
}
/**
* 二叉树的后序遍历
* @param node
* @return
*/
int static behindTraversal(PBinaryNode node)
{
if(node == NULL) return 0;
else{
behindTraversal(node->lChild);
behindTraversal(node->rChild);
printf("%d\n", node->data);
return 0;
}
}
int testBinary()
{
//根节点的指针
PBinaryNode root;
root = createBinaryNode(root);
//frontTraversal(root);
//middleTraversal(root);
behindTraversal(root);
}