How can this be fixed to read, sort, and count the binary tree correctly? Also how I could use a delimiter for my overloaded constructor, or as another method, or in my main method?
This is the current Input File I need to use:
A
/ \
B C
\ / \
D E F
/ \
G H
/ / \
I J K
The current file that represents the above tree is TreeFile.txt with a sequence that represents the missing nodes:
A
B
C
NULL
D
E
F
NULL
NULL
G
NULL
NULL
H
NULL
NULL
NULL
NULL
NULL
NULL
I
NULL
NULL
NULL
NULL
NULL
J
K
NULL
NULL
NULL
NULL
But If I use this text file then it will through off the algorithm I use in my code bellow to create and show the tree for the pre-order, in-order, post-order, and level-order, node counting, leaf counting, number of nodes on the left and right. What can be corrected in my code so that the "NULL" is not displayed back and sorting and counting shows correct too?
My Code that takes the input and makes the tree with a sample of pre-order and count left nodes:
import java.util.Scanner;
public class BinaryTree<E> {
private class NodeClass<E> {
private NodeClass<E> left;
private NodeClass<E> right;
private E value;
public NodeClass(E value) {
left = null;
right = null;
this.value = value;
}
}
private NodeClass<E> root;
// confusion here
public BinaryTree(Scanner scan) throws Exception {
NodeClass<E> currentNode = root;
NodeClass<E> nextNode = null;
while (scan.hasNextLine()) {
String value = (String) scan.nextLine().trim();
if (value.length() == 0)
continue;
if (root == null) {
root = new NodeClass(value);
} else {
currentNode = root;
nextNode = currentNode;
while (nextNode != null) {
if (nextNode.left == null) {
insertingLeft(nextNode, (E) value);
break;
} else if (nextNode.right == null) {
insertingRight(nextNode, (E) value);
break;
}
if (nextNode == currentNode) {
nextNode = currentNode.left;
} else if (nextNode == currentNode.left) {
nextNode = currentNode.right;
} else if (nextNode == currentNode.right) {
nextNode = currentNode.left.left;
currentNode = currentNode.left;
}
}
}
}
}
public void insertingRight(NodeClass<E> Node, E element) {
if (Node.right == null) {
Node.right = new NodeClass<E>(element);
} else {
insertingRight(Node.right, element);
}
}
public void insertingLeft(NodeClass<E> Node, E element) {
if (Node.left == null) {
Node.left = new NodeClass<E>(element);
} else {
insertingLeft(Node.left, element);
}
}
public BinaryTree() {
root = null;
}
public String PreOrder() {
StringBuilder builder = new StringBuilder();
PreOrder(builder, root);
return builder.toString();
}
private void PreOrder(StringBuilder builder, NodeClass<E> node) {
builder.append(" " + node.value.toString());
if (node.left != null) {
PreOrder(builder, node.left);
}
if (node.right != null) {
PreOrder(builder, node.right);
}
}
public int leftCount() {
return leftCount(root);
}
private int leftCount(NodeClass<E> node) {
int count = 0;
if (node.left != null && node.right == null) {
count++;
count += leftCount(node.left);
}
if (node.right != null) {
count += leftCount(node.right);
}
return count;
}
}
My main driver class:
import java.io.File;
import java.util.Scanner;
public class testBinaryTree {
public static void main(String[] args) {
try {
Scanner in = new Scanner(new File("TreeFile.txt"));
BinaryTree<String> bt = new BinaryTree<String>(in);
System.out.print("Pre Order: " + bt.PreOrder() + "\n");
System.out.println("Left Count: " + bt.leftCount());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Which displays back:
Pre Order: A B NULL NULL NULL NULL I NULL NULL NULL NULL NULL NULL NULL J K NULL NULL NULL H NULL NULL NULL NULL NULL D G NULL C E F
Left Count: 0
Your "NULL" nodes are displayed because you actually create nodes containing the text "NULL" and don't take them into account when printing the preorder output.
You can either filter those nodes with the text "NULL" when printing the preorder and ignore them (the same as you ignore nodes that are actually null).
Or, which I think is better, not insert them from the beginning. This, however, requires you to keep track of the insertion position. I think the easiest way to do that is to put the whole list of nodes in an array first and then create the tree from it. There are formulas to calculate the index of the left or right children of a node (2*i+1 for the left son, 2*i+2 for the right son) that help you find the right children.
So first just read all the strings into an array String[] nodes and then call BuildTreeFromArray(0, nodes) on it to build the tree:
NodeClass<E> BuildTreeFromArray(int index, String[] nodes) {
if (index >= nodes.length) {
return null;
} else {
NodeClass<E> node = new NodeClass<E>(nodes[index]);
node.left = BuildTreeFromArray(2 * index + 1, nodes);
node.right = BuildTreeFromArray(2 * index + 2, nodes);
return node;
}
}
I strongly recommend that you use a better tree representation in the input file. For example, use a list of triples, each linking a parent to its children. Then order doesn't matter (except make the first node root), and you can ignore all the non-existent nodes:
A B C
B - D
D G -
G I -
C E F
E - H
H J K
Then it's easy to build the tree using a dictionary of nodes:
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class TreeBuilder {
Map<String, Node> valToNode = new HashMap<>();
static class Node {
String key;
Node left, right;
Node(String key) { this.key = key; }
void printTree() {
System.out.print(key);
if (left != null || right != null) {
System.out.print("(");
print(left);
System.out.print(", ");
print(right);
System.out.print(")");
}
}
static void print(Node node) {
if (node == null)
System.out.print("-");
else
node.printTree();
}
}
Node buildTree(String filePath) throws IOException {
Node root = null;
for (String line : Files.readAllLines(
Paths.get(filePath), Charset.forName("UTF-8"))) {
String[] vals = line.split("\\s+");
Node parent = lookupOrCreateNode(vals[0]);
parent.left = lookupOrCreateNode(vals[1]);
parent.right = lookupOrCreateNode(vals[2]);
if (root == null) root = parent;
}
return root;
}
Node lookupOrCreateNode(String key) {
if ("-".equals(key)) return null;
Node node = valToNode.get(key);
if (node == null) {
node = new Node(key);
valToNode.put(key, node);
}
return node;
}
void run() {
try {
Node tree = buildTree("src/hacking/data.txt");
tree.printTree();
System.out.println();
} catch (IOException ex) {
System.err.println("input file not found");
}
}
public static void main(String[] args) {
new TreeBuilder().run();
}
}
Output:
A(B(-, D(G(I, -), -)), C(E(-, H(J, K)), F))
NB: The java is short, but it skips error checking, data hiding, etc.
Related
hi im new to codings and i have to print my binary search tree in a 2d model but this codes only print the orders of number in order(left-root-right) such as when i insert 10, 9, 11, 8, it will print inorder (left root right) = 8,9,10,11. what method or codes should i add to create a 2d tree here. sorry idk how to properly put the codes here just look at it like it is only a one code only.
class binarySearchTree {
class Node {
int key;
Node left, right;
int data;
public Node(int data){
key = data;
left = right = null;
}
}
// BST root node
Node root;
// Constructor for BST =>initial empty tree
binarySearchTree(){
root = null;
}
//delete a node from BST
void deleteKey(int key) {
root = delete_Recursive(root, key);
}
//recursive delete function
Node delete_Recursive(Node root, int key) {
//tree is empty
if (root == null) return root;
//traverse the tree
if (key < root.key) //traverse left subtree
root.left = delete_Recursive(root.left, key);
else if (key > root.key) //traverse right subtree
root.right = delete_Recursive(root.right, key);
else {
// node contains only one child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
// node has two children;
//get inorder successor (min value in the right subtree)
root.key = minValue(root.right);
// Delete the inorder successor
root.right = delete_Recursive(root.right, root.key);
}
return root;
}
int minValue(Node root) {
//initially minval = root
int minval = root.key;
//find minval
while (root.left != null) {
minval = root.left.key;
root = root.left;
}
return minval;
}
// insert a node in BST
void insert(int key) {
root = insert_Recursive(root, key);
}
//recursive insert function
Node insert_Recursive(Node root, int key) {
//tree is empty
if (root == null) {
root = new Node(key);
return root;
}
//traverse the tree
if (key < root.key) //insert in the left subtree
root.left = insert_Recursive(root.left, key);
else if (key > root.key) //insert in the right subtree
root.right = insert_Recursive(root.right, key);
// return pointer
return root;
}
void inorder() {
inorder_Recursive(root);
}
// recursively traverse the BST
void inorder_Recursive(Node root) {
if (root != null) {
inorder_Recursive(root.left);
System.out.print(root.key + " x ");
inorder_Recursive(root.right);
}
}
//PostOrder Traversal - Left:Right:rootNode (LRn)
void postOrder(Node node) {
if (node == null)
return;
// first traverse left subtree recursively
postOrder(node.left);
// then traverse right subtree recursively
postOrder(node.right);
// now process root node
System.out.print(node.key + " ");
}
// InOrder Traversal - Left:rootNode:Right (LnR)
void inOrder(Node node) {
if (node == null)
return;
//first traverse left subtree recursively
inOrder(node.left);
//then go for root node
System.out.print(node.key + " ");
//next traverse right subtree recursively
inOrder(node.right);
}
//PreOrder Traversal - rootNode:Left:Right (nLR)
void preOrder(Node node) {
if (node == null)
return;
//first print root node first
System.out.print(node.key + " ");
// then traverse left subtree recursively
preOrder(node.left);
// next traverse right subtree recursively
preOrder(node.right);
}
// Wrappers for recursive functions
void postOrder_traversal() {
postOrder(root); }
void inOrder_traversal() {
inOrder(root); }
void preOrder_traversal() {
preOrder(root); }
}
here i found this codes in stackoverflow, i want te output like this, i can use this but i dont know how can i make this as user input for the data and make it insert the integer into a tree not this manually inserted of the integer. thankyou very much to whoever put effort to understand my question and my situation as newbie.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BTreePrinterTest {
private static Node<Integer> test2() {
Node<Integer> root = new Node<Integer>(2);
Node<Integer> n11 = new Node<Integer>(3);
Node<Integer> n12 = new Node<Integer>(5);
Node<Integer> n21 = new Node<Integer>(2);
Node<Integer> n22 = new Node<Integer>(6);
Node<Integer> n23 = new Node<Integer>(9);
Node<Integer> n31 = new Node<Integer>(5);
root.left = n11;
root.right = n12;
n11.left = n21;
n11.right = n22;
n12.left = n23;
n12.right = n31;
return root;
}
public static void main(String[] args) {
BTreePrinter.printNode(test2());
}
}
class Node<T extends Comparable<?>> {
Node<T> left, right;
T data;
public Node(T data) {
this.data = data;
}
}
class BTreePrinter {
public static <T extends Comparable<?>> void printNode(Node<T> root) {
int maxLevel = BTreePrinter.maxLevel(root);
printNodeInternal(Collections.singletonList(root), 1, maxLevel);
}
private static <T extends Comparable<?>> void printNodeInternal(List<Node<T>> nodes, int level, int maxLevel) {
if (nodes.isEmpty() || BTreePrinter.isAllElementsNull(nodes))
return;
int floor = maxLevel - level;
int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0)));
int firstSpaces = (int) Math.pow(2, (floor)) - 1;
int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1;
BTreePrinter.printWhitespaces(firstSpaces);
List<Node<T>> newNodes = new ArrayList<Node<T>>();
for (Node<T> node : nodes) {
if (node != null) {
System.out.print(node.data);
newNodes.add(node.left);
newNodes.add(node.right);
} else {
newNodes.add(null);
newNodes.add(null);
System.out.print(" ");
}
BTreePrinter.printWhitespaces(betweenSpaces);
}
System.out.println("");
for (int i = 1; i <= endgeLines; i++) {
for (int j = 0; j < nodes.size(); j++) {
BTreePrinter.printWhitespaces(firstSpaces - i);
if (nodes.get(j) == null) {
BTreePrinter.printWhitespaces(endgeLines + endgeLines + i + 1);
continue;
}
if (nodes.get(j).left != null)
System.out.print("/");
else
BTreePrinter.printWhitespaces(1);
BTreePrinter.printWhitespaces(i + i - 1);
if (nodes.get(j).right != null)
System.out.print("\\");
else
BTreePrinter.printWhitespaces(1);
BTreePrinter.printWhitespaces(endgeLines + endgeLines - i);
}
System.out.println("");
}
printNodeInternal(newNodes, level + 1, maxLevel);
}
private static void printWhitespaces(int count) {
for (int i = 0; i < count; i++)
System.out.print(" ");
}
private static <T extends Comparable<?>> int maxLevel(Node<T> node) {
if (node == null)
return 0;
return Math.max(BTreePrinter.maxLevel(node.left), BTreePrinter.maxLevel(node.right)) + 1;
}
private static <T> boolean isAllElementsNull(List<T> list) {
for (Object object : list) {
if (object != null)
return false;
}
return true;
}
}
btw im learning this by my own, i tried merging the two codes but it gives me error i cant fix it.
I should have not made the whole exercise for you, so please try to understand the code. Tell me if something is not clear for you.
public static void main(String[] args) throws IOException {
System.out.println("Write your input");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String lines = br.readLine();
binarySearchTree b = new binarySearchTree();
b.input(lines);
b.print();
}
These functions go to binarySearchTree.
protected void printRecursive(Node node, int depth) {
System.out.println("");
for(int i = 0; i<depth; i++) {
System.out.print(" ");
}
System.out.print(node.key);
if(node.left != null) {
printRecursive(node.left, depth + 1);
}
if(node.right != null) {
printRecursive(node.right, depth + 1);
}
}
public void input(String s) throws IOException {
String[] strs = s.trim().split("\\s+");
for (int i = 0; i < strs.length; i++) {
insert(Integer.parseInt(strs[i]));
}
}
Also i used this answer in my code.
I have this method that uses recursion to find a node that holds a specified String in a binary tree. The issue is that it returns null, when it should return the node that holds the specified name, and I'm not sure why.
Here's the method:
public Node getNode(Node currentNode, String name) {
Node retrieved = null;
if (currentNode.getName().equals(name)) { retrieved = currentNode; }
else
{
if (currentNode.right != null) {
getNode(currentNode.right, name);
}
if (currentNode.left != null) {
getNode(currentNode.left, name);
}
}
return retrieved;
}
Any insight into what may be the problem would be appreciated.
You need to capture the return value of your two recursive calls. Otherwise you're doing recursion "for nothing" and throwing away the result of the recursion.
public Node getNode(Node currentNode, String name){
Node retrieved = null;
if (currentNode.getName().equals(name)) { retrieved = currentNode; }
else
{
if (currentNode.right != null){
retrieved = getNode(currentNode.right, name);
}
if (retrieved == null && currentNode.left != null){
retrieved = getNode(currentNode.left, name);
}
}
return retrieved;
}
The following solution is arguably better style because you leave null checks for a base case. Notice how you no longer need to check currentNode.right != null or currentNode.left != null, as they're covered by the base case after one more recursive step.
public static Node getNode(Node currentNode, String name){
// Base case: currentNode is null, nothing left to search
if (currentNode == null) {
return null;
}
Node retrieved = null;
if (currentNode.getName().equals(name)) {
retrieved = currentNode;
} else {
// Try to search right subtree
retrieved = getNode(currentNode.right, name);
// If not found in right subtree, then search left subtree
if (retrieved == null){
retrieved = getNode(currentNode.left, name);
}
}
return retrieved;
}
Solution
getNode(currentNode.right, name);
You call the getNode(...) method but you don't do anything with it.
A better solution
If you are willing to use googles Guava (must-have for every project in my opinion) and java 8, you can do the following:
public static final Traverser<Node> TREE_TRAVERSER =
Traverser.forTree((SuccessorsFunction<Node>) node ->
Stream.of(node.right, node.left)
.filter(Objects::nonNull)
.collect(Collectors.toList()));
And then call it where ever you want to traverse the tree:
for (Node n : TREE_TRAVERSER.depthFirstPreOrder(root)) {
if (n.getName().equals("foo")) {
// todo: do stuff with node foo
}
}
The java 8 way of traversing the tree would then be:
Iterable<Node> nodes = TREE_TRAVERSER.depthFirstPreOrder(root);
Optional<Node> result = StreamSupport.stream(nodes.spliterator(), false)
.filter(n -> n.getName().equals("foo")) // find node with name "foo"
.findAny(); // we assume there is <= 1 node, so we take any.
// node.isPresent() to check if you found a Node and result.get() to get the Node
How does this work?
Well, Guava has this nice class called a Traverser<N>. You just give it one parameter, which is the SuccessorsFunction<N>. It takes any object N and returns a Iterable<? extends N>, which are the child nodes.
We use Streams to do this. First we create a Stream of the two child nodes. We then filter them to only have a Stream of nonNull Nodes and collect them in a List (since the SuccessorsFunction<Node> wants to return a Iterable<Node>).
This Traverser<N> only has to be created once, so we made it public static final. You can now choose an iteration order. We chose depthFirstPreOrder, which returns an Iterable<N> we can iterate over
If you haven't heard of Streams before, I would recommend this turorial.
I would suggest taking tail recursions into account, since performance-wise this is a major factor :
public static Node getNode(Node currentNode, String name){
// Base case: currentNode is null, nothing left to search
if (currentNode == null) {
return null;
}
Node retrieved = null;
if (currentNode.name.equals(name)) {
return currentNode;
} else {
// Tail recursions
if(currentNode.left == null) {
return getNode(currentNode.right, name);
}
else if(currentNode.right == null) {
return getNode(currentNode.left, name);
}
// Non Tail recursion
else {
retrieved = getNode(currentNode.left, name);
// If not found in left subtree, then search right subtree
if (retrieved == null){
retrieved = getNode(currentNode.right, name);
}
}
}
return retrieved;
}
Attached is the full code which was executed on an online compiler:
public class MyClass {
static class Node {
public String name;
public Node left;
public Node right;
Node(String name) {
this.name = name;
right = null;
left = null;
}
#Override
public String toString() {
return "name = " + name + " hasLeft = " + (left != null) + " hasRight = " + (right != null);
}
}
static class Tree {
Node root;
public Node getRoot() {
return root;
}
private Node addRecursive(Node current, String value) {
if (current == null) {
return new Node(value);
}
if (value.compareTo(current.name) < 0) {
current.left = addRecursive(current.left, value);
} else if (value.compareTo(current.name) > 0) {
current.right = addRecursive(current.right, value);
} else {
// value already exists
return current;
}
return current;
}
public Tree add(String value) {
root = addRecursive(root, value);
return this;
}
public void traverseInOrder(Node node) {
if (node != null) {
traverseInOrder(node.left);
System.out.print(" " + node.name);
traverseInOrder(node.right);
}
}
public void traverseInOrder() {
traverseInOrder(root);
System.out.println("");
}
}
public static void main(String args[]) {
Tree tree = new Tree();
tree.add("a").add("ab").add("bbb").add("cc").add("zolko").add("polip").traverseInOrder();
Node found = getNode(tree.getRoot(),"vv");
System.out.println(found);
found = getNode(tree.getRoot(),"ab");
System.out.println(found);
found = getNode(tree.getRoot(),"polip");
System.out.println(found);
found = getNode(tree.getRoot(),"java");
System.out.println(found);
found = getNode(tree.getRoot(),"zolko");
System.out.println(found);
}
public static Node getNode(Node currentNode, String name){
// Base case: currentNode is null, nothing left to search
if (currentNode == null) {
return null;
}
Node retrieved = null;
if (currentNode.name.equals(name)) {
return currentNode;
} else {
// Tail recursions
if(currentNode.left == null) {
return getNode(currentNode.right, name);
}
else if(currentNode.right == null) {
return getNode(currentNode.left, name);
}
// Non Tail recursion
else {
retrieved = getNode(currentNode.left, name);
// If not found in left subtree, then search right subtree
if (retrieved == null){
retrieved = getNode(currentNode.right, name);
}
}
}
return retrieved;
}
}
And the outputs of the main method execution:
a ab bbb cc polip zolko
null
name = ab hasLeft = false hasRight = true
name = polip hasLeft = false hasRight = false
null
name = zolko hasLeft = true hasRight = false
public class doubleLinkedList {
class Node {
String value;
Node prev;
Node next;
Node(String val, Node p, Node n) {
value = val;
prev = p;
next = n;
}
Node(String val) {
value = val;
prev = null;
next = null;
}
}
Node first;
Node last;
public doubleLinkedList() {
first = null;
last = null;
}
public boolean isEmpty() {
if (first == null)
return true;
else
return false;
}
/**The size method returns the length of the linked list
* #return the number of element in the linked list
*/
public int size() {
int count = 0;
Node traverse = first;
while (traverse != null) {
count++;
traverse = traverse.next;
}
return count;
}
public void add(String element) {
if (isEmpty()) {
first = new Node(element);
last = first;
} else {
Node p = first;
Node elementTobeAdded;
while (((p.value).compareTo(element)) > 0 && p.next != null) {
p = p.next;
}
if (p.next != null) {
elementTobeAdded = new Node(element, p, p.next);
p.next.prev = elementTobeAdded;
p = elementTobeAdded.prev;
} else {
elementTobeAdded = new Node(element, p, null);
p.next = elementTobeAdded;
elementTobeAdded.next = null;
last = elementTobeAdded;
}
}
}
public void printForward() {
Node printNode = first;
while (printNode != null) {
System.out.print(printNode.value + ", ");
printNode = printNode.next;
}
}
}
public class test {
public static void main(String[] args) {
doubleLinkedList car = new doubleLinkedList();
car.add("Jeep");
car.add("benz");
car.add("Honda");
car.add("Lexus");
car.add("BMW");
car.printForward();
}
}
My add method is trying to add nodes to a list in alphabetical order. My printForward method prints out each element in the list.
In my main method, it prints out "Jeep, benz, Honda, BMW,", which is not in alphabetical order.
Change the not empty case for your add method from this
Node p = first;
Node elementTobeAdded;
while(((p.value).compareTo(element)) > 0 && p.next != null)
{
p = p.next;
}
if(p.next != null)
{
elementTobeAdded = new Node(element,p,p.next);
p.next.prev = elementTobeAdded;
p = elementTobeAdded.prev;
}
else
{
elementTobeAdded = new Node(element, p, null);
p.next = elementTobeAdded;
elementTobeAdded.next = null;
last = elementTobeAdded;
}
to this:
Node p = first;
while (p.value.compareTo(element) < 0 && p.next != null) {
p = p.next;
}
if (p.value.compareTo(element) > 0) {
Node toAdd = new Node(element, p.prev, p);
p.prev = toAdd;
if (toAdd.prev != null) {
toAdd.prev.next = toAdd;
}else {
first = toAdd;
}
}else {
Node toAdd = new Node(element, p, p.next);
p.next = toAdd;
if (toAdd.next != null) {
toAdd.next.prev = toAdd;
}else {
last = toAdd;
}
}
There were many errors here. The biggest one was that you never checked for the case where the new element should be inserted at the beginning of the list. A new element was always inserted after the first element even if it should have come first.
Note that "benz" comes at the end because the String.compareTo method treats capitals as coming before lower case letters.
It is not an a linked list... You wrote some sort of Queue (with optional possibility to make it Dequeue).
About your question - you have an error in your 'add' method - at least you don't check if it is necessary to move head forward. It is possible that you have another bugs, but it is too hard to read such styled sources (please fix your question formatting)...
So i have 3 methods 1 that adds a node to the binary tree using the traverseAdd method, and another method which finds the location of where a value would be placed within the tree based on its parent node. I would like to eliminate the traverseAdd method and use the findLocation method within the add method to add the new value to the BST.
public void add(int val) {
/*Adds a new node to the binary tree after traversing the tree and figuring out where it belongs*/
Node nodeObjToAdd = new Node(val);
if(root == null){
//if node root is not null root = new node value
root = nodeObjToAdd;
}
Node nodeTraversed = root;
traverseAdd(nodeTraversed, nodeObjToAdd);
}
private void traverseAdd(Node node, Node nodeObjToAdd){
/*Traverses tree and finds a place to add the node to be added by comparing values of the left child and right child of the
* focus node*/
if(nodeObjToAdd.value < node.value){
if(node.leftChild == null){
node.leftChild = nodeObjToAdd;
}
else {
//if the val < the root.value set int he constructor
traverseAdd(node.leftChild, nodeObjToAdd);
}
}
else if(nodeObjToAdd.value > node.value) {
if (node.rightChild == null) {
node.rightChild = nodeObjToAdd;
} else {
traverseAdd(node.rightChild, nodeObjToAdd);
}
}
}
public Node findNodeLocation(Node rootNode, int val) {
/*returns where a the Node after which the value would be added.*/
if(val < rootNode.value && rootNode.leftChild != null){
return rootNode.leftChild;
}
if(val >= rootNode.value && rootNode.rightChild != null){
return rootNode.rightChild;
}
else
return this.root;
}
public void add(int val) {
if (root == null) {
root = new Node(val);
}
Node cur = root;
Node next = null;
while (true) {
next = findNodeLocation(cur, val);
if (next != cur) {
cur = next;
} else {
break;
}
}
if (val < cur.value) {
cur.leftChild = new Node(val);
} else {
cur.rightChild = new Node(val);
}
}
I think this should work
I'm trying to write code for a binary search tree, the first method I'm working on is the add (insert) method. The root seems to insert properly, but I'm getting null pointer exception when adding the second node. I'll indicate the exact problem spot in my code with comments.
If you can see how to fix the bugs, or let me know if my overall logic is flawed it would be incredibly helpful.-- I will mention that this is for school, so I'm not looking to make a really impressive model...most of my layout choices simply reflect the way we've been working in class. Also, method names were selected by the teacher and should stay the same. Feel free to edit the formatting, had a little trouble.
BINARY TREE CLASS
public class BinarySearchTree
{
private static Node root;
public BinarySearchTree()
{
root = null;
}
public static void Add (Node newNode)
{
Node k = root;
if (root == null)//-----------------IF TREE IS EMPTY -----------------
{
root = newNode;
}
else // -------TREE IS NOT EMPTY --------
{
if (newNode.value > k.value) //-------NEW NODE IS LARGER THAN ROOT---------
{
boolean searching = true;
while(searching) // SEARCH UNTIL K HAS A LARGER VALUE
{ //***CODE FAILS HERE****
if(k.value > newNode.value || k == null)
{
searching = false;
}
else {k = k.rightChild; }
}
if ( k == null) { k = newNode;}
else if (k.leftChild == null){ k.leftChild = newNode;}
else
{
Node temp = k.leftChild;
k.leftChild = newNode;
newNode = k.leftChild;
if(temp.value > newNode.value )
{
newNode.rightChild = temp;
}
else
{
newNode.leftChild = temp;
}
}
}
if (newNode.value < k.value) //-----IF NEW NODE IS SMALLER THAN ROOT---
{
boolean searching = true;
while(searching) // ----SEARCH UNTIL K HAS SMALLER VALUE
{// **** CODE WILL PROBABLY FAIL HERE TOO ***
if(k.value < newNode.value || k == null) {searching = false;}
else {k = k.leftChild;}
}
if ( k == null) { k = newNode;}
else if (k.rightChild == null){ k.rightChild = newNode;}
else
{
Node temp = k.rightChild;
k.rightChild = newNode;
newNode = k.rightChild;
if(temp.value > newNode.value )
{
newNode.rightChild = temp;
}
else
{
newNode.leftChild = temp;
}
}
}
}} // sorry having formatting issues
}
NODE CLASS
public class Node
{
int value;
Node leftChild;
Node rightChild;
public Node (int VALUE)
{
value = VALUE;
}
}
TEST APPLICATION
public class TestIT
{
public static void main(String[] args)
{
BinarySearchTree tree1 = new BinarySearchTree();
Node five = new Node(5);
Node six = new Node(6);
tree1.Add(five);
tree1.Add(six);
System.out.println("five value: " + five.value);
System.out.println("five right: " + five.rightChild.value);
}
}
The conditional statement is checked from left to right, so you need to check whether k is null before you check whether k.value > newNode.value because if k is null, then it doesn't have a value.