-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0474e17
commit c3d1ec5
Showing
4 changed files
with
453 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# Lab-09 | ||
|
||
## Question | ||
2-3 Trees are self-balancing search trees. In this question, you have to write methods for a 2-3 tree. | ||
|
||
Queries | ||
|
||
1 x : Insert a node with key x into the tree. | ||
|
||
2 : Print the number of times a node is split (middle node pushed to parent) and the number of times nodes are merged till now, in that order, as two space separated integers. | ||
|
||
3 x : Delete the node with key x in the tree. | ||
|
||
4 : Print the special traversel of the tree, which is basically an inorder traversal, but when visiting a leaf node, you are printing the sum of all the keys in the leaf. | ||
|
||
Input Format | ||
|
||
The first line contains an integer q, where q is the number of queries that will follow. | ||
|
||
Each of the next q lines contains a query. | ||
|
||
Constraints | ||
|
||
q <= 100 | ||
|
||
Output Format | ||
|
||
2 : Print the number of times a node is split (middle node pushed to parent) and the number of times nodes are merged till now, in that order, as two space separated integers. | ||
|
||
4 : Print the special traversal of the tree. | ||
|
||
Sample Input 0 | ||
|
||
7 | ||
1 42 | ||
1 20 | ||
1 51 | ||
1 68 | ||
1 9 | ||
1 15 | ||
4 | ||
Sample Output 0 | ||
|
||
9 15 20 42 119 | ||
Explanation 0 | ||
|
||
-Insert 42: | ||
|
||
42 | ||
-Insert 20: | ||
|
||
20, 42 | ||
-Insert 51: | ||
|
||
42 | ||
|
||
| | | ||
|
||
20 51 | ||
-Insert 9 and 68: | ||
|
||
42 | ||
|
||
| | | ||
|
||
9,20 51,68 | ||
-Insert 15: | ||
|
||
15, 42 | ||
|
||
| | | | ||
|
||
9 20 51,68 | ||
Sample Input 1 | ||
|
||
7 | ||
1 42 | ||
1 20 | ||
1 51 | ||
1 68 | ||
1 9 | ||
1 15 | ||
2 | ||
Sample Output 1 | ||
|
||
2 0 | ||
Explanation 1 | ||
|
||
-Final tree: | ||
|
||
15, 42 | ||
| | | | ||
9 20 51,68 | ||
Sample Input 2 | ||
|
||
9 | ||
1 42 | ||
1 20 | ||
1 51 | ||
1 68 | ||
1 9 | ||
1 15 | ||
3 9 | ||
2 | ||
4 | ||
Sample Output 2 | ||
|
||
2 1 | ||
35 42 119 | ||
Explanation 2 | ||
|
||
Final tree: | ||
|
||
42 | ||
| | | ||
15,20 51,68 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define mod 1000000007 | ||
#define pb push_back | ||
#define mp make_pair | ||
#define PI 3.14159265358979323 | ||
#define debug(x) cout<<"Case "<<x<<": " | ||
#define For(i,n) for(long long i=0;i<n;i++) | ||
#define Frabs(i,a,b) for(long long i = a; i < b; i++) | ||
#define Frabr(i,a,b) for(long long i = a; i >=b; i--) | ||
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); | ||
typedef long long int ll; | ||
typedef long double ld; | ||
typedef unsigned long long int ull; | ||
typedef vector <int> vi; | ||
typedef vector <ll> vll; | ||
typedef pair <int, int> pii; | ||
typedef pair <ll, ll> pll; | ||
typedef vector < pii > vpii; | ||
typedef vector < pll > vpll; | ||
typedef vector <string> vs; | ||
|
||
//Handle:cyber_rajat | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
ll n,q; | ||
cin>>n>>q; | ||
ll arr[n+5]; | ||
memset(arr,-1,sizeof(arr)); | ||
while(q--) | ||
{ | ||
ll t; | ||
cin>>t; | ||
if(t==1) | ||
{ | ||
ll v; | ||
cin>>v; | ||
ll index=v%n; | ||
if(arr[index]==-1) | ||
arr[index]=v; | ||
else | ||
{ | ||
ll j=1; | ||
while(arr[index]!=-1 && arr[index]!=-2){ | ||
index=(v+j*j)%n; | ||
j++; | ||
} | ||
arr[index]=v; | ||
} | ||
} | ||
else if(t==2) | ||
{ | ||
ll v; | ||
cin>>v; | ||
ll index=v%n; | ||
bool flag=true; | ||
ll probes=0,j=1; | ||
while(1) | ||
{ | ||
if(arr[index]==v) | ||
{ | ||
probes++; | ||
break; | ||
} | ||
else if(arr[index]==-1) | ||
{ | ||
probes++; | ||
break; | ||
} | ||
else | ||
{ | ||
index=(v+j*j)%n; | ||
j++; | ||
probes++; | ||
} | ||
} | ||
cout<<probes<<endl; | ||
|
||
} | ||
else if(t==3) | ||
{ | ||
ll v; | ||
cin>>v; | ||
ll j=1; | ||
ll index=v%n; | ||
while(1) | ||
{ | ||
if(arr[index]==v) | ||
{ | ||
arr[index]=-2; | ||
break; | ||
} | ||
else if(arr[index]==-1) | ||
{ | ||
arr[index]=-1; | ||
break; | ||
} | ||
else | ||
{ | ||
index=(v+j*j)%n; | ||
j++; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
for(ll i=0;i<n;i++) | ||
{ | ||
if(arr[i]!=-1 && arr[i]!=-2) | ||
cout<<arr[i]<<" "; | ||
else | ||
cout<<"NULL"<<" "; | ||
} | ||
cout<<endl; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Lab-10 | ||
|
||
## Question-01 | ||
Construct a hash table using an array and quadratic hashing. Complete insertion, search and deletion and print operations on the table. | ||
|
||
Notes: | ||
|
||
1: Observe that, the hash function would be r = x mod SIZE, where SIZE is the size of the hash table. | ||
|
||
2: For deletion operation, just mark the position as empty, no need to shift elements. | ||
|
||
3: Assume that insertion query is only called when the array as atleast one empty space. Similarly, search and delete queries are called for keys present in the array. | ||
|
||
Input Format | ||
|
||
The first line will contain two integers n and q denoting the size of the hash table and the number of queries respectively. Next q lines will follow queries in the following format: | ||
|
||
1 x: Insert x into the hash table | ||
|
||
2 x: Search x in the table, and print the number of probes required to find x | ||
|
||
3 x: Delete x from the hash table | ||
|
||
4: Print all the elements in the table, starting from the index 0 of the array. You should print the space separated elements in the hash table, and "NULL" when no element is present at an index. | ||
|
||
Constraints | ||
|
||
0 ≤ q ≤ 100 | ||
Subtask 1 Complete the queries 1 x and 4 | ||
|
||
Subtask 2 Complete the query 2 x | ||
|
||
Subtask 3 Complete the query 3 x | ||
|
||
Output Format | ||
|
||
2 x: Search x in the table, and print the number of probes required to find x | ||
|
||
4: Print all the elements in the table | ||
|
||
Sample Input 0 | ||
|
||
7 8 | ||
1 76 | ||
1 40 | ||
1 48 | ||
1 5 | ||
1 55 | ||
4 | ||
3 5 | ||
4 | ||
Sample Output 0 | ||
|
||
48 NULL 5 55 NULL 40 76 | ||
48 NULL NULL 55 NULL 40 76 | ||
|
||
## Question-02 | ||
Harold is a kidnapper who wrote a ransom note, but now he is worried it will be traced back to him through his handwriting. He found a magazine and wants to know if he can cut out whole words from it and use them to create an untraceable replica of his ransom note. The words in his note are case-sensitive and he must use only whole words available in the magazine. He cannot use substrings or concatenation to create the words he needs. | ||
|
||
Given the words in the magazine and the words in the ransom note, print Yes if he can replicate his ransom note exactly using whole words from the magazine; otherwise, print No. | ||
|
||
For example, the note is "Attack at dawn". The magazine contains only "attack at dawn". The magazine has all the right words, but there's a case mismatch. The answer is therefore No. | ||
|
||
Function Description Complete the checkMagazine function in the editor below. It must print Yes if the note can be formed using the magazine, or No. | ||
|
||
checkMagazine has the following parameters: | ||
|
||
magazine: an array of strings, each a word in the magazine | ||
note: an array of strings, each a word in the ransom note | ||
Input Format | ||
|
||
The first line contains two space-separated integers, m and n, the numbers of words in the magazine and the note. The second line contains m space-separated strings, each magazine[i]. The third line contains n space-separated strings, each note[i]. | ||
|
||
Constraints | ||
|
||
1 ≤ m,n ≤ 30000 | ||
1 ≤ |magazine[i]|, |note[i]| ≤ 5 | ||
Each word consists of English alphanumeric letters (i.e., a to z and A to Z). | ||
Output Format | ||
|
||
Print Yes if he can use the magazine to create an untraceable replica of his ransom note. Otherwise, print No. | ||
|
||
Sample Input 0 | ||
|
||
6 4 | ||
give me one grand today night | ||
give one grand today | ||
Sample Output 0 | ||
|
||
Yes | ||
Sample Input 1 | ||
|
||
6 5 | ||
two times three is not four | ||
two times two is four | ||
Sample Output 1 | ||
|
||
No | ||
Explanation 1 | ||
|
||
'two' occurs only once in the magazine. | ||
|
||
Sample Input 2 | ||
|
||
7 4 | ||
Ive got a lovely bunch of coconuts | ||
ive got coconuts | ||
Sample Output 2 | ||
|
||
No | ||
Explanation 2 | ||
|
||
Harold's magazine is missing the word ive. |
Oops, something went wrong.