-
Notifications
You must be signed in to change notification settings - Fork 0
/
290.c
134 lines (115 loc) · 2.68 KB
/
290.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// http://www.bjfuacm.com/problem/290/
#include <stdio.h>
#include <stdlib.h>
struct TreeNode;
struct TreeNode {
struct TreeNode *left;
struct TreeNode *right;
int val;
int count;
};
typedef struct TreeNode *PtrToTree;
typedef PtrToTree SearchTree;
SearchTree BuildBST(int );
void PrintTree(SearchTree , int );
void DestroyTree(SearchTree );
int GetMax(SearchTree );
void InsertNode(SearchTree , SearchTree , SearchTree , SearchTree );
void PrintCount(SearchTree , int );
int main()
{
int n; // node num
int m; // search node
while (1) {
scanf("%d", &n);
if (n == 0) break;
SearchTree K = BuildBST(n);
scanf("%d", &m);
SearchTree i = (SearchTree)calloc(1, sizeof(*i));
i->val = m;
SearchTree pre;
SearchTree q;
InsertNode(pre, q, i, K);
PrintTree(K, GetMax(K));
PrintCount(K, GetMax(K));
DestroyTree(K);
}
}
void InsertNode(SearchTree pre, SearchTree q, SearchTree p, SearchTree root)
{
/**
* params:
* pre (q'previous) to assign ptr
* q to get right pos
* p is node to insert
* root node
*/
q = root;
while (q != NULL) {
if (p->val > q->val) {// go right
pre = q;
q = q->right;
} else if (p->val < q->val) {
pre = q;
q = q->left;
} else { // p->val == q->val
q->count += 1;
free(p);
return;
}
}
if (p->val > pre->val) {
pre->right = p;
} else { // p->val < pre
pre->left = p;
}
}
SearchTree BuildBST(int n)
{
SearchTree p;
SearchTree root;
SearchTree q; // get right position
SearchTree q_p; // save q's previous to assign pointer
for (int i = 0; i < n; i++) {
p = (SearchTree)calloc(1, sizeof(*p));
scanf("%d", &(p->val));
p->left = NULL;
p->right = NULL;
p->count = 0;
if (i == 0) { // is root
root = p;
} else {
InsertNode(q_p, q, p, root);
}
}
return root;
}
int GetMax(SearchTree K)
{
if (K->right != NULL) {
return GetMax(K->right);
} else {
return K->val;
}
}
void PrintTree(SearchTree K, int max)
{
if (K == NULL) return;
PrintTree(K->left, max);
printf("%d%s", K->val, K->val == max? "\n": " ");
PrintTree(K->right, max);
}
void PrintCount(SearchTree K, int max)
{
if (K == NULL) return;
PrintCount(K->left, max);
printf("%d%s", K->count, K->val == max? "\n": " ");
PrintCount(K->right, max);
}
void DestroyTree(SearchTree K)
{
if (K == NULL) return;
DestroyTree(K->left);
DestroyTree(K->right);
free(K);
}