nampa
nampa

Reputation: 1

Why won't my binary tree display what's being inserted?

I want to know why my display() method is printing the base case even after inserting items onto my binary tree. I'm trying to compare last names of students. All I want is for my tree to display so I can confirm that the insert() works.

Here's my Node.java file:

class Node  {
   Student data;
   Node left;
   Node right;

public Node(Student data) {
    this.data = data;
    left = null;
    right = null;
  }
}

Here's my BinaryTree.java file:

public class BinaryTree {
    public Node root;

public void insert(Student s) {
    root.left = new Node(s);
    root.right = new Node(s);
    root = insert(s,root);
}

private Node insert(Student s, Node t) {
    if(t == null) {
        t = new Node(s);
        return t;
    }
    else { 
        if(s.getLastName().compareTo(t.data.getLastName()) < 0) {
            t.left = new Node(s);
            t.left = insert(s,t.left);
            return t.left;
        } else if(s.getLastName().compareTo(t.data.getLastName()) > 0) {
            t.right = new Node(s);
            t.right = insert(s,t.right);         
            return t.right;
        }
    }
    return t;
}

public void display(Node root)  { 
    if(root == null) { 
        System.out.println("Nothing found.");
    } else if(root != null) {
        display(root.right);
        System.out.println(root.data);
        display(root.left);
    }
  }
}

Here's my Student.java file:

public class Student {

private String firstName;
    private String lastName;
    private String id;

public Student(String first, String last, String Identification) {
    firstName = first;
    lastName = last;
    id = Identification;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public boolean equals(String studentId) {
    return id.equals(studentId);    
  }
}

Here's my Main.java file:

public class Main {
  public static void main(String[] args) {
    Student student = new Student("hi", "bye", "testing");
    Student student2 = new Student("out", "some", "names");

    BinaryTree bt = new BinaryTree();
    bt.insert(student);
    bt.insert(student2);
    bt.display(bt.root);
   }
} 

Here's my output in the console:

Nothing found.

Upvotes: 0

Views: 160

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191701

I'd say focus on the insert logic since that's wrong anyway.

You don't need a display method to see the tree. Fire up the debugger, and check the left and right Node object references

1) You're immediately going to hit a nullpointerexception on an empty tree
2) You only need to assign the root on an empty tree, not set every node you can reach to the same student node

For example, it's simply this

public class BinaryTree {
    public Node root;

    public void insert(Student s) {
        root = insert(s,root);
    }

    private Node insert(Student s, Node t) {
        if (root == null) return new Node(s);

        if (...) // compare names 

Then, if you look here, you overwrite anytime you make a new Node.

    if(s.getLastName().compareTo(t.data.getLastName()) < 0) {
        t.left = new Node(s);  // this is useless 
        t.left = insert(s,t.left);
        return t.left;

You need to check when you're at a leaf node, then return accordingly. And you need to return the current Node back up the recursion, not the child.

    if(s.getLastName().compareTo(t.data.getLastName()) < 0) {
        if (t left == null) {
            t.left = new Node(s);
       } else {
            t.left = insert(s,t);  // insert using t as the new root 
       } 
       return t;  // return t with its new child 

Do the same for the right side

If that's not working, try writing out your algorithm for a small 3 node tree, then for a 4-7 node balanced tree

Upvotes: 1

Related Questions