-
Notifications
You must be signed in to change notification settings - Fork 2
/
intervaltree.cpp.u1conflict
executable file
·95 lines (89 loc) · 1.64 KB
/
intervaltree.cpp.u1conflict
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
#include<iostream>
using namespace std;
struct node
{
int low,maxhigh;
node *left,*right;
};
class inttree
{
private:
node *ROOT;
public:
inttree()
{
ROOT=NULL;
}
void insert(int lo,int hi)
{
node *temp=new node;
temp->low=lo;
temp->maxhigh=hi;
temp->left=temp->right=NULL;
if(ROOT==NULL)
{
ROOT=temp;
return;
}
node *parent=NULL;
node *child=ROOT;
while(child!=NULL)
{
parent=child;
if(temp->low<child->low){
child=child->left;
if(temp->maxhigh>child->maxhigh)
child->maxhigh=temp->maxhigh;
}
else
if(temp->low>child->low)
{
if(temp->maxhigh>child->maxhigh)
child->maxhigh=temp->maxhigh;
child=child->right;
}
else
if(temp->low==child->low)
{
if(temp->maxhigh>child->maxhigh)
child=child->right;
else
child=child->left;
}
}
if(temp->low<parent->low)
parent->left=temp;
if(temp->low>parent->low)
parent->right=temp;
if(temp->low==parent->low)
{
if(temp->maxhigh>parent->maxhigh)
parent->right=temp;
else
parent->left=temp;
}
}
void search(int lo,int hi)
{
node *temp=ROOT;
while(!(lo<=temp->maxhigh && hi>=temp->low) && !(temp->low<=lo && temp->maxhigh>=hi))
{
if(temp->low > lo && temp->maxhigh>hi)
temp=temp->left;
if(temp->low < lo && temp->maxhigh>hi)
temp=temp->right;
}
if(temp->low<=lo && temp->maxhigh>=hi)
cout<<"Found! temp->low"<<temp->low<<" temp->maxhigh" <<temp->maxhigh<<endl;
else
cout<<"Not found!"<<endl;
}
};
int main()
{
inttree T;
T.insert(1,10);
T.insert(11,21);
T.search(3,35);
return 0;
}