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:

13 17 11 25 15 NIL NIL NIL NIL NIL 22 NIL NIL NIL NIL NIL NIL

(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("\n RBTree Operations \n");

           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(" \n inorder \n ");

           rbt.inorder();

           System.out.println(" \n postorder \n ");

           rbt.postorder();

           System.out.println(" \n preorder \n ");

           rbt.preorder();

          

           System.out.println("\n Continue ? (y/n) \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;

   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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\n");        
        char ch;
    
        do  
        {
            System.out.println("\nRed Black Tree Operations\n");
            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("\nTree Cleared");
                rbt.makeEmpty();
                break;       
            default :
                System.out.println("Wrong Entry \n ");
                break;  
            }
          
            System.out.print("\nPost order : ");
            rbt.postorder();
            System.out.print("\nPre order : ");
            rbt.preorder();
            System.out.print("\nIn order : ");
            rbt.inorder();

            System.out.println("\nDo you want to continue (Type y or n) \n");
            ch = scan.next().charAt(0);                      
        } while (ch == 'Y'|| ch == 'y');             
     }
}

Add a comment
Know the answer?
Add Answer to:
please make a pretty JAVA GUI for this code this is RED BLACK TREE and i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need...

    PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need help. import java.util.LinkedList; public class TwoDTree { private TwoDTreeNode root; private int size; public TwoDTree() {    clear(); } /** * Returns true if a point is already in the tree. * Returns false otherwise. * * The traversal remains the same. Start at the root, if the tree * is not empty, and compare the x-coordinates of the point passed * in and...

  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

  • Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import...

    Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • Have to write the tree into a text file? JAVA CODE Binary search tree This is...

    Have to write the tree into a text file? JAVA CODE Binary search tree This is the tree public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left;...

  • Return a method as an expression tree Hi guys. I need to return a method as...

    Return a method as an expression tree Hi guys. I need to return a method as an expression tree, it's currently returning null. public static ExpressionTree getExpressionTree(String expression) throws Exception {       char[] charArray = expression.toCharArray(); Node root = et.constructTree(charArray); System.out.println("infix expression is"); et.inorder(root); return et; } In the above method, et needs to have a value. -- Take a look at the complete class below. Kindly assist. ; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT