Reputation: 21
I'm currently learning about trees using java and i have some errors going on here in the insertion of items in a binary tree I don't why it doesn't work
this is the code: tree node:
public class TNode {
int data;
TNode left;
TNode right;
public TNode(int data) {
this.data = data;
left = null;
right = null;
}
}
tree class:
public class Tree {
TNode root;
public Tree(){
root = null;
}
public TNode insertNode(TNode item, int d) {
if (item == null) {
return new TNode(d);
}
if (d < item.data) {
item.left = insertNode(item, d);
}
if (d > item.data) {
item.right = insertNode(item, d);
} else {
return item;
}
return item;
}
public void add(int d) {
insertNode(root, d);
}
}
Whenever I add an item the root remains null with no right or left items if someone can help I'll be really thankful
Upvotes: 1
Views: 229
Reputation: 1
This Java code creates a binary tree and inserts nodes with char values. The BinaryTree class has methods to insert nodes and get the current and last inserted nodes. The Node class represents a single node in the tree, with a char value and references to its left and right children.
package binarytreecreation;
class Node {
char value;
Node leftChild;
Node rightChild;
public Node(char value) {
this.value = value;
}
}
class BinaryTree {
private Node root;
private Node current;
public void insert(char value) {
var node = new Node(value);
if (root == null) {
root = node;
System.out.println(value + " Inserted");
return;
}
current = root;
while (true) {
if (current.leftChild == null) {
current.leftChild = node;
break;
}
if (current.rightChild == null) {
current.rightChild = node;
break;
}
current = current.leftChild;
}
System.out.println(value + " Inserted");
}
public char getCurrent() {
return current.value;
}
public char getLastInserted() {
return current.leftChild.value;
}
}
public class BinaryTreeCreation {
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
bt.insert('D');
bt.insert('A');
bt.insert('E');
bt.insert('B');
bt.insert('C');
bt.insert('F');
bt.insert('G');
System.out.println("current node = " + bt.getCurrent());
System.out.println("last inserted node =" + bt.getLastInserted());
}
}
Upvotes: 0
Reputation: 109547
Fine code, but recursing does not step further
item.left = insertNode(item.left, d);
item.right = insertNode(item.right, d);
And the initial root is not updated:
root = insertNode(root, d);
The else part or final return is superfluous.
Something on the code style
insertNode
has a node as input, and returns the updated node value, hence the call "pattern" should look like
X = insertNode(X, d); // X is some expression
This is because java can never assign to the passed argument expression: it has no pass-by-reference, but pass-by-value; f(x)
never assigns to x
.
Upvotes: 1
Reputation: 2216
root
is always null because you never assign value to it.
you can add to the beginning of your method check and assign it
public TNode insertNode(TNode item, int d){
if(item == null){
TNode node = new TNode(d);
if (root == null) {
root = node;
}
return node
}
... rest of method isn't changed...
Also when you are recursing you should make a call with a proper child node instead of always calling with item
, so for example first case would be:
item.left = insertNode(item.left, d);
For second case you would just use item.right
instead.
Upvotes: 1