Your contribution can guide someone’s learning journey. Share your
documents today.
Introduction The Red-Black Tree is a self-balancing Binary Search Tree (BST). The structure is governed by a number of rules which include; 1.Nodes are either red or black 2.The root node is always black 3.Every final/last leaf is black 4.A red node must have both of its children red 5.Simple paths for any node must have equal number of black descendant nodes How to Compile and Run the Program The solution developed for this question is presented in a single java file named; RedBlackTree.java to compile the program; ï‚·java RedBlackTree.java ï‚·javac RedBlackTree The program requests for the filename from which it will read the commands, as shown below.
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
SOURCE CODE import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.Scanner; public class RedBlackTree { //The colors will be represented by 0 and 1 private final int RED = 0; private final int BLACK = 1; private final RBNode nil = new RBNode(-1); private RBNode root = nil; /* We use a private inner class to define the NODE */ private class RBNode { int data = -1; int color = BLACK; RBNode left = nil; RBNode right = nil; RBNode parent = nil;
public RBNode(int key) { this.data = key; } /** * Constructor */ public RBNode(int key, RBNode parent, RBNode left, RBNode right, char color) { this.data = key; this.parent = parent; this.left = left; this.right = right; this.color = color; } void setColor(char color) { this.color = color; } } /** * This function prints out the Tree * inorder * @param node */ void printInorder(RBNode node) {
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
relativeNode = node.parent.parent.right; if (relativeNode != nil && relativeNode.color == RED) { node.parent.color = BLACK; relativeNode.color = BLACK; node.parent.parent.color = RED; node = node.parent.parent; continue; } if (node == node.parent.right) { //Double rotation needed node = node.parent; rotateLeft(node); } node.parent.color = BLACK; node.parent.parent.color = RED; //if the "else if" code hasn't executed, this //is a case where we only need a single rotation rotateRight(node.parent.parent); } else { relativeNode = node.parent.parent.left; if (relativeNode != nil && relativeNode.color == RED) { node.parent.color = BLACK; relativeNode.color = BLACK; node.parent.parent.color = RED; node = node.parent.parent; continue;
} if (node == node.parent.left) { //Double rotation needed node = node.parent; rotateRight(node); } node.parent.color = BLACK; node.parent.parent.color = RED; //if the "else if" code hasn't executed, this //is a case where we only need a single rotation rotateLeft(node.parent.parent); } } root.color = BLACK; } /** * The function Rotates the NODE * to the left * @param node */ void rotateLeft(RBNode node) { if (node.parent != nil) { if (node == node.parent.left) { node.parent.left = node.right; } else { node.parent.right = node.right;
} return null; } /** * This function is called by the main method * It displays the console interface where a user * can input the filename and where the output is * to be printed out. */ public void run() { //Read user Input for the filename System.out.println("Data Structure"); // input from file Scanner scan = new Scanner(System.in); System.out.print("Enter your input file name: "); String fileName = scan.nextLine(); String s = ""; // file -> string s try { InputStreamReader reader = new InputStreamReader( new FileInputStream(fileName)); BufferedReader br = new BufferedReader(reader); s = br.readLine(); } catch (Exception e) { System.out.println("Invalid file."); return; }
RBNode node; String[] select = s.split(" "); for (int i = 0; i < select.length; i++) { String tmp = select[i]; int data = Integer.parseInt(tmp.substring(0, tmp.indexOf('.'))); String opt = tmp.substring(tmp.indexOf('.') + 1, tmp.length()); System.out.println(data + " " + opt); if (opt.equals("in")) { node = new RBNode(data); if (searchNode(node, root) == null) { insert(node); } else { System.out.println("Duplicate."); // continue; } } else if (opt.equals("del")) { node = new RBNode(data); delete(node); } System.out.print("Display after this operation: "); //st.inorder(st.root); printInorder(root);
System.out.println(""); } } /** * The entry point for the program * @param args */ public static void main(String[] args) { RedBlackTree redBlack = new RedBlackTree(); redBlack.run(); } }