-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPLT.java
More file actions
162 lines (138 loc) · 2.49 KB
/
Copy pathSPLT.java
File metadata and controls
162 lines (138 loc) · 2.49 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
package SPLT_A4;
public class SPLT implements SPLT_Interface {
private BST_Node root;
int size;
public SPLT(){
size = 0;
root = null;
}
@Override
//used for testing, please leave as is
public BST_Node getRoot(){
return root;
}
private void reRoot() {
while(root != null && root.parent != null)
{
root = root.parent ;
}
}
@Override
public void insert(String s) { //It was me
// TODO Auto-generated method stub
if (empty()) {
root = new BST_Node(s);
size += 1;
reRoot();
return;
}
if (contains(s)) {
return;
} else {
size += 1;
root.insertNode(s);
reRoot();
return;
}
}
@Override
public void remove(String s) { //DIO
// TODO Auto-generated method stub
if (!contains(s)) {
return;
} else {
if (root.data.equals(s) && size == 1) {
root = null;
size -= 1;
return;
}
else if(root.left == null) {
BST_Node right = root.right;
right.parent = null;
root = right;
size -= 1;
return;
}
else {
BST_Node left = root.left;
BST_Node right = root.right;
if(right != null) right.parent = null;
left.parent = null;
left = left.findMax();
left.right = right;
if(right != null) right.parent = left;
root = left;
size -= 1;
return;
}
}
}
@Override
public String findMin() {
// TODO Auto-generated method stub
if (empty()) {
return null;
} else {
String rtn = root.findMin().data;
reRoot();
return rtn;
}
}
@Override
public String findMax() {
// TODO Auto-generated method stub
if (empty()) {
return null;
} else {
String rtn = root.findMax().data;
reRoot();
return rtn;
}
}
@Override
public boolean empty() {
// TODO Auto-generated method stub
if (size == 0) {
return true;
} else {
return false;
}
}
@Override
public boolean contains(String s) {
// TODO Auto-generated method stub
if (empty()) {
return false;
}
boolean rtn = root.containsNode(s);
reRoot();
return rtn;
}
@Override
public int size() {
// TODO Auto-generated method stub
return size;
}
@Override
public int height() {
// TODO Auto-generated method stub
if (empty()) {
return -1;
} else {
return root.getHeight();
}
}
}
/*
* package SPLT_A4;
public class SPLT implements SPLT_Interface{
private BST_Node root;
private int size;
public SPLT() {
this.size = 0;
}
public BST_Node getRoot() { //please keep this in here! I need your root node to test your tree!
return root;
}
}
*/