please make a pretty JAVA GUI for this code this is RED BLACK TREE and i Finishe
ID: 3804676 • 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
----------------------------------------
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;
}
}
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication45;
/**
*
* @author ttechnocraft
*/
public class UI extends javax.swing.JFrame {
/**
* Creates new form UI
*/
public UI() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(0, 0, 0));
jButton1.setIcon(new javax.swing.ImageIcon("C:\Users\ttechnocraft\Pictures\pepsi.png")); // NOI18N
jButton1.setText("Pepsi ");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setText("Total:");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jButton2.setIcon(new javax.swing.ImageIcon("C:\Users\ttechnocraft\Pictures\coke.png")); // NOI18N
jButton2.setText("Coke");
jButton3.setIcon(new javax.swing.ImageIcon("C:\Users\ttechnocraft\Pictures\juice.png")); // NOI18N
jButton3.setText("Juice");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setIcon(new javax.swing.ImageIcon("C:\Users\ttechnocraft\Pictures\md.png")); // NOI18N
jButton4.setText("Mountain Dew");
jTextField2.setText("Total TTo Be Paid:");
jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 609, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE))))
.addGap(22, 22, 22)))
.addContainerGap(21, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(51, 51, 51)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(31, 31, 31)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(168, 168, 168)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.