I am having an issue with my code, I am making a Binary Search Tree data structure, and when I call a function with a node's child, then assign a value to that child within the function, it doesn't update the node's child.
//*** Pseudo-ish Code ***
class BSTNode {
private BSTNode lChild;
private BSTNode rChild;
private int key;
public BSTNode(int key) {
this.lChild = null;
this.rChild = null;
this.key = key;
}
//getters and setters for each field ^
}
class BST {
private BSTNode root;
public BST() {
this.root = null;
}
public void insert(BSTNode currentNode, int value) {
BSTNode newNode = new BSTNode(value);
if (currentNode == null) {
currentNode = newNode;
if (this.root == null) {
this.root = currentNode;
}
} else {
//ignore the newNode == currentNode value statement right now
if (newNode.getValue() < currentNode.getValue()) {
insert(currentNode.getlChild(), value);
} else if (newNode.getValue() > curNode.getValue()) {
insert(curNode.getrChild(), value);
}
}
}
//getters and setters
}
I still want to figure out the code myself, but I am curious as to why if I were to run this code with:
BST testBST = new BST();
testBST.insert(testBST.getRoot(), 10);
testBST.insert(testBST.getRoot(), 7);
System.out.print(testBST.getRoot());
System.out.print(" ");
System.out.print(testBST.getRoot().getlChild());
This will output 10 then a NullPointerException. I understand this is because somehow the 7 didn't get allocated as 10's lChild, but I don't know why? Is it a scope issue I am having, or is it because I call recursively with the getlChild() in my insert function that I don't have access to the actual private lChild field?
NOTE: I was using sysout to debug my code, and I noticed the recursion does work, and it does assign the 7 properly to currentNode, but then once the function is done running, it is like currentNode no longer references the lChild of the initial root node.
The problem is here:
BSTNode newNode = new BSTNode(value);
Each time computer is calling the recursive method insert() , it is creating a new BSTNode(). You just want to add one new BSTNode() each time but it is creating nodes again and again. For example, You want to add 3 and for this it has to call insert() 4 times. Instead of creating only 1 node it will be creating 4 nodes.
What I have done, apart of removing some errors, I have created the recursive insertValue() method in BSTNode class. So you dont have to keep track of currentNode every time you call this method. As, every Node will be calling its own insertValue() method.
//*** Pseudo-ish Code ***
class BSTNode
{
public BSTNode lChild;
public BSTNode rChild;
public int key;
public BSTNode(int key)
{
this.lChild = null;
this.rChild = null;
this.key = key;
}
/* Create INSERT function in BSTNode class so that you dont have to give the "CurrentNode" everytime
you call this method, Now you just have to pass the "Key"*/
public void insertValue(int insertValue)
{
if(insertValue < key)
{
if(lChild == null)
lChild = new BSTNode(insertValue);
else
lChild.insertValue(insertValue);
}
else if(insertValue > key)
{
if(rChild == null)
rChild = new BSTNode(insertValue);
else
rChild.insertValue(insertValue);
}
else;
}
}
class BST
{
private BSTNode root;
public BST()
{
this.root = null;
}
// just create the root if not present else it'll call the recursive method of BSTNode class
public void insert(int value)
{
if(root == null)
root = new BSTNode(value);
else
root.insertValue(value);
}
// you didn't provide these methods so i wrote my own just to get your code runing
public BSTNode getRoot()
{
return root;
}
public int getRootValue()
{
return root.key;
}
}
public class BSTMain
{
public static void main(String[] args)
{
BST testBST = new BST();
testBST.insert(10);
testBST.insert(7);
System.out.print(testBST.getRootValue());
System.out.print(" ");
System.out.print(testBST.getRoot().lChild.key);
}
}
NOTE: I have added some methods like getRoot() just to get your code working, as you haven't provided them.
Related
In this binary tree implementation
I've tried to create an object from the BinaryTree class and thus insert elements and access them in order. While debugging it seems it's always returning root as NULL and thus the traversal fails.
I don't understand what I'm missing here. Where is my mistake?
public class BinaryTree{
public static class Node{
int value;
Node left;
Node right;
public Node(int data){
this.value = data;
left = null;
right = null;
}
}
Node root;
BinaryTree() {
root = null;
}
public Node addrecursive(Node current,int value){
if(current==null){
return new Node(value);
}else
if(value<current.value){
int n=current.value;
current.left=addrecursive(current.left,value);
}else
if(value>current.value){
int n=current.value;
current.right=addrecursive(current.right,value);
}else
{
return current;
}
return current;
}
public void add(int value) {
Node n = null;
if(root==null)
root = addrecursive(root, value);
else
n = addrecursive(n, value);
}
private void createBinaryTree(){
BinaryTree bt = new BinaryTree();
bt.add(6);
bt.add(4);
bt.add(8);
bt.add(3);
bt.add(5);
bt.add(7);
bt.add(9);
return;
}
private boolean containsNodeRecursive(Node current, int value) {
if (current == null) {
return false;
}
if (value == current.value) {
return true;
}
return value < current.value
? containsNodeRecursive(current.left, value)
: containsNodeRecursive(current.right, value);
}
public boolean containsNode(int value) {
return containsNodeRecursive(this.root, value);
}
public void traverseInOrder(Node node) {
if (node != null) {
traverseInOrder(node.left);
System.out.print(" " + node.value);
traverseInOrder(node.right);
}
}
void printInorder() { //wrapper class for access without passing node
traverseInOrder(root);
}
public static void main(String [] args){
BinaryTree bt = new BinaryTree() ; //object of class
bt.createBinaryTree(); //creating the binary tree within that object
Boolean b = bt.containsNode(7);
System.out.println(b);
System.out.println("\nInorder traversal of binary tree is " );
bt.printInorder();
}
}
There are several issues:
In addrecursive the variable n is a local reference that is unrelated to your root. So whatever n = addrecursive(n, value); does with the null that you pass as argument, it doesn't do anything with the linked list that starts at root.
It is actually quite simple... Your addrecursive function should only do:
public void add(int value) {
root = addrecursive(root, value);
}
The assignment to root is only really needed when root was null, but it doesn't hurt to always make that assignment. It is however important to pass root as argument, as that is the lead for where to append the new node.
createBinaryTree creates a new local instance of BinaryTree (which is already strange, since this already is an instance), adds nodes to it, and then just discards that local instance -- all work done for nothing. There are different ways to solves this, but I would make this method a static method, and have it return the BinaryTree instance that it populated. The caller in main should then take that returned tree and assign it to its own variable:
// Static!
private static BinaryTree createBinaryTree(){
BinaryTree bt = new BinaryTree();
bt.add(6);
bt.add(4);
bt.add(8);
bt.add(3);
bt.add(5);
bt.add(7);
bt.add(9);
return bt; // return the work that was done
}
public static void main(String [] args){
// Call static function to get the reference to the new tree
BinaryTree bt = createBinaryTree();
Boolean b = bt.containsNode(7);
System.out.println(b);
System.out.println("\nInorder traversal of binary tree is " );
bt.printInorder();
}
My goal is to create a tree-like object structure.
For this i created a class named Node (I removed the implementation because the problem still persists without it):
public class Node<S> {
public Node<S> addChild(Node<S> node) {
return this;
}
}
Important to know is that i want to define the generic type S only in the root node, all child nodes should automatically inherit from the root node.
Something like this:
new Node<String>().addChild(
new Node<>().addChild(
new Node<>()
)
)
I restricted the addChild method to only accept Nodes with the same generic type S,
so as far as i know my child node should know that it's generic type S has to be (in this example) String. However it seems like the generic type S gets lost after instantiating a new Node, because it gives me the following Exception:
error: incompatible types: Node<Object> cannot be converted to Node<String>
The use of <> requires type inference, and the argument of the first
addChild must be a Node, and just passing new Node<>() would do - infering from the return type.
But chaining to .addChild(new Node<>()) cannot infer anything, can only provide Node<Object>. So: one cannot use <>.
The problem is (of course) that you want addChild to return the head of the list, and keep adding to the tail of the list.
Normal practice is not to create Node instances, but just use the S values.
public class Node<S> {
private S value;
private Node<S> next;
public Node(S value) {
this.value = value;
}
public static <T> void print(Node<T> root) {
if (root == null) {
System.out.println("empty");
return;
}
System.out.print(root.value);
System.out.print(" --> ");
print(root.next);
}
public static <T> Node<T> addAll(T... values) {
Node<T> root = null;
Node<T> previous = null;
for (T value : values) {
Node<T> current = new Node<>(value);
if (root == null) {
root = current;
} else {
previous.next = current;
}
previous = current;
}
return root;
}
public static void main(String[] args) {
Node<String> root = Node.addAll("a", "b", "c", "d");
print(root);
}
}
Comparable to Collections.addAll or List.of. If you keep a Node<S> last field, you could indeed create something like:
public void addLast(S value) {
last.next = new Node<>(value);
}
This also shows a serious problem of the class: an empty list is not a Node.
One could use Optional<Node<S>> or a special constant for an empty list EMPTY - without value.
The normal solution is to have a container:
public class List<S> {
private class Node {
...
}
private Node<S> root;
private Node<S> last;
private int size;
public List<S> addLast(S value) {
Node<S> current = new Node<>(value);
if (root == null) {
root = current;
last = current;
} else {
last.next = current;
}
last = current;
++size;
return this;
}
private int size() {
return size;
}
...
}
Now everything fits.
List<String> nodes = new List<>()
.addLast("a")
.addLast("b")
.addLast("c")
.addLast("d");
After feedback, when wanting Node references.
Then discard chaining, and make Node public again.
public Node<S> addLast() {
addLast(null);
}
public Node<S> addLast(S value) {
Node<S> current = new Node<>(value);
if (root == null) {
root = current;
last = current;
} else {
last.next = current;
}
last = current;
++size;
return last;
}
List<String> nodes = new List<>()
Node<String> a = nodes.addLast();
Node<String> b = nodes.addLast();
var c = nodes.addLast();
var d = nodes.addLast();
One could use var for shortness.
What you are trying to do is something like this
public class Node<T> {
private Node<T> child;
private T data = null;
public Node (T data) {
this.data = data;
}
public T getData() {
return data;
}
public Node<T> getChild() {
return child;
}
public void addChild(Node<T> child) {
this.child = child;
}
#Override
public String toString() {
return "this node's data: " + data + "; has child? " + (child != null);
}
public static void main(String[] args) {
Node<String> root = new Node<> ("parent");
Node<String> child = new Node<>("child");
root.addChild(child);
System.out.println(root);
System.out.println(child);
}
}
If you were to execute this, it will output
this node's data: parent; has child? true
this node's data: child; has child? false
this node's data: 0; has child? false
this node's data: 1; has child? false
Notice how I can create nodes of type String and Integer. However, this class is incomplete if you want to create a tree structure. The implementation of "tree" will depend on what kind of tree you are talking about. For example, a simple binary tree will have two children at most. Other types of trees could have more children. Also, adding nodes to a tree might require balancing the tree.
Now, to your question, this answer suffices. I was able to demonstrate the use of generics to create Node objects of type T.
Having a lot of trouble trying to write a recursive method to add a new Node to the end of a LinkedList. I've been staring at this for a looooong time and still my terminal is printing blanks...if someone can help me out that would be greatly appreciated! Still trying to wrap my head around the concept of recursion. If I try to do the problem iteratively with a while loop, then I'm able to do it no problem but my assignment requires me to write it recursively. Thanks for the help :)
public class LinkedList
{
//
//Instance variable
//
private Node top;
//
//Instance and static methods below
//
//Accessor for the top Node
public Node getTop()
{
return top;
}
public void add(Object data) {
Node newNode = new Node(data,null);
addRec(top,newNode);
}
private Node addRec(Node start, Node newNode) {
if (start == null) {
start = newNode;
return start;
}
start.setLink(addRec(start.getLink(), newNode));
return start;
}
public String toString() {
String value = "";
Node current = top;
while (current != null) {
value += current.getData();
current = current.getLink();
}
return value;
}
Node class:
public class Node
{
//
//Instance variables
//
private Object data;
private Node link;
//
//Constructor
//
public Node (Object initData, Node initLink)
{
data = initData;
link = initLink;
}
//
//Accessors
//
public Object getData()
{
return data;
}
public Node getLink()
{
return link;
}
public void setData(Object data) {
this.data = data;
}
//
//Mutators
//
public void setLink(Node newLink)
{
link = newLink;
}
}
Recursively adding a node to the linked list is a fairly straightforward concept.
Lets look at the algorithm:
1] If the given node does not have a node after it (which we will call the next node), we add the current node.
2] Otherwise, we add the node to the next node. This would involve performing step 1 on the next node.
If you notice, Step 2 has a recursive call to step 1, as it is referencing the same node.
To implement this, first, we create a child class called Node, inside the LinkedList. This will require an int to store the data, and a Node that points to the next Node.
we create the method add() inside Node.
To implement step 1, we check if next is equal to null. If it is, we add the node as the next on on the linked list.
if(this.next== null)this.next= toAdd;
To implement step 2, we say otherwise, we call the method add on the next Node, and pass the value to add.
if(this.next== null)this.next= toAdd;
Now, we have to implement this in the class LinkedList.
We declare a root node, where the list starts.
Now, we declare a method that will do the following:
add a value to root.
That's it.
Recursion takes care of the rest.
Think about it, if root has a node after it, the data will be added to the next node, and so on.
Therefore, problem sorted, you have your list!
public class LinkedList
{
private Node root;
private class Node{
int data;
Node next;
public Node(int data){
this.data = data;
this.next = null;
}
public void add(Node toAdd){
if(this.next== null)this.next= toAdd;
else this.next.add(toAdd);
}
}
public LinkedList(int root){
this.root = new Node(root);
}
public void add(int toAdd){
this.root.add(new Node(toAdd));
}
}
I have TreeNode class - implementation of the node of the non-binary tree (List<TreeNode> children).
I need find the first node with the given data among the children of this. I wrote some method, but there is some problem obviously (java.lang.AssertionError: Failed to find a child with not-null data: expected:<2> but was:<null>). (if data is null I need to return first child with null data).
public TreeNode findChild(Object data) {
if (data == null) {
Iterator<TreeNode> a = getChildrenIterator();
TreeNode tmp;
while (a.hasNext()) {
tmp = a.next();
if (tmp.getData()==null) return tmp;
tmp.findChild(data);
}
}else
{
Iterator<TreeNode> a = getChildrenIterator();
TreeNode tmp;
while (a.hasNext()) {
tmp = a.next();
if (data.equals(tmp.getData())) return tmp;
tmp.findChild(data);
}
}
return null;
}
Your recursion isn't correct. You should be returning the result of tmp.findChild() if it returns a non-null value.
You also need to consider whether you're supposed to be implementing a depth-first or breadth-first search.
The problem is within the fact you don't return the result of the recursive call.
Maybe the following code will help:
import java.util.*;
public class TreeNode
{
// Constructor
public TreeNode()
{
children = new ArrayList<TreeNode>();
node_data = null;
}
// Get node's data
public Object getData()
{
return (node_data);
}
// Set node's data
public void setData(Object data)
{
node_data = data;
}
// Find the node with specified data
// Return null if not found
public TreeNode findChild(Object data)
{
// Maybe we're the one we're looking for
if (equalData(data))
return (this);
// Search within child nodes
Iterator<TreeNode> it;
TreeNode node;
it = getChildrenIterator();
while (it.hasNext())
{
node = findChild(it.next());
if (node != null)
return (node);
}
// If we get here, we didn't find it
return (null);
} // findChild
// Return whether specified data equals ours
private boolean equalData(Object data)
{
if (node_data == null)
return (data == null);
else
return (node_data.equals(data));
}
// Return iterator over node's children
private Iterator<TreeNode> getChildrenIterator()
{
return (children.iterator());
}
// The node's children
private List<TreeNode> children;
// The node's data
private Object node_data;
} // class TreeNode
public class LinkedList<T>
{
private Node head;
private int size;
public LinkedList()
{
}
public void addToHead(T value) // create new node, make new node point to head, and head point to new node
{
if (head == null)
{
head = new Node(value,null);
}
else
{
Node newNode = new Node(value,head);
head = newNode;
}
size++;
}
public boolean isEmpty()
{
return head == null;
}
public int size()
{
return size;
}
public void removeHead()
{
head = head.next;
size--;
}
public void addToTail(T value)
{
if (isEmpty())
{
System.out.println("You cannot addtoTail of a emptyList!");
}
else
{
System.out.println(value);
Node current = head;
System.out.println("we are pointing to head: "+current);
while (current.getNext() != null) // loop till the end of the list (find the last node)
{
System.out.println("we are now pointing to: "+current.getElement());
current = current.getNext();
}
System.out.println("We are at the last node:"+current); // its working
System.out.println("it should point to null:"+current.getNext()); // its working
current.setNext(new Node(value,null)); // make it point to our new node we want to insert
System.out.println(current.getNext()); // it is pointing to the new node.. yet the node is not actually inserted (local variable problem? )
size++;
}
}
public String toString()
{
String output = "";
if (!isEmpty())
{
Node current = head;
output = "";
while (current.getNext() != null)
{
output += current.toString()+ "->";
current = current.getNext();
}
}
return output;
}
protected class Node
{
private T element;
private Node next;
public Node()
{
this(null,null);
}
public Node(T value, Node n)
{
element = value;
next = n;
}
public T getElement()
{
return element;
}
public Node getNext()
{
return next;
}
public void setElement(T newElement)
{
element = newElement;
}
public void setNext(Node newNext)
{
next = newNext;
}
public String toString()
{
return ""+element;
}
}
}
So I have written this linkedlist class, and every method works except addtoTail. For example say I create a instance of my linkedlist class, and call addToHead(5), then addtoTail(6) and use my toString method to print out the linkedlist, it only contains 5->. I debugged the addToTail and everything seems to be pointing to the correct locations, yet for some reason it does not add the new node (6) to the list. Hopefully I explained that clearly. I am probably missing something really simple (I even drew it on paper to visualize it but do not see the problem).
Your addToTail function is probably fine. I think the culprit is your toString function. In particular, in this snippet:
while (current.getNext() != null)
{
output += current.toString()+ "->";
current = current.getNext();
}
Your condition terminates the loop before reaching the end. What you actually want is:
while(current != null) {
....
}