-
Notifications
You must be signed in to change notification settings - Fork 0
/
crack_4_3.c
74 lines (68 loc) · 1.37 KB
/
crack_4_3.c
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
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
typedef struct tnode {
int data;
struct tnode *left, *right;
} tnode;
tnode *addTree(tnode *, int);
void treeprint(tnode *);
int getHeight(tnode *);
tnode *getmidle(tnode *, int [], int, int);
tnode *root;
int height, max;
main()
{
int test[SIZE] = {
1,3,5,7,9,10,11,12,13,14
};
int n = 10;
height = max = 0;
root = getmidle(root, test, 0, 9);
getHeight(root);
printf("max_height= %d\n", max);
//treeprint(root);
return 0;
}
tnode *getmidle(tnode *node, int test[], int begin, int end)
{
if (begin <= end) {
int temp = (begin + end) / 2;
node = addTree(node, test[temp]);
getmidle(node, test, begin, temp-1);
getmidle(node, test, temp+1, end);
return node;
}
return NULL;
}
tnode *addTree(tnode *node, int data)
{
if (node == NULL) {
node = (tnode *)malloc(sizeof(tnode));
node->data = data;
node->left = node->right = NULL;
} else if (node->data > data)
node->right = addTree(node->right, data);
else if (node->data <= data)
node->left = addTree(node->left, data);
return node;
}
void treeprint(tnode *root)
{
if (root == NULL) return ;
printf("%d\n", root->data);
treeprint(root->left);
treeprint(root->right);
}
int getHeight(tnode *node)
{
if (node == NULL) {
if (height > max)
max = height;
return max;
}
height++;
getHeight(node->left);
getHeight(node->right);
height--;
}