View the step-by-step solution to:

Doing a red black tree but i keep getting a null exception error in my addNode method.

Doing a red black tree but i keep getting a null exception error in my addNode method. Below is the code and junit tester


package project3;


public class RedBlackTree<Key extends Comparable<Key>> {

private static RedBlackTree.Node<String> root;

private static RedBlackTree.Node<String> nil;


public static class Node<Key extends Comparable<Key>> { // changed to static


Key key;

Node<String> parent;

Node<String> leftChild;

Node<String> rightChild;

boolean isRed;

int color;


public Node(Key data) {

this.key = data;

leftChild = null;

rightChild = null;

}


public int compareTo(Node<Key> n) { // this < that <0

return key.compareTo(n.key); // this > that >0

}


public boolean isLeaf() {

if (this.equals(root) && this.leftChild == null && this.rightChild == null)

return true;

if (this.equals(root))

return false;

if (this.leftChild == null && this.rightChild == null) {

return true;

}

return false;

}

}


public boolean isLeaf(RedBlackTree.Node<String> n) {

if (n.equals(root) && n.leftChild == null && n.rightChild == null)

return true;

if (n.equals(root))

return false;

if (n.leftChild == null && n.rightChild == null) {

return true;

}

return false;

}


public interface Visitor<Key extends Comparable<Key>> {

/**

* This method is called at each node.

* @param n

*      the visited node

*/

void visit(Node<Key> n);

}


public void visit(Node<Key> n) {

System.out.println(n.key);

}


public void printTree() { // preorder: visit, go left, go right

RedBlackTree.Node<String> currentNode = root;

printTree(currentNode);

}


public void printTree(RedBlackTree.Node<String> node) {

System.out.print(node.key);

if (node.isLeaf()) {

return;

}

printTree(node.leftChild);

printTree(node.rightChild);

}


// place a new node in the RB tree with data the parameter and color it red.

public void addNode(String data) { // this < that <0. this > that >0

RedBlackTree.Node<String> b=nil;

RedBlackTree.Node<String> a=root;

while(a!=nil){

b=a;

if(data.compareTo(a.key)<0){

a=a.leftChild;

}

else {

a = a.rightChild;

}

}

RedBlackTree.Node<String> c = lookup(data);

c.parent=b;

if(b==nil){

root=c;

}

else if(c.key.compareTo(b.key)<0){

b.leftChild=c;}

else{

b.rightChild=c;

c.leftChild=nil;

c.rightChild=nil;

c.color=0;

fixTree(c);

}

}


public void insert(String data) {

RedBlackTree.Node<String> a = new RedBlackTree.Node<String>(data);

addNode(a.key);

}


public RedBlackTree.Node<String> lookup(String k) {

return lookupnode(root,k);

}

private RedBlackTree.Node<String> lookupnode(RedBlackTree.Node<String> currNode, String string){

if(currNode==nil||string.compareTo(currNode.key)==0)

{

return currNode;

}

else if(string.compareTo(currNode.key)<0)

{

return lookupnode(currNode.leftChild, string);

}

else if(string.compareTo(currNode.key)>0)

{

return lookupnode(currNode.rightChild, string);

}

return null;

}


public RedBlackTree.Node<String> getSibling(RedBlackTree.Node<String> n) {

RedBlackTree.Node<String> p =lookup(n.key).parent;

if(p!=null){

if(p.key.compareTo(n.key)<0){

return p.leftChild;

}

else if(p.key.compareTo(n.key)>0){

return p.rightChild;

}

}

return null;

}


public RedBlackTree.Node<String> getAunt(RedBlackTree.Node<String> n) {

RedBlackTree.Node<String> gp = getGrandparent(n);

if(gp!=null){

if(gp.key.compareTo(n.key)<0){

return gp.leftChild;

}

else if(gp.key.compareTo(n.key)>0){

return gp.rightChild;

}

}

return null;

}


public RedBlackTree.Node<String> getGrandparent(RedBlackTree.Node<String> n) {

return n.parent.parent;

}


public void rotateLeft(RedBlackTree.Node<String> n) {

RedBlackTree.Node<String> y;

y = n.rightChild;

n.rightChild = y.leftChild;

if (y.leftChild != null) {

y.leftChild.parent = n;

y.parent = n.parent;

}

if (n.parent == null) {

root = y;

} else if (n == n.parent.leftChild) {

n.parent.leftChild = y;

} else

n.parent.rightChild = y;

y.leftChild = n;

n.parent = y;

//

}


public void rotateRight(RedBlackTree.Node<String> n) {

RedBlackTree.Node<String> y;

y = n.leftChild;

n.leftChild = y.rightChild;

if (y.rightChild != null){

y.rightChild.parent = n;

y.parent = n.parent;}

if (n.parent == null){

root = y;}

else if (n == n.parent.rightChild){

n.parent.rightChild = y;}

else

n.parent.leftChild = y;

y.rightChild = n;

n.parent = y;

}


public void fixTree(RedBlackTree.Node<String> current) {

int counter = 0;

while (current.parent.color==0) {

if (current.parent == current.parent.parent.leftChild) {

RedBlackTree.Node<String> y = current.parent.parent.rightChild;

if (y.color==0) {

current.parent.color = 1;

y.color = 1;

current.parent.parent.color = 0;

current = current.parent.parent;

counter += 3;

} else {

if (current == current.parent.rightChild) {

current = current.parent;

rotateLeft(current);

}

current.parent.color = 1;

current.parent.parent.color = 0;

rotateRight(current.parent.parent);

counter += 2;

}

} else {

RedBlackTree.Node<String> y = current.parent.parent.leftChild;

if (y.color==0) {

current.parent.color = 1;

y.color = 1;

current.parent.parent.color = 0;

current = current.parent.parent;


counter += 3;

} else {

if (current == current.parent.leftChild) {

current = current.parent;

rotateRight(current);

}

current.parent.color = 1;

current.parent.parent.color = 0;

rotateLeft(current.parent.parent);

counter += 2;

}

}

}

//

}


public boolean isEmpty(RedBlackTree.Node<String> n) {

if (n.key == null) {

return true;

}

return false;

}


public boolean isLeftChild(RedBlackTree.Node<String> parent, RedBlackTree.Node<String> child) {

if (child.compareTo(parent) < 0) {// child is less than parent

return true;

}

return false;

}


public void preOrderVisit(Visitor<String> v) {

preOrderVisit(root, v);

}


private static void preOrderVisit(RedBlackTree.Node<String> n, Visitor<String> v) {

if (n == null) {

return;

}

v.visit(n);

preOrderVisit(n.leftChild, v);

preOrderVisit(n.rightChild, v);

}

}




JUNIT


package project3;



import static org.junit.Assert.*;


import org.junit.Test;



public class RBTTester {


@Test

  //Test the Red Black Tree

public void test() {

RedBlackTree rbt = new RedBlackTree();

    rbt.insert("D");

    rbt.insert("B");

    rbt.insert("A");

    rbt.insert("C");

    rbt.insert("F");

    rbt.insert("E");

    rbt.insert("H");

    rbt.insert("G");

    rbt.insert("I");

    rbt.insert("J");

    assertEquals("DBACFEHGIJ", makeString(rbt));

    String str=   "Color: 1, Key:D Parent: n"+

            "Color: 1, Key:B Parent: Dn"+

            "Color: 1, Key:A Parent: Bn"+

            "Color: 1, Key:C Parent: Bn"+

            "Color: 1, Key:F Parent: Dn"+

            "Color: 1, Key:E Parent: Fn"+

            "Color: 0, Key:H Parent: Fn"+

            "Color: 1, Key:G Parent: Hn"+

            "Color: 1, Key:I Parent: Hn"+

            "Color: 0, Key:J Parent: In";

assertEquals(str, makeStringDetails(rbt));

       

  }

   

  //add tester for spell checker

   

  public static String makeString(RedBlackTree t)

  {

    class MyVisitor implements RedBlackTree.Visitor {

     String result = "";

     public void visit(RedBlackTree.Node n)

     {

       result = result + n.key;

     }

    };

    MyVisitor v = new MyVisitor();

    t.preOrderVisit(v);

    return v.result;

  }


  public static String makeStringDetails(RedBlackTree t) {

   {

       class MyVisitor implements RedBlackTree.Visitor {

        String result = "";

        public void visit(RedBlackTree.Node n)

        {

         if(!(n.key).equals(""))

         result = result +"Color: "+n.color+", Key:"+n.key+" Parent: "+n.parent.key+"n";

          

        }

       };

       MyVisitor v = new MyVisitor();

       t.preOrderVisit(v);

       return v.result;

    }

  }

 // add this in your class  

 // public static interface Visitor

 // {

 //  /**

 //   This method is called at each node.

 //   @param n the visited node

 //   */

 //  void visit(Node n);

 // }

 

  

 // public void preOrderVisit(Visitor v)

 // {

 //  preOrderVisit(root, v);

 // }

 

 

 // private static void preOrderVisit(Node n, Visitor v)

 // {

 //  if (n == null) return;

 //  v.visit(n);

 //  preOrderVisit(n.left, v);

 //  preOrderVisit(n.right, v);

 // }

   

   

 }

  

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question