In Java I'm trying to make a Red Black Tree through inheritance with the Binary Tree I created (as provided along with the Binary Node) yet I'm having a lot of difficulty knowing where and how to start as well as how to override and even create the Insert and Remove functions for the Red Black Tree. Code and assistance in this topic will be greatly appreciated... here is the code I'm utilizing for this project.
Binary Node I created:
public class BinNode
{
private String data;
private BinNode left;
private BinNode right;
private int height; //height field
public BinNode(){
data = "";
left = null;
right = null;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public BinNode(String d){
data = d;
left = null;
right = null;
}
public void setData(String d){
this.data = d;
}
public String getData(){
return this.data;
}
public void setLeft(BinNode l){
this.left = l;
}
public BinNode getLeft(){
return this.left;
}
public void setRight(BinNode r){
this.right = r;
}
public BinNode getRight(){
return this.right;
}
}
Binary Tree I created:
public class BinaryTree
{
private BinNode root;
private BinNode curr;
/* Function to traverse right */
public void goRight() {
this.curr = this.curr.getRight();
}
public BinaryTree()
{
root = new BinNode();
curr = new BinNode();
}
public BinNode getRoot(){
return this.root;
}
/* Function to check if tree is empty */
public boolean isEmpty()
{
return this.root == null;
}
/* Functions to insert data */
public void insert(String data)
{
this.root = insert(this.root, data);
} //done
/* Function to insert data recursively */
private BinNode insert(BinNode node, String data)
{
if (node == null)
node = new BinNode(data);
else
{
int i = node.getData().compareTo(data);
if (i < 0) {
// Go right
node.setRight(insert(node.getRight(), data));
} else if (i >= 0) {
// Go left
node.setLeft(insert(node.getLeft(), data));
}
// write this!
}
return node;
}
/* Function to count number of nodes */
public int countNodes()
{
return countNodes(this.root);
}
/* Function to count number of nodes recursively */
private int countNodes(BinNode r)
{
if (r == null) {
return 0; // If the current node is null, there are no nodes to count.
}
int leftCount = countNodes(r.getLeft()); // Count nodes in the left subtree
int rightCount = countNodes(r.getRight()); // Count nodes in the right subtree
// Add 1 to count the current node itself
return 1 + leftCount + rightCount;
}
/* Function to search for an element */
public BinNode search(String val)
{
return search(this.root, val);
}
/* Function to search for an element recursively */
private BinNode search(BinNode r, String val)
{
// Base case: If the current node is null, the value is not found.
if (r == null) {
return null;
}
int comparison = val.compareTo(r.getData());
if (comparison < 0) {
// Value is smaller, so search in the left subtree.
return search(r.getLeft(), val);
} else if (comparison > 0) {
// Value is larger, so search in the right subtree.
return search(r.getRight(), val);
} else {
// Value is equal to the current node's data, so it's found.
//this.curr = r;
return r;
}
}
/* Function for inorder traversal */
public void inorder()
{
inorder(this.root);
}
private void inorder(BinNode r)
{
if (r != null)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
/* Function for preorder traversal */
public void preorder()
{
preorder(root);
}
private void preorder(BinNode r)
{
if (r != null)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
/* Function for postorder traversal */
public void postorder()
{
postorder(root);
}
private void postorder(BinNode r)
{
if (r != null)
{
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() +" ");
}
}
// find the in-order successor of a given node
public BinNode findInorderSuccessor(BinNode node) {
BinNode current = node.getRight();
while (current.getLeft() != null) {
current = current.getLeft();
}
return current;
}
//Remove function
public void remove(String key) {
// Find the node to be removed and its parent
BinNode parent = null;
BinNode n = this.root;
while (n != null && !n.getData().equals(key)) {
parent = n;
if (key.compareTo(n.getData()) < 0) {
n = n.getLeft();
} else {
n = n.getRight();
}
}
if (n == null) {
// Key not found, nothing to remove
return;
}
// Case 1: Node with no children
if (n.getLeft() == null && n.getRight() == null) {
if (parent == null) {
// Removing the root node
this.root = null;
} else if (parent.getLeft() == n) {
parent.setLeft(null);
} else {
parent.setRight(null);
}
}
// Case 2: Node with one child
else if (n.getLeft() == null || n.getRight() == null) {
BinNode child = (n.getLeft() != null) ? n.getLeft() : n.getRight();
if (n == this.root) {
this.root = child;
} else if (parent.getLeft() == n) {
parent.setLeft(child);
} else {
parent.setRight(child);
}
}
// Case 3: Node with two children
else {
BinNode successor = findInorderSuccessor(n);
n.setData(successor.getData());
remove(successor.getData()); // Recursively remove the successor
}
}
}
Step by stepSolved in 3 steps
- B elow for each class you find a UML and description of the public interface. Implementing the public interface as described is madatory. There's freedom on how to implement these classes.The private properties and private methods are under your control.. There are multiple ways of implementing all these classes. Feel free to add private properties and methods. For each object, it's mandatory to create a header file (.h), implementation file (.cpp) and a driver. Blank files are included. The header should have the class definition in it. The implementation file should contain the implementations of the methods laid out in the header fine. And finally the Driver should test/demonstrate all the features of the class. It's best to develop the driver as the class is being written. Check each section to see if there are added additional requirements for the driver. Two test suites are included so that work can be checked. It's important to implement the drivers to test and demonstrate…arrow_forwardImplement the transaction manager class using java Explain your code in few words. I have included other classes that have relation with the parking transaction calssarrow_forwardFor beginning Java, two things 1) I got this error from my code and how and where can I fix it: Main.java:133: error: reached end of file while parsing } ^ 1 error 2) I was told my code is too long and needs to be separate files. How can I fix this into separate Java files? 3) I need to Draw the UML diagram using MS Word or PowerPoint for the classes and implement them. My original assignment is attached. Here's my code: import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.time.LocalDate; class Person { private String name; private String address; private String phoneNumber; private String emailAddress; public Person(String name, String address, String phoneNumber, String emailAddress) { this.name = name; this.address = address; this.phoneNumber = phoneNumber; this.emailAddress = emailAddress; } @Override public String toString() { return "Person: " + name + "\nAddress: " + address…arrow_forward
- I need help with the main function, please. I have all classes I need I just need the main functionarrow_forwardExplore scenarios where you might need to implement a custom Comparator for a class that already implements the Comparable interface. Provide real-world examples for context.arrow_forwardVariableReferenceNode.java, OperationNode.java, ConstantNode.java, and PatternNode.java must have their own java classes with the correct methods implemented: OperationNode: Has enum, left and Optional right members, good constructors and ToString is good VariableReferenceNode: Has name and Optional index, good constructors and ToString is good Constant & Node Pattern: Have name, good constructor and ToString is good Make sure to include the screenshot of the output of Parser.java as well.arrow_forward
- We have a parking office class for a parking management system. It has dependencies and relations with customer, car, parking lot and parking charge classes. Explain the code by stating implementation decisions, reasons behind those implementation decisions, what you assume was hard or easy to implement, and what helped. public class ParkingOffice {String name;String address;String phone;List<Customer> customers;List<Car> cars;List<ParkingLot> lots;List<ParkingCharge> charges; public ParkingOffice(){customers = new ArrayList<>();cars = new ArrayList<>();lots = new ArrayList<>();charges = new ArrayList<>();}public Customer register() {Customer cust = new Customer(name,address,phone);customers.add(cust);return cust;}public Car register(Customer c,String licence, CarType t) {Car car = new Car(c,licence,t);cars.add(car);return car;}public Customer getCustomer(String name) {for(Customer cust :…arrow_forwardhow to solve this java code?arrow_forwardIn Java, a class can only inherit from one other ------- . A class can, however, extend many ----------.arrow_forward
- What is the best way to go about this whole code with the given instructions?arrow_forwardHelp, I am making a elevator simulation using polymorphism. I am not sure to do now. Any help would be appreciated. My code is at the bottom, that's all I could think off. There are 4 types of passengers in the system:Standard: This is the most common type of passenger and has a request percentage of 70%. Standard passengers have no special requirements.VIP: This type of passenger has a request percentage of 10%. VIP passengers are given priority and are more likely to be picked up by express elevators.Freight: This type of passenger has a request percentage of 15%. Freight passengers have large items that need to be transported and are more likely to be picked up by freight elevators.Glass: This type of passenger has a request percentage of 5%. Glass passengers have fragile items that need to be transported and are more likely to be picked up by glass elevators. public abstract class Passenger { public static int passangerCounter = 0; private String passengerID; protected…arrow_forwardPlease help with this error. I have 2 seperate files pet.java and dog.java. I don't know what code I'm missing or how to resolve the errors, I've been at this for 3 days trying to trouble shoot. I wrote the code and everything passed except I keep getting below errors: Class Definition: Please click next for more feedback. If you review the UML diagram you will find that the classes are to be in separate class files. Class found, not "Dog" (case-sensitive) // pet.java public class Pet { private String petType; private String petName; private int dogSpaces; private int catSpaces; private int petAge; private int daysStay; private double amountDue; // constructor public Pet (String petType, String petName, int dogSpaces, int catSpaces, int petAge, int dogsStay, double amountDue) { } /** * @return the petType */ public String getPetType() { return petType; } /** * @return the petName */ public String getPetName() { return petName; } /** * @return the petAge */ public int…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education