This repository was archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ4.cpp
More file actions
154 lines (136 loc) · 3.57 KB
/
Copy pathQ4.cpp
File metadata and controls
154 lines (136 loc) · 3.57 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include<iostream>
#include <unordered_map>
using namespace std;
class Node{
public:
int key;
int height;
Node * left;
Node * right;
Node(int k){
height = 1;
key = k;
left = nullptr;
right = nullptr;
}
};
class AVL
{
public:
Node * root = nullptr;
int n;
unordered_map<int,int> m;
void insert(int x){
m[x] = x;
root=_insert(root, x);
}
Node *search(int x){
return _search(root,x);
}
bool exists(int x){
if (m.find(x) == m.end())
return false;
return true;
}
void inorder(){
_inorder(root);
cout<<endl;
}
int count_less_than(int x)
{
return _count_less_than(root,x);
}
private:
int height(Node * head){
if(head==nullptr) return 0;
return head->height;
}
int _count_less_than(Node *r,int x)
{
if(r == nullptr)
return 0;
int countLeft = _count_less_than(r->left, x);
int countRight = 0;
if(r->key <= x)
countRight = _count_less_than(r->right, x);
return (r->key < x ? 1 : 0) + countLeft + countRight;
}
Node *rightRotation(Node * head){
Node * new_head = head->left;
head->left = new_head->right;
new_head->right = head;
head->height = 1+max(height(head->left), height(head->right));
new_head->height = 1+max(height(new_head->left), height(new_head->right));
return new_head;
}
Node *leftRotation(Node * head){
Node * new_head = head->right;
head->right = new_head->left;
new_head->left = head;
head->height = 1+max(height(head->left), height(head->right));
new_head->height = 1+max(height(new_head->left), height(new_head->right));
return new_head;
}
void _inorder(Node * head){
if(head==nullptr) return ;
_inorder(head->left);
cout<<head->key<<" ";
_inorder(head->right);
}
Node * _insert(Node * head, int x){
if(head==nullptr){
n+=1;
Node * temp = new Node(x);
return temp;
}
if(x < head->key) head->left = _insert(head->left, x);
else if(x > head->key) head->right = _insert(head->right, x);
head->height = 1 + max(height(head->left), height(head->right));
int b = height(head->left) - height(head->right);
if(b>1){
if(x < head->left->key){
return rightRotation(head);
}else{
head->left = leftRotation(head->left);
return rightRotation(head);
}
}else if(b<-1){
if(x > head->right->key){
return leftRotation(head);
}else{
head->right = rightRotation(head->right);
return leftRotation(head);
}
}
return head;
}
Node * _search(Node * head, int x){
if(head == nullptr) return nullptr;
int k = head->key;
if(k == x) return head;
if(k > x) return _search(head->left, x);
if(k < x) return _search(head->right, x);
}
};
void run()
{
AVL t;
int q;
cin >> q;
while(q > 0)
{
q--;
int request_type,x;
cin >> request_type >> x;
if(request_type == 1)
t.insert(x);
// else if(request_type == 2)
// if (t.exists(x)) cout << "YES" << endl;
// else cout << "NO" << endl;
// else
// cout << t.count_less_than(x) << endl;
}
}
int main(){
run();
}