Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why is my program only inserting the first student in my binary tree? When I print out the elements, only Alice is presented, why? JAVA

Why is my program only inserting the first student in my binary tree? 


When I print out the elements, only Alice is presented, why? 

 

JAVA CLASS FILES (3)

StudentBinaryTree.java (1)

package exam3;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Stack;

public class StudentBinaryTree implements Iterable{
public static class Node {
private UnfStudent studentInfo;
private Node left, right, parent;

public Node(int nNumber, String name, double gpa, Node parent) {
studentInfo = new UnfStudent(nNumber, name, gpa);
this.parent = parent;
}

public String toString() {
return studentInfo.toString();
}
}

private Node root = null;

// inserts a new node using the path made up of 0s and 1s like we did in the class
public void insert(int nNumber, String name, double gpa, String path) {
// Complete; no two nodes can have the same UnfStudent
Node currentNode = root;
Node parentNode = null;

if (currentNode == null) {
// Tree is empty, make the new node the root
root = new Node(nNumber, name, gpa, null);
return;
}

for (char c : path.toCharArray()) {
parentNode = currentNode;
if (c == '0') {
currentNode = currentNode.left;
} else if (c == '1') {
currentNode = currentNode.right;
} else {
// Handle invalid path characters
return;
}

if (currentNode == null) {
break;
}
}

if (currentNode != null) {
// Node with the given path already exists
return;
}

Node newNode = new Node(nNumber, name, gpa, parentNode);

if (parentNode == null) {
// Tree is empty, make the new node the root
root = newNode;
} else if (path.charAt(path.length() - 1) == '0') {
parentNode.left = newNode;
} else {
parentNode.right = newNode;
}
}

// returns the binary path for the node that contains 'nNumber'; return null if such a node is not present
public String findPathEncodingOf(int nNumber) {
return findPathEncodingOfHelper(root, nNumber, "");
}

private String findPathEncodingOfHelper(Node node, int nNumber, String path) {
if (node == null) {
return null;
}

if (node.studentInfo.getnNumber() == nNumber) {
return path;
}

String leftPath = findPathEncodingOfHelper(node.left, nNumber, path + "0");
String rightPath = findPathEncodingOfHelper(node.right, nNumber, path + "1");

if (leftPath != null) {
return leftPath;
} else {
return rightPath;
}
}

// returns the inorder traversal sequence, computed iteratively
public String inOrderNonRecursive() { // hint: use a stack; feel free to use java.util.Stack
// Complete
StringBuilder result = new StringBuilder();
Stack stack = new Stack<>();
Node current = root;

while (current != null || !stack.isEmpty()) {
while (current != null) {
stack.push(current);
current = current.left;
}

current = stack.pop();
result.append(current.studentInfo).append(" ");

current = current.right;
}

return result.toString().trim();
}

// returns the inorder traversal sequence, computed recursively
public String inOrderRecursive() {
StringBuilder result = new StringBuilder();
inOrderRecursiveHelper(root, result);
return result.toString().trim();
}

private void inOrderRecursiveHelper(Node node, StringBuilder result) {
if (node == null) {
return;
}

inOrderRecursiveHelper(node.left, result);
result.append(node.studentInfo).append(" ");
inOrderRecursiveHelper(node.right, result);
}

// returns the binary path from the node that contains 'nNumber' to the root
// return null if such a node is not present; hint: use the parent field
public String constructPathFromNodeToRoot(int nNumber) {
// Complete
return ""; // dummy statement; delete when you are done
}

// checks if the tree is also a BST
public boolean isBSTBasedOnnNumber() {
// Complete
return false; // dummy statement; delete when you are done
}

// checks if the tree is also a min-heap
public boolean isHeapBasedOnnNumber() {
// Complete
return false; // dummy statement; delete when you are done
}

// counts the number of students whose gpa is > 'g'
public int numberOfStudentsWhoseGPAisGreaterThan(double g) {
// Complete
return 0; // dummy statement; delete when you are done
}

// returns the name of the student whose nNumber is known
public String findTheNameOf(int nNumber) {
Node node = findNodeBynNumber(root, nNumber);
return node == null ? null : node.studentInfo.getName();
}

private Node findNodeBynNumber(Node node, int nNumber) {
if (node == null) {
return null;
}

if (node.studentInfo.getnNumber() == nNumber) {
return node;
}

Node leftResult = findNodeBynNumber(node.left, nNumber);
Node rightResult = findNodeBynNumber(node.right, nNumber);

return leftResult != null ? leftResult : rightResult;
}

// returns the number of nodes having 2 children
public int countNumberofNodesHaving2children() {
// Complete
return 0; // dummy statement; delete when you are done
}

// returns the number of leaves
public int countNumberofLeaves() {
// Complete
return 0; // dummy statement; delete when you are done
}

// checks if this current tree is same as the given tree 'B'
// 2 binary trees are same if their inorder travsersal sequence + preorder/postorder sequences are same
public boolean isSame(StudentBinaryTree B){
// Complete
return false; // dummy statement; delete when you are done
}

// returns the nNumber stored in the inorder successor; null it does not exist
public int inOrderSuccessorOf(int nNumber){
// Complete
return 0; // dummy statement; delete when you are done
}

// Complete the iterator for this class
public Iterator iterator() {
return new TreeIterator();
}

public class TreeIterator implements Iterator {
private Stack stack;

public TreeIterator() {
stack = new Stack<>();
pushLeftNodes(root);
}

private void pushLeftNodes(Node node) {
while (node != null) {
stack.push(node);
node = node.left;
}
}

public boolean hasNext() {
return !stack.isEmpty();
}

public UnfStudent next() {
if (!hasNext()) {
throw new NoSuchElementException();
}

Node node = stack.pop();
pushLeftNodes(node.right);
return node.studentInfo;
}
}
}

 

 

 

TestStudentBinaryTree.java (2)

package exam3;

public class TestStudentBinaryTree {
public static void main(String[] args){
// Create a few trees and try out the methods; no need to use files;
// just use the insert() method to create a tree
// See the slides and the binary tree HW for sample trees
StudentBinaryTree B = new StudentBinaryTree();
B.insert(123, "Alice Wonder", 2.3, "R");
B.insert(456, "Bob Schmidt", 2.4, "RL");
B.insert(789, "Cameron Ruble", 2.5, "RR");
B.insert(1011, "Dalton Wilken", 2.6, "L");

// print the inorder traversal of the tree using iterator
System.out.println("Inorder traversal: ");
for(UnfStudent student : B)
System.out.println(student);

System.out.println("nFind Path of Encoding: ");
System.out.println(B.findPathEncodingOf(789));


System.out.println("nInorder Non Recursive: ");
System.out.println(B.inOrderNonRecursive());

System.out.println("nInorder Recursive: ");
System.out.println(B.inOrderRecursive());

System.out.println("nFind Name: ");
System.out.println(B.findTheNameOf(789));
}
}


 

 

 

UnfStudent.java (3)

package exam3; // do not delete

public class UnfStudent {
private int nNumber;
private String name;
private double gpa;

public UnfStudent(int nNumber, String name, double gpa){
this.nNumber = nNumber;
this.name = name;
this.gpa = gpa;
}

public int getnNumber() { return nNumber; }
public void setnNumber(int n) { nNumber = n; }
public String getName() { return name; }
public void setName(String s) { name = s; }
public double getGPA() { return gpa; }
public void setGpa(double g) { gpa = g; }

public String toString() {
return "<" + nNumber + ", " + name + "> ";
}
}

Step by Step Solution

3.53 Rating (160 Votes )

There are 3 Steps involved in it

Step: 1

It seems that the issue with the program lies in the insertion process into the binary tree The problem may stem from the fact that the path specified ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Algorithms

Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest

3rd edition

978-0262033848

More Books

Students also viewed these Programming questions

Question

Explain the concept of latent learning.

Answered: 1 week ago