please make a pretty JAVA GUI for this code this is RED BLACK TREE and i Finishe
ID: 3804764 • Letter: P
Question
please make a pretty JAVA GUI for this code
this is RED BLACK TREE and i Finished code already
jus need a JAVA GUI for this code ... if poosible make it pretty to look thanks and
please make own GUI code base on my code thanks
ex:
(GUI only have to show RBTree)
----------------------------------------
RBTree.java
import java.util.Stack;
public class RBTree{
private Node current;
private Node parent;
private Node grandparent;
private Node header;
private Node great;
private static Node nil;
// set the nil null cuz java cannot directly use nil
// set the int color for RED & BLACK
private static final int RED = 0;
private static final int BLACK = 1;
// static initializer for nil
static{
nil = new Node(0);
nil.left = nil;
nil.right = nil;
}
// constructor
public RBTree(int neglnf){
header = new Node(neglnf);
header.left = nil;
header.right = nil;
}
// This function check if the tree is empty or not
public boolean isEmpty(){
return header.right == nil;
}
// this function make tree empty
public void makeEmpty(){
header.right = nil;
}
// this function insert element(node)
public void insert(int keyvalue){
current = parent = grandparent = header;
nil.element = keyvalue;
while(current.element != keyvalue){
great = grandparent;
grandparent = parent;
parent= current;
current = keyvalue < current.element ? current.left : current.right;
// next step, check children = RED
if(current.left.color == RED && current.right.color == RED){
handleRotate(keyvalue);
}
}
// fail insertion if present already
if(current != nil){
return;
}
current = new Node(keyvalue, nil, nil);
// next linked with parent
if(keyvalue < parent.element){
parent.left = current;
}
else{
parent.right = current;
}
handleRotate(keyvalue);
}
// this function do rotation ( if i can - -)
private void handleRotate(int keyvalue){
// color flip
// property : insert red Node with 2 nil children
current.color = RED;
current.left.color = BLACK;
current.right.color = BLACK;
if(parent.color == RED){
//rotate
grandparent.color = RED;
if(keyvalue < grandparent.element != keyvalue < parent.element){
parent = checkRotate(keyvalue, grandparent); /////////
}
current = checkRotate(keyvalue, great);
current.color = BLACK;
}
// this part make sure Root is always BLACK
header.right.color = BLACK;
}
// this function check rotation
private Node checkRotate(int keyvalue, Node parent){
if(keyvalue < parent.element){
return parent.left = keyvalue < parent.left.element ?
rotateLeftChild(parent.left) : rotateRightChild(parent.right);
}
else{
return parent.right = keyvalue < parent.right.element ?
rotateLeftChild(parent.right) : rotateRightChild(parent.right);
}
}
// this function do binary tree left rotate
private Node rotateLeftChild(Node k2){
Node k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
return k1;
}
// this function do binart tree right rotate
private Node rotateRightChild(Node k1){
Node k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
return k2;
}
// this function count tree nodes
public int countNodes(){
return countNodes(header.right);
}
// +1 if its not nil node
// check left, right
// and return total
private int countNodes(Node n){
if(n == nil){
return 0;
}
else{
int i = 1;
i += countNodes(n.left);
i += countNodes(n.right);
return i;
}
}
// this function search element
public boolean search(int value){
return search(header.right, value);
}
// bigger then go left
// smaller then go right
// return found
private boolean search(Node n, int value){
boolean found = false;
while(( n != nil) && !found){
int nvalue = n.element;
if(value < nvalue){
n = n.left;
}
else if( value > nvalue){
n = n.right;
}
else{
found = true;
break;
}
}
return found;
}
/////////////////////////////////////////////////////////////////////
/////////////////////// Traversal part ////////////////////////////
public void inorder(){
inorder(header.right);
}
private void inorder(Node n){
if( n != nil){
Stack stack = new Stack();
Node node = n;
while(node != nil){
stack.push(node);
node = node.left;
}
while(stack.size()>0){
// visit the top node
node = stack.pop();
char c = 'B';
if(n.color == 0){
c = 'R';
}
System.out.println(n.element + " " + c + " ");
if(node.right != nil){
node = node.right;
// visit the next left node
while(node != nil){
stack.push(node);
node = node.left;
}
}
}
}
}
//inorder(n.left);
//char c = 'B';
//if(n.color == 0){
// c = 'R';
// if (n != nil)
// --------------- adjust later -----------------
//System.out.println(n.element + " " + c + " ");
//inorder(n.right);
public void preorder(){
preorder(header.right);
}
private void preorder(Node n){
//if( n != nil){
//char c = 'B';
//if( n.color == 0){
// c = 'R';
//}
//System.out.println(n.element + " " + c + " ");
//preorder(n.right);
//}
Stack nodes = new Stack();
nodes.push(n);
while(!nodes.isEmpty()){
Node node = nodes.pop();
char c = 'B';
if(node.color == 0){
c = 'R';
}
System.out.println(node.element + " " + c + " ");
if(current.right != nil){
nodes.push(current.right);
}
if(current.left != nil){
nodes.push(current.left);
}
}
}
public void postorder(){
postorder(header.right);
}
private void postorder(Node n){
//if( n != nil){
//postorder(n.left);
//postorder(n.right);
//char c = 'B';
//if(n.color == 0){
// c = 'R';
//}
//System.out.println(n.element + " " + c + " ");
//}
Node current = n;
Node previous = n;
Stack s = new Stack ();
if( n != nil){
s.push(n);
while(!s.isEmpty()){
current = s.peek();
if(current == previous || current == previous.right){
// traversing bottom
if(current.left != nil){
s.push(current.left);
}
else if(current.right != nil){
s.push(current.right);
}
if(current.left == nil && current.right == nil){
Node node = s.pop();
char c = 'B';
if(node.color == 0){
c = 'R';
}
System.out.println(node.element + " "+ c + "");
}
}
else if(previous == current.left){
//traversing up from left side
if(current.right != nil){
s.push(current.right);
}
else if(current.right == nil){
Node node = s.pop();
char c = 'B';
if(node.color == 0){
c = 'R';
}
System.out.println(node.element + " " + c + " ");
}
}
else if(previous == current.right){
// traversing up from right side
Node node = s.pop();
char c = 'B';
if(node.color == 0){
c = 'R';
}
System.out.println(node.element + " "+ c + " ");
}
previous = current;
}
}
}
}
-------------------------------
RBTreeMain.java
public class RBTreeMain {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
// Creating Object
RBTree rbt = new RBTree(Integer.MIN_VALUE);
System.out.println(" Showing the RED BLACK TREE" );
char ch;
// switch showing option
do{
System.out.println(" RBTree Operations ");
System.out.println(" 1. insert ");
System.out.println(" 2. search ");
System.out.println(" 3. count nodes ");
System.out.println(" 4. check empty ");
System.out.println(" 5. clear tree ");
int choice = scan.nextInt();
switch(choice){
case 1:
System.out.println("Enter Integer ");
rbt.insert(scan.nextInt());
break;
case 2:
System.out.println("Enter integer to search ");
System.out.println(" Result : " + rbt.search(scan.nextInt()));
break;
case 3:
System.out.println("Total Nodes: " + rbt.countNodes());
break;
case 4:
System.out.println("Empty state: " + rbt.isEmpty());
break;
case 5:
System.out.println(" Tree Cleared ");
rbt.makeEmpty();
break;
default:
System.out.println(" WRONG ENTRY ");
break;
}
// show Traversal
System.out.println(" inorder ");
rbt.inorder();
System.out.println(" postorder ");
rbt.postorder();
System.out.println(" preorder ");
rbt.preorder();
System.out.println(" Continue ? (y/n) ");
ch = scan.next().charAt(0);
}while(ch == 'Y' || ch == 'y');
}
}
----------------------------
Node.java
public class Node{
Node left, right;
int element;
//private final int RED = 0;
//private final int BLACK = 1;
//this part souhld set on RBTree class
int color;
// Constructor
public Node(int node){
// this.element = node;
// well, people using lots on code in this way
this(node, null, null);
}
public Node(int node, Node lt, Node rt){
left = lt;
right = rt;
element = node;
color = 1;
}
}
13 17 11 25 15 NIL NIL NIL NIL NIL 22 NIL NIL NIL NIL NIL NILExplanation / Answer
import java.util.*;
class RedBlackNode
{
RedBlackNode value1, value2;
int name;
int indication;
public RedBlackNode(int thename)
{
this( thename, null, null );
}
public RedBlackNode(int thename, RedBlackNode lt, RedBlackNode rt)
{
value1 = lt;
value2 = rt;
name = thename;
indication = 1;
}
}
class RBTree
{
private RedBlackNode current;
private RedBlackNode parent;
private RedBlackNode grand;
private RedBlackNode great;
private RedBlackNode header;
private static RedBlackNode nullNode;
static
{
nullNode = new RedBlackNode(0);
nullNode.value1 = nullNode;
nullNode.value2 = nullNode;
}
static final int BLACK = 1;
static final int RED = 0;
public RBTree(int negInf)
{
header = new RedBlackNode(negInf);
header.value1 = nullNode;
header.value2 = nullNode;
}
public boolean isEmpty()
{
return header.value2 == nullNode;
}
public void makeEmpty()
{
header.value2 = nullNode;
}
public void insert(int item )
{
current = parent = grand = header;
nullNode.name = item;
while (current.name != item)
{
great = grand;
grand = parent;
parent = current;
current = item < current.name ? current.value1 : current.value2;
if (current.value1.indication == RED && current.value2.indication == RED)
handleReorient( item );
}
if (current != nullNode)
return;
current = new RedBlackNode(item, nullNode, nullNode);
if (item < parent.name)
parent.value1 = current;
else
parent.value2 = current;
handleReorient( item );
}
private void handleReorient(int item)
{
current.indication = RED;
current.value1.indication = BLACK;
current.value2.indication = BLACK;
if (parent.indication == RED)
{
grand.indication = RED;
if (item < grand.name != item < parent.name)
parent = rotate( item, grand );
current = rotate(item, great );
current.indication = BLACK;
}
header.value2.indication = BLACK;
}
private RedBlackNode rotate(int item, RedBlackNode parent)
{
if(item < parent.name)
return parent.value1 = item < parent.value1.name ? rotateWithvalue1Child(parent.value1) : rotateWithvalue2Child(parent.value1) ;
else
return parent.value2 = item < parent.value2.name ? rotateWithvalue1Child(parent.value2) : rotateWithvalue2Child(parent.value2);
}
private RedBlackNode rotateWithvalue1Child(RedBlackNode k2)
{
RedBlackNode k1 = k2.value1;
k2.value1 = k1.value2;
k1.value2 = k2;
return k1;
}
private RedBlackNode rotateWithvalue2Child(RedBlackNode k1)
{
RedBlackNode k2 = k1.value2;
k1.value2 = k2.value1;
k2.value1 = k1;
return k2;
}
public int countNodes()
{
return countNodes(header.value2);
}
private int countNodes(RedBlackNode r)
{
if (r == nullNode)
return 0;
else
{
int l = 1;
l += countNodes(r.value1);
l += countNodes(r.value2);
return l;
}
}
public boolean search(int val)
{
return search(header.value2, val);
}
private boolean search(RedBlackNode r, int val)
{
boolean found = false;
while ((r != nullNode) && !found)
{
int rval = r.name;
if (val < rval)
r = r.value1;
else if (val > rval)
r = r.value2;
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
public void inorder()
{
inorder(header.value2);
}
private void inorder(RedBlackNode r)
{
if (r != nullNode)
{
inorder(r.value1);
char c = 'B';
if (r.indication == 0)
c = 'R';
System.out.print(r.name +""+c+" ");
inorder(r.value2);
}
}
public void preorder()
{
preorder(header.value2);
}
private void preorder(RedBlackNode r)
{
if (r != nullNode)
{
char c = 'B';
if (r.indication == 0)
c = 'R';
System.out.print(r.name +""+c+" ");
preorder(r.value1);
preorder(r.value2);
}
}
public void postorder()
{
postorder(header.value2);
}
private void postorder(RedBlackNode r)
{
if (r != nullNode)
{
postorder(r.value1);
postorder(r.value2);
char c = 'B';
if (r.indication == 0)
c = 'R';
System.out.print(r.name +""+c+" ");
}
}
}
public class RedBlackTreeTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
RBTree rbt = new RBTree(Integer.MIN_VALUE);
System.out.println("Red Black Tree Test ");
char ch;
do
{
System.out.println(" Red Black Tree Operations ");
System.out.println("1. insert ");
System.out.println("2. search");
System.out.println("3. count nodes");
System.out.println("4. check empty");
System.out.println("5. clear tree");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer name to insert");
rbt.insert( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer name to search");
System.out.println("Search result : "+ rbt.search( scan.nextInt() ));
break;
case 3 :
System.out.println("Nodes = "+ rbt.countNodes());
break;
case 4 :
System.out.println("Empty status = "+ rbt.isEmpty());
break;
case 5 :
System.out.println(" Tree Cleared");
rbt.makeEmpty();
break;
default :
System.out.println("Wrong Entry ");
break;
}
System.out.print(" Post order : ");
rbt.postorder();
System.out.print(" Pre order : ");
rbt.preorder();
System.out.print(" In order : ");
rbt.inorder();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.