-
Notifications
You must be signed in to change notification settings - Fork 1
/
decare_tree.c
144 lines (130 loc) · 2.1 KB
/
decare_tree.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
#include "stdio.h"
#include "stdlib.h"
#define ERRORMin 99999
#define ERRORMax -99999
typedef struct node *Node;
struct node{
int K1;
int Max_K1,Min_K1;
int K2;
int Left;
int Right;
}*Dacare;
int n;
int Root();
int DLR(int);
int LDR(int);
int main()
{
int i = 0;
scanf("%d",&n);
Decare = (Node)malloc(sizeof(struct node)*n);
for(i=0;i<n;i++)
{
scanf("%d",&Decare[i].K1);
scanf("%d",&Decare[i].K2);
scanf("%d",&Decare[i].Left);
scanf("%d",&Decare[i].Right);
Decare[i].Min_K1 = ERRORMin;
Decare[i].Max_K1 = ERRORMax;
}
int head = Root();
int Result = DLR(head);
if(Result)
{
Result = LRD(head);
}
if(Result)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return 0;
}
int Root(){
int *temp = (int*)malloc(sizeof(int)*n);
int i = 0;
for(i=0;i<n;i++)
{
temp[i] = 0 ;
}
for(i=0;i<n;i++)
{
temp[Decare[i].Left] = 1;
temp[Decare[i].Right] = 1;
}
for(i=0;i<n;i++)
{
if(!temp[i])
{
return i;
}
}
return -1;
}
int DLR(int K) //k2 minnum heap
{
if(K == -1)
{
return 1;
}
if(Decare[K].Left != -1)
{
if(Decare[Decare[K].Left].K2 < Decare[K].K2)
{
return 0;
}
}
if(Decare[K].Right != -1)
{
if(Decare[Decare[K].Right].K2 < Decare[K].K2)
{
return 0;
}
}
if(DLR(Decare[K].Left == 0))
{
return 0;
}
if(DLR(Decare[K].Right) == 0)
{
return 0;
}
return 1;
}
int LDR(int K) //K1 binary search tree
{
if(K == -1)
{
return 1;
}
if(LDR(Decare[K].Left) == 0)
{
return 0;
}
if(Decare[K].Left == -1)
{
Decare[K].Max_K1 = Decare[K].K1;
}
else
{
Decare[K].Max_K1 = Decare[Decare[K].Right].Max_K1;
}
int temp=K;
if(Decare[temp].Left != -1)
if(Decare[temp.K1] < Decare[Decare[temp].Left].Max_K1)
{
return 0;
}
if(Decare[temp].Right != -1)
{
}
if(Decare[temp].K1 > Decare[Decare[temp].Right].Min_K1)
{
return 0;
}
return 1;
}