FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
learning-notes/algorithm/adversarial_input/bad3.cpp at master · ifding/learning-notes · GitHub
ifding
/
learning-notes
Public
Notifications
You must be signed in to change notification settings
Fork
19
Star
63
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
learning-notes
/
algorithm
/
adversarial_input
/
bad3.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
122 lines (106 loc) · 2.93 KB
Breadcrumbs
learning-notes
/
algorithm
/
adversarial_input
/
bad3.cpp
Copy path
File metadata and controls
executable file
·
122 lines (106 loc) · 2.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
iostream
>
#
include
<
algorithm
>
using
namespace
std
;
//
Generate a random number in the range 0..N-1
int
get_rand
(
int
N)
{
//
I forgot to attend the lecture where Prof Dean. explained how to generate
//
random numbers well, so hopefully this will work ok...
//
It looks like get_rand(N) always returns the same number if you call
//
it multiple times with the same value of N, but how could someone
//
possibly exploit that to make quicksort run slowly...
return
123456789
% N;
}
struct
Node
{
int
key;
int
size;
Node *left;
Node *right;
Node
(
int
k) { key = k; size =
1
; left = right =
NULL
; }
};
void
fix_size
(Node *T)
{
T->
size
=
1
;
if
(T->
left
) T->
size
+= T->
left
->
size
;
if
(T->
right
) T->
size
+= T->
right
->
size
;
}
//
prints out the inorder traversal of T (i.e., the contents of T in sorted order)
void
print_inorder
(Node *T)
{
if
(T ==
NULL
)
return
;
print_inorder
(T->
left
);
cout << T->
key
<<
"
"
;
print_inorder
(T->
right
);
}
//
Split tree T on rank r into tree L (containing ranks < r) and
//
a tree R (containing ranks >= r)
void
split
(Node *T,
int
r, Node **L, Node **R)
{
//
TBD: please fill in this function appropriately
if
(T==
NULL
) {
*L=
NULL
;
*R=
NULL
;
return
;
}
int
rank_of_root = T->
left
? T->
left
->
size
:
0
;
if
(r>rank_of_root) {
split
(T->
right
, r - rank_of_root -
1
, &T->
right
, R);
*L = T;
}
else
{
split
(T->
left
, r, L, &T->
left
);
*R = T;
}
fix_size
(T);
}
//
insert value v at rank r
Node *
insert_keep_balanced
(Node *T,
int
v,
int
r)
{
//
TBD: fill this in
if
(T==
NULL
)
return
new
Node
(v);
if
(
rand
() % (T->
size
+
1
) ==
0
) {
Node *new_root =
new
Node
(v);
split
(T, r, &new_root->
left
, &new_root->
right
);
fix_size
(new_root);
return
new_root;
}
int
rank_of_root = T->
left
? T->
left
->
size
:
0
;
if
(r > rank_of_root) T->
right
=
insert_keep_balanced
(T->
right
, v, r - rank_of_root -
1
);
else
T->
left
=
insert_keep_balanced
(T->
left
, v, r);
fix_size
(T);
return
T;
}
//
implement in a balanced BST
void
generate_adversarial
(Node **T,
int
N)
{
if
(N==
1
) {
*T =
insert_keep_balanced
(*T,
1
,
0
);
return
;
}
generate_adversarial
(T, N-
1
);
int
pivot =
get_rand
(N);
*T =
insert_keep_balanced
(*T, N, pivot);
}
int
main
(
int
argc,
char
*argv[])
{
if
(argc !=
2
) {
cout <<
"
Usage: bad1 <input size>
\n
"
;
return
0
;
}
int
N =
atoi
(argv[
1
]);
//
get first command-line argument
if
(N<
1
|| N>
100000
) {
cout <<
"
Invalid input size!
\n
"
;
return
0
;
}
//
Generate and print bad input of size N for prog1
//
(currently just generates an input of N random numbers)
cout << N <<
"
\n
"
;
//
int *A = new int[N];
//
for (int i=0; i<N; i++)
//
A[i] = rand() % 1000000;
Node *T=
NULL
;
generate_adversarial
(&T, N);
print_inorder
(T);
//
for (int i=0; i<N; i++)
//
cout << A[i] << "\n";
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL