-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.c
More file actions
86 lines (69 loc) · 1.9 KB
/
Copy pathtree.c
File metadata and controls
86 lines (69 loc) · 1.9 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
//
// main.c
// TreeTest
//
// Created by 王浩宇 on 2018/7/6.
// Copyright © 2018 UCAS Developers. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
struct treeNode {
double value;
struct treeNode * p1;
struct treeNode * p2;
};
struct treeNode * createTreeByRecursion(int level) {
struct treeNode * motherNode = (struct treeNode *)malloc(sizeof(struct treeNode));
motherNode -> p1 = NULL;
motherNode -> p2 = NULL;
if (level <= 1) {
return motherNode;
}
struct treeNode * Node1 = (struct treeNode *)malloc(sizeof(struct treeNode));
struct treeNode * Node2 = (struct treeNode *)malloc(sizeof(struct treeNode));
motherNode -> p1 = Node1;
motherNode -> p2 = Node2;
Node1 -> p1 = NULL;
Node1 -> p2 = NULL;
Node2 -> p1 = NULL;
Node2 -> p2 = NULL;
if (level == 2) {
return motherNode;
}
Node1 -> p1 = createTreeByRecursion(level - 2);
Node1 -> p2 = createTreeByRecursion(level - 2);
Node2 -> p1 = createTreeByRecursion(level - 2);
Node2 -> p2 = createTreeByRecursion(level - 2);
return motherNode;
}
void treeInput(struct treeNode * treeHead) {
double number;
printf("请输入值:");
scanf("%lf", &number);
treeHead -> value = number;
if (treeHead -> p1 == NULL) {
return;
}
treeInput(treeHead -> p1);
treeInput(treeHead -> p2);
}
void treeOutput(struct treeNode * treeHead) {
printf("%g", treeHead -> value);
if (treeHead -> p1 == NULL) {
printf("\n");
return;
}
treeOutput(treeHead -> p1);
treeOutput(treeHead -> p2);
}
int main() {
int level;
printf("请输入树的层数:");
scanf("%d", &level);
struct treeNode * treeHead = createTreeByRecursion(level);
treeInput(treeHead);
treeOutput(treeHead);
return 0;
}