Can you make a pretty GUI for this one please ----------------------------------
ID: 3804491 • Letter: C
Question
Can you make a pretty GUI for this one please
------------------------------------------------------------------
----------------------------------------
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<Node> stack = new Stack<Node>();
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<Node> nodes = new Stack<Node>();
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<Node> s = new Stack <Node>();
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
import java.text.*;
/** This category implements a Hidden Markov Model, as well as
the Baum-Welch algorithmic program for coaching HMMs.
@author Holger Wunsch (wunsch@sfs.nphil.uni-tuebingen.de)
*/
public category HMM variety of states */
public int numStates;
/** size of output vocabulary */
public int sigmaSize;
/** initial state chances */
public double pi[];
/** transition chances */
public double a[][];
/** emission chances */
public double b[][];
/** initializes associate HMM.
@param numStates range of states
@param sigmaSize size of output vocabulary
*/
public HMM(int numStates, int sigmaSize)
/** implementation of the Baum-Welch algorithmic program for HMMs.
@param o the coaching set
@param steps the quantity of steps
*/
public void train(int[] o, int steps) {
int T = o.length;
double[][] fwd;
double[][] bwd;
double pi1[] = new double[numStates];
double a1[][] = new double[numStates][numStates];
double b1[][] = new double[numStates][sigmaSize];
for (int s = 0; s < steps; s++) {
/* calculation of Forward- und Backward Variables from the
current model */
fwd = forwardProc(o);
bwd = backwardProc(o);
/* re-estimation of initial state chances */
for (int i = 0; i < numStates; i++)
pi1[i] = gamma(i, 0, o, fwd, bwd);
/* re-estimation of transition chances */
for (int i = 0; i < numStates; i++)
a1[i][j] = divide(num, denom);
}
}
/* re-estimation of emission chances */
for (int i = 0; i < numStates; i++) one : 0);
denom += g;
}
b1[i][k] = divide(num, denom);
}
}
pi = pi1;
a = a1;
b = b1;
}
}
/** calculation of Forward-Variables f(i,t) for state i at time
t for output sequence O with the present HMM parameters
@param o the output sequence O
@return associate array f(i,t) over states and times, containing
the Forward-variables.
*/
public double[][] forwardProc(int[] o) format (time 0) */
for (int i = 0; i < numStates; i++)
fwd[i][0] = pi[i] * b[i][o[0]];
/* induction */
for (int t = 0; t <= T-2; t++)
}
come back fwd;
}
/** calculation of Backward-Variables b(i,t) for state i at time
t for output sequence O with the present HMM parameters
@param o the output sequence O
@return associate array b(i,t) over states and times, containing
the Backward-Variables.
*/
public double[][] backwardProc(int[] o) low-level formatting (time 0) */
for (int i = 0; i < numStates; i++)
bwd[i][T-1] = 1;
/* induction */
for (int t = T - 2; t >= 0; t--)
}
come back bwd;
}
/** calculation of chance P(X_t = s_i, X_t+1 = s_j | O, m).
@param t time t
@param i the quantity of state s_i
@param j the quantity of state s_j
@param o associate output sequence o
@param fwd the Forward-Variables for o
@param bwd the Backward-Variables for o
@return P
*/
public double p(int t, int i, int j, int[] o, double[][] fwd, double[][] bwd) {
double num;
if (t == o.length - 1)
num = fwd[i][t] * a[i][j];
else
num = fwd[i][t] * a[i][j] * b[j][o[t+1]] * bwd[j][t+1];
double denom = 0;
for (int k = 0; k < numStates; k++)
denom += (fwd[k][t] * bwd[k][t]);
come back divide(num, denom);
}
/** computes gamma(i, t) */
public double gamma(int i, int t, int[] o, double[][] fwd, double[][] bwd) {
double num = fwd[i][t] * bwd[i][t];
double denom = 0;
for (int j = 0; j < numStates; j++)
denom += fwd[j][t] * bwd[j][t];
come back divide(num, denom);
}
/** prints all the parameters of associate HMM */
public void print()
import java.text.*;
/** This category implements a Hidden Markov Model, as well as
the Baum-Welch algorithmic program for coaching HMMs.
@author Holger Wunsch (wunsch@sfs.nphil.uni-tuebingen.de)
*/
public category HMM variety of states */
public int numStates;
/** size of output vocabulary */
public int sigmaSize;
/** initial state chances */
public double pi[];
/** transition chances */
public double a[][];
/** emission chances */
public double b[][];
/** initializes associate HMM.
@param numStates range of states
@param sigmaSize size of output vocabulary
*/
public HMM(int numStates, int sigmaSize)
/** implementation of the Baum-Welch algorithmic program for HMMs.
@param o the coaching set
@param steps the quantity of steps
*/
public void train(int[] o, int steps) {
int T = o.length;
double[][] fwd;
double[][] bwd;
double pi1[] = new double[numStates];
double a1[][] = new double[numStates][numStates];
double b1[][] = new double[numStates][sigmaSize];
for (int s = 0; s < steps; s++) {
/* calculation of Forward- und Backward Variables from the
current model */
fwd = forwardProc(o);
bwd = backwardProc(o);
/* re-estimation of initial state chances */
for (int i = 0; i < numStates; i++)
pi1[i] = gamma(i, 0, o, fwd, bwd);
/* re-estimation of transition chances */
for (int i = 0; i < numStates; i++)
a1[i][j] = divide(num, denom);
}
}
/* re-estimation of emission chances */
for (int i = 0; i < numStates; i++) one : 0);
denom += g;
}
b1[i][k] = divide(num, denom);
}
}
pi = pi1;
a = a1;
b = b1;
}
}
/** calculation of Forward-Variables f(i,t) for state i at time
t for output sequence O with the present HMM parameters
@param o the output sequence O
@return associate array f(i,t) over states and times, containing
the Forward-variables.
*/
public double[][] forwardProc(int[] o) format (time 0) */
for (int i = 0; i < numStates; i++)
fwd[i][0] = pi[i] * b[i][o[0]];
/* induction */
for (int t = 0; t <= T-2; t++)
}
come back fwd;
}
/** calculation of Backward-Variables b(i,t) for state i at time
t for output sequence O with the present HMM parameters
@param o the output sequence O
@return associate array b(i,t) over states and times, containing
the Backward-Variables.
*/
public double[][] backwardProc(int[] o) low-level formatting (time 0) */
for (int i = 0; i < numStates; i++)
bwd[i][T-1] = 1;
/* induction */
for (int t = T - 2; t >= 0; t--)
}
come back bwd;
}
/** calculation of chance P(X_t = s_i, X_t+1 = s_j | O, m).
@param t time t
@param i the quantity of state s_i
@param j the quantity of state s_j
@param o associate output sequence o
@param fwd the Forward-Variables for o
@param bwd the Backward-Variables for o
@return P
*/
public double p(int t, int i, int j, int[] o, double[][] fwd, double[][] bwd) {
double num;
if (t == o.length - 1)
num = fwd[i][t] * a[i][j];
else
num = fwd[i][t] * a[i][j] * b[j][o[t+1]] * bwd[j][t+1];
double denom = 0;
for (int k = 0; k < numStates; k++)
denom += (fwd[k][t] * bwd[k][t]);
come back divide(num, denom);
}
/** computes gamma(i, t) */
public double gamma(int i, int t, int[] o, double[][] fwd, double[][] bwd) {
double num = fwd[i][t] * bwd[i][t];
double denom = 0;
for (int j = 0; j < numStates; j++)
denom += fwd[j][t] * bwd[j][t];
come back divide(num, denom);
}
/** prints all the parameters of associate HMM */
public void print()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.