-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacktrackingArray.java
More file actions
162 lines (148 loc) · 5.08 KB
/
BacktrackingArray.java
File metadata and controls
162 lines (148 loc) · 5.08 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
155
156
157
158
159
160
161
162
public class BacktrackingArray implements Array<Integer>, Backtrack {
private Stack stack;
private int[] arr;
private int size;
private int lastIndex;
private int ZERO = 0;
private int ONE = 1;
// Do not change the constructor's signature
public BacktrackingArray(Stack stack, int size) {
this.stack = stack;
arr = new int[size];
this.size = size;
lastIndex = 0;
}
@Override
public Integer get(int index){
if (index >= lastIndex | index < 0)
throw new IllegalArgumentException("index out of bounds.");
return (arr[index]);
}
@Override
public Integer search(int k) {
int output = -1;
boolean found = false;
for (int i = 0; i < lastIndex & !found; i++){ //regular array search
if (arr[i] == k){
output = i;
found = true;
}
}
return output;
}
@Override
public void insert(Integer x) {
if (lastIndex == size)
throw new IllegalArgumentException("underlying data structure overflow");
arr[lastIndex] = x;
lastIndex++;
stack.push(ONE); //signing for insertion operation.
}
@Override
public void delete(Integer index) {
if(index < 0 | index >= lastIndex)
throw new IllegalArgumentException("entered index out of set bounds");
stack.push(index); //will use to backtrack deleted index position
stack.push(arr[index]); //will use to backtrack deleted index value
stack.push(ZERO); //signing for delete operation
for (int i = index; i < lastIndex-1; i++){ //shift array left over index.
arr[i] = arr[i+1];
}
lastIndex--;
arr[lastIndex] = 0;
}
@Override
public Integer minimum() {
if (lastIndex == 0)
throw new IllegalArgumentException("can not return minimum for an empty set");
int output = arr[0];
for (int i = 1; i < lastIndex; i++){
if (arr[i] < output)
output = arr[i];
}
return output;
}
@Override
public Integer maximum() {
if (lastIndex == 0)
throw new IllegalArgumentException("can not return maximum for an empty set");
int output = arr[0];
for (int i = 1; i < lastIndex; i++){
if (arr[i] > output)
output = arr[i];
}
return output;
}
@Override
public Integer successor(Integer index) {
if (index < 0 | index >= lastIndex)
throw new IllegalArgumentException("index out of bounds");
int output = arr[index];
boolean flag = false;
for (int i = 0; i < lastIndex; i++){
if (!flag & arr[i] > output){ //find first value that is larger than input index value.
output=arr[i];
flag = true;
} else if (flag & arr[i] < output & arr[i] > arr[index]) { //compare values to determine successor.
output = arr[i];
}
}
if (output == arr[index]){
throw new IllegalArgumentException("successor does not exist");
}
return output;
}
@Override
public Integer predecessor(Integer index) {
if (index < 0 | index >= lastIndex)
throw new IllegalArgumentException("index out of bounds");
int output = arr[index];
boolean flag = false;
for (int i = 0; i < lastIndex; i++){
if (!flag & arr[i] < output){ //find first value that is smaller than input index value.
output=arr[i];
flag = true;
} else if (flag & arr[i] > output & arr[i] < arr[index]) { //compare values to determine predecessor.
output = arr[i];
}
}
if (output == arr[index]){
throw new IllegalArgumentException("successor does not exist");
}
return output;
}
@Override
public void backtrack() {
if (!stack.isEmpty()){ //determine if needs to backtrack delete or insert operaton
if((int)stack.pop() == 1) { //backtrack insertion
lastIndex--; //delete the last added value to the array
arr[lastIndex] = 0;
} else { //backtrack delete
int val = (int)stack.pop(); //find deleted val
int index = (int)stack.pop(); //find deleted index
for (int i = lastIndex; i>index; i--){
arr[i] = arr[i-1];
}
arr[index] = val;
lastIndex++;
}
}
}
@Override
public void retrack() {
/////////////////////////////////////
// Do not implement anything here! //
/////////////////////////////////////
}
@Override
public void print() {
String output = "";
for (int i = 0; i < lastIndex; i++){
output = output + arr[i] + " ";
}
if (output.length() > 0){
output = output.substring(0, output.length()-1);
}
System.out.println(output);
}
}