import java.util.Objects; class CovidVariant { String Name; String Unique_code; CovidVariant left, right; public CovidVariant(String name, String code) { this.Name = name; this.Unique_code = code; left = right = null; } } class VariantCollection { private boolean compare_code(String code, String code_2) { int year = Integer.parseInt(code.substring(0, 2)); int month = Integer.parseInt(code.substring(2, 4)); int date = Integer.parseInt(code.substring(4, 6)); int year_2 = Integer.parseInt(code_2.substring(0, 2)); int month_2 = Integer.parseInt(code_2.substring(2, 4)); int date_2 = Integer.parseInt(code_2.substring(4, 6)); if (year == year_2) { if (month == month_2) { if (date > date_2) { return true; } else { return false; } } else if (month > month_2) { return true; } else { return false; } } else if (year > year_2) { return true; } else { return false; } } CovidVariant root; VariantCollection() { root = null; } void addVariant(CovidVariant cv) { root = add(root, cv); } CovidVariant add(CovidVariant root, CovidVariant cv) { if (root == null) { root = new CovidVariant(cv.Name, cv.Unique_code); return root; } if (!compare_code(cv.Unique_code, root.Unique_code)) { root.left = add(root.left, cv); } else if (compare_code(cv.Unique_code, root.Unique_code)) { root.right = add(root.right, cv); } return root; } void inorder() { inorderRec(root); } // Inorder Traversal void inorderRec(CovidVariant root) { if (root != null) { inorderRec(root.left); System.out.print("Name: " + root.Name + " code: " + root.Unique_code + "\n"); inorderRec(root.right); } } void search(String code) { search_Recursive(root, code); } CovidVariant search_Recursive(CovidVariant root, String code) { if (root != null && Objects.equals(root.Unique_code, code)) { System.out.println(root.Name); return root; } assert root != null; if (!compare_code(code, root.Unique_code)) { root.left = search_Recursive(root.left, code); } else if (compare_code(code, root.Unique_code)) { root.right = search_Recursive(root.right, code); } return null; } } public class Problem1 { public static void main(String[] args) { VariantCollection col = new VariantCollection(); col.addVariant(new CovidVariant("Alpha", "210201A")); col.addVariant(new CovidVariant("Delta", "210311D")); col.addVariant(new CovidVariant("Beta", "210311A")); col.addVariant(new CovidVariant("Omicron", "211120D")); col.inorder(); System.out.println("\n"); col.search("210311A"); // return the Beta variant // col.previous("211120D"); // return the Delta variant } } Here is my full code that create BTS TREE HAVE FUNCTION INORDER(), SEARCH(), AND PREVIOUS() I already done inorder(), and Search() PLS, HELP ME CREATE FUNCTION PREVIOUS() in BTS TREE
import java.util.Objects;
class CovidVariant {
String Name;
String Unique_code;
CovidVariant left, right;
public CovidVariant(String name, String code) {
this.Name = name;
this.Unique_code = code;
left = right = null;
}
}
class VariantCollection {
private boolean compare_code(String code, String code_2) {
int year = Integer.parseInt(code.substring(0, 2));
int month = Integer.parseInt(code.substring(2, 4));
int date = Integer.parseInt(code.substring(4, 6));
int year_2 = Integer.parseInt(code_2.substring(0, 2));
int month_2 = Integer.parseInt(code_2.substring(2, 4));
int date_2 = Integer.parseInt(code_2.substring(4, 6));
if (year == year_2) {
if (month == month_2) {
if (date > date_2) {
return true;
} else {
return false;
}
} else if (month > month_2) {
return true;
} else {
return false;
}
} else if (year > year_2) {
return true;
} else {
return false;
}
}
CovidVariant root;
VariantCollection() {
root = null;
}
void addVariant(CovidVariant cv) {
root = add(root, cv);
}
CovidVariant add(CovidVariant root, CovidVariant cv) {
if (root == null) {
root = new CovidVariant(cv.Name, cv.Unique_code);
return root;
}
if (!compare_code(cv.Unique_code, root.Unique_code)) {
root.left = add(root.left, cv);
} else if (compare_code(cv.Unique_code, root.Unique_code)) {
root.right = add(root.right, cv);
}
return root;
}
void inorder() {
inorderRec(root);
}
// Inorder Traversal
void inorderRec(CovidVariant root) {
if (root != null) {
inorderRec(root.left);
System.out.print("Name: " + root.Name + " code: " + root.Unique_code + "\n");
inorderRec(root.right);
}
}
void search(String code) {
search_Recursive(root, code);
}
CovidVariant search_Recursive(CovidVariant root, String code) {
if (root != null && Objects.equals(root.Unique_code, code)) {
System.out.println(root.Name);
return root;
}
assert root != null;
if (!compare_code(code, root.Unique_code)) {
root.left = search_Recursive(root.left, code);
} else if (compare_code(code, root.Unique_code)) {
root.right = search_Recursive(root.right, code);
}
return null;
}
}
public class Problem1 {
public static void main(String[] args) {
VariantCollection col = new VariantCollection();
col.addVariant(new CovidVariant("Alpha", "210201A"));
col.addVariant(new CovidVariant("Delta", "210311D"));
col.addVariant(new CovidVariant("Beta", "210311A"));
col.addVariant(new CovidVariant("Omicron", "211120D"));
col.inorder();
System.out.println("\n");
col.search("210311A"); // return the Beta variant
// col.previous("211120D"); // return the Delta variant
}
}
Here is my full code that create BTS TREE HAVE FUNCTION INORDER(), SEARCH(), AND PREVIOUS()
I already done inorder(), and Search()
PLS, HELP ME CREATE FUNCTION PREVIOUS() in BTS TREE
ALL CODE I HAVE BEEN SHOWN ABOVE.

Step by step
Solved in 3 steps with 1 images









