Binary Tree implementation for adding nodes level by level - java

I have been trying to write a program for creating a binary tree .I wish add nodes level by level .When i google the topic all i get is binary search tree .
here is the code
public class BinaryTree
{
public static void main(String[] args)
throws NullPointerException
{
Tree myTree = new Tree();
myTree.add(2,new Node(4));
myTree.add(4,new Node(5));
myTree.add(2,new Node(6));
//myTree.add(2,new Node(7));
}
}
class Node
{
int data;
boolean visited;
Node left,right;
Node(int data)
{
left=null;
right=null;
visited=false;
this.data=data;
}
}
class Tree
{
Node root;
int level,cLevel;
Tree()
{
root=null;
level=0;
cLevel=level-1;
}
protected void add(int data,Node node)
{
System.out.println("node.data k: "+node.data);
Node t;
if(root==null)
{
Node n=new Node(data);
root=n;
root.visited=true;
System.out.println("root visited"+root.data+""+root.visited);
level++;
cLevel++;
return;
}
while(root!=null)
{
}
}
}
I want to add new nodes level by level ,new level should not be created until a level isnt comleted ,all i get by googling is binary search tree .what should i do ,i have tried to use
depth first and breath first approach which didnt prrove helpfull .

You could achieve this by maintaining a queue of nodes which do not yet have two children. Every time you add a new node, stick it on the end of this queue, and also make it a child of the node at the front of this queue. Once the node at the front has two children, remove it from the queue. This way you'll build it up one level at a time, left to right, and only move on to the next level once the current one is finished.

You could also try "Limited Depth-first search"
A recursive implementation in Java using part of your code could be:
class Tree
{
Node root;
int level,cLevel;
Tree()
{
root=null;
level=0;
cLevel=level-1;
}
protected void add(int data)
{
System.out.println("data k: "+ data);
Node t;
if(root==null)
{
root=new Node(data);
level++;
} else {
cLevel = 0;
boolean added = add(data, root);
//Couldn't add to current level, add new level
if (!added){
level++;
cLevel = 0;
add(data, root);
}
}
}
private boolean add(int data, Node node)
{
cLevel++;
boolean added;
//Depth limited
if (cLevel<=level){
added = true;
//Try to add to current node
if (node.left == null)
node.left = new Node(data);
else if (node.right == null)
node.right = new Node(data);
else if (!add(data, node.left)) //Recursively trying to add to children
added = add(data, node.right);
} else {
added=false;
}
cLevel--;
return added;
}
}
Hope it helps.

Done with it.Thanks Animatinator .Although i have tested it with hardcode as i do not have time right now ,i have a paper early morning of Compiler Construction .
protected void add(int data,Tree mytree)
{
if(root==null)
{
root=new Node(data);
myList.addLast(root);
root.count++;
return;
}
Node node=mytree.myList.getFirst();
if(root!=null)
{
if(node.left==null)
{
node.count++;
node.left=new Node(data);
mytree.myList.add(node.left);
return;
}
else
{
node.count++;
node.right=new Node(data);
mytree.myList.add(node.right);
}
if(node.left!=null & node.right!=null)
{
mytree.myList.removeFirst();
}
}
}

Related

Insert in binary tree using recursion

I am trying to implement binary tree NOT binary search tree. I spent a good amount of time to write insert operation using recursion but did not get.
It should be a complete tree that is filling from left to right.
Can someone help me with that?Preferably in Java.
The following is the iterative way to do it .(:( this is not even working))
public static void insertNode(Node root,int x){
if(root==null) {
root = new Node(x);
return;
}
Node current;
Queue<Node> qq = new LinkedList<Node>();
((LinkedList<Node>) qq).push(root);
while(true){
current=qq.peek();
if(current.leftchild==null){
Node child = new Node(x);
child.parent = current;
current.leftchild=child;
return;
}
else { ((LinkedList<Node>) qq).push(current.leftchild);}
if(current.rightChild==null){
Node child = new Node(x);
child.parent=current;
current.rightChild=child;
return;
}
else{
((LinkedList<Node>) qq).push(current.rightChild);
}
((LinkedList<Node>) qq).pop();
}
The problem with your code is that you are pushing into the Linked List when actually you want to add to the list.
LinkedList.push(element) adds element to the front of the list while the LinkedList.add(element) will add element to the end. Following is the correct snippet:
public static void insertNode(Node root,int x){
{
if(root==null) {
root = new Node(x);
return;
}
Node current;
Queue<Node> qq = new LinkedList<Node>();
((LinkedList<Node>) qq).push(root);
while(true){
current=qq.peek();
if(current.leftchild==null){
Node child = new Node(x);
child.parent = current;
current.leftchild=child;
return;
}
else {
((LinkedList<Node>) qq).add(current.leftchild);}
if(current.rightChild==null){
Node child = new Node(x);
child.parent=current;
current.rightChild=child;
return;
}
else{
((LinkedList<Node>) qq).add(current.rightChild);
}
((LinkedList<Node>) qq).pop();
}
}

Inserting Elements in Binary Tree in Java

I have written a code to insert an element in a binary tree in java. Here are the functions to do the same:
public void insert(int data)
{
root = insert(root, data);
}
private Node insert(Node node, int data)
{
if (node == null)
node = new Node(data);
else
{
if (node.getRight() == null)
node.right = insert(node.right, data);
else
node.left = insert(node.left, data);
}
return node;
}
However when I traverse the tree, the answer I get is wrong. Here are the traversal functions (preorder):
public void preorder()
{
preorder(root);
}
private void preorder(Node r)
{
if (r != null)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
Okay so as suggested here's the definition for the Node class:
public class Node {
public int data;
public Node left, right;
/* Constructor */
public Node() {
left = null;
right = null;
data = 0;
}
/* Constructor */
public Node(int d, Node l, Node r) {
data = d;
left = l;
right = r;
}
//Constructor
public Node(int d) {
data = d;
}
/* Function to set link to next Node */
public void setLeft(Node l) {
left = l;
}
/* Function to set link to previous Node */
public void setRight(Node r) {
right = r;
}
/* Function to set data to current Node */
public void setData(int d) {
data = d;
}
/* Function to get link to next node */
public Node getLeft() {
return left;
}
/* Function to get link to previous node */
public Node getRight() {
return right;
}
/* Function to get data from current Node */
public int getData() {
return data;
}
}
I have re-checked the algorithm for traversal many times, and it's working perfectly. I believe the problem is in the insertion algorithm. Any suggestions?
If I understood correctly, you want to fill your binary tree in "layers". E.g. you want to put something into depth 4 only if depth 3 is "full binary tree".
Then the problem is whole logic of your insert algorithm that is DFS-based. In other words it inserts elements deeper and deeper on the one side instead of building full binary tree on both sides.
If you look closer to your insert algorithm you will see that once you skip "right" subtree, you will never return to it - even if the "left" subtree is already full binary tree. That leads to the tree that will be growing deeper and deeper on the left side but not growing on the right side.
Speaking in programming language. You do:
(node.right != null) && (node.left != null) => insert (node.left)
but you can't do this (start inserting node.left). What if node.left has both children and node.right has no children? You will attempt to insert to the left even you should do it in node.right.
So what you really need to do insertion BFS-based. That means you will traverse the tree for insertion "in layers". Queue should be your new friend here:-) (not the stack/recursion):
public void insert(int data) {
if (root == null) {
root = new Node(data);
return;
}
Queue<Node> nodesToProcess = new LinkedList<>();
nodesToProcess.add(root);
while (true) {
Node actualNode = nodesToProcess.poll();
// Left child has precedence over right one
if (actualNode.left == null) {
actualNode.left = new Node(data);
return;
}
if (actualNode.right == null) {
actualNode.right = new Node(data);
return;
}
// I have both children set, I will process them later if needed
nodesToProcess.add(actualNode.left);
nodesToProcess.add(actualNode.right);
}
}
Your method returns given node, but your method has to return inserted node which is node.right or node.left

Deleting a node that is in the middle of a linked list

I am trying to delete a node in the middle of two other nodes in a singly linked list.
public void deleteAfter(Node del){
del.next=del.next.next;
}
where it deletes the the node that is after the specified node,del.
I get a null pointer exception .I think the problem is after deletion the link with other nodes is broken.How can I mend it .Here's my full code :
public class Node{
public Object item;
public Node next;
public Node(){
item=null;
next=null;
}
public Node(Object x){
item=x;
next=null;
}
public void insertAfter(Node after,Object x){
Node newNode=new Node(x);
newNode.next=after.next;
after.next=newNode;
}
public void deleteAfter(Node del){//Deletes the node that is after the specified node
del.next=del.next.next;
}
public static void main (String args[]){
Node front=new Node(),p=new Node(),q=new Node();
p.item="green";
q.item="red";
p.next=q;
front=p;
front.deleteAfter(p);
front.insertAfter(p,"black");
front.insertAfter(q,"blue");
front.insertAfter(q.next,"orange");
front.deleteAfter(q);
System.out.println(front.item);
System.out.println(front.next.item);
System.out.println(front.next.next.item);
System.out.println(front.next.next.next.item);
}
}
First your list should remember last element or head.
public class YourList{
Node heaed;
public YourList(){
head = null;
}
public void insert(Node node){
if(last == null){
head = node;
}
}
public void deleteAfter(Node del){
if(del.next == head)){
head = del;
}
if(del.next == null){
//do nothing because there is nothing to delete
}
else{
del.next=del.next.next;
}
}
}
At:
p.next=q;
front=p;
front.deleteAfter(p);
front.insertAfter(p,"black");
You have created a two node linked list starting at p, and pointed front to it, then you shrank it down to a one node linked list, and bumped it back to two, consisting of { "green", "black" }. q is a singleton list node which you later manipulate.
When you print starting from front, since it only has two nodes, trying to obtain the item of the third node, which doesn't exist, causes your exception.
Your question started out by asking if there was something wrong with your deleteAfter(), and there is in that it won't correctly handle any correct list of nodes, only a list that actually has something after it. Passing in an empty list or a list with only one node in it will result in an exception.
You can first find the middle node using two pointer approach and then delete the node.
public Node findMiddleNode(Node node){
Node runner = node;
while(node!=null){
node = node.next;
if(node != null){
node = node.next;
runner = runner.next;
}
}
return runner;
}
public static boolean deleteNode(Node node){
if(node==null || node.next==null) return false;
Node next = node.next;
node.data = next.data;
node.next = next.next;
return true;
}
Solution which first calculates the size of the list and deletes the n/2 element without using two pointers.
For example, if given linked list is 1->2->3->4->5 then linked list should be modified to 1->2->4->5. If there are even nodes, then there would be two middle nodes, the second middle element gets deleted. For example, if given linked list is 1->2->3->4->5->6 then it should be modified to 1->2->3->5->6.
public void deleteMiddle() {
if (head == null) {
System.out.println("List is emplty");
return;
} else if (head.next == null) {
head = null;
return;
} else {
int count = 0;
int nodeDeleteIndex = 0;
Node node = this.head;
Node temp = this.head;
// To calculate the list size
while (node != null) {
count++;
node = node.next;
}
// getting the n/2 index of the node which needs to be deleted
nodeDeleteIndex = (count / 2);
for (int i = 0; i < nodeDeleteIndex - 1; i++) {
temp = temp.next;
}
temp.next = temp.next.next;
}
}

Write a method to find the common elements between two BSTs, and insert them in 3rd BST

I got an insert method and a search method, and I was thinking of a way to loop through the binary search tree and use a method like get nodes then search for it on the other binary search tree and if it comes true then I insert it that element, but the problem is I can't come up with a way to get the nodes based on index because its different than linkedList for example and can't think of a way to get the nodes to begin with; to sum up, I actually don't the proper way to start to solve that question.
public class BinarySearchTree extends BinaryTree {
//default constructor
//Postcondition: root = null;
public BinarySearchTree() {
super();
}
//copy constructor
public BinarySearchTree(BinarySearchTree otherTree) {
super(otherTree);
}
public class BinaryTree {
//Definition of the node
protected class BinaryTreeNode {
DataElement info;
BinaryTreeNode llink;
public DataElement getInfo() {
return info;
}
public BinaryTreeNode getLlink() {
return llink;
}
public BinaryTreeNode getRlink() {
return rlink;
}
BinaryTreeNode rlink;
}
protected BinaryTreeNode root;
//default constructor
//Postcondition: root = null;
public BinaryTree() {
root = null;
}
//copy constructor
public BinaryTree(BinaryTree otherTree) {
if (otherTree.root == null) //otherTree is empty
{
root = null;
} else {
root = copy(otherTree.root);
}
}
public BinaryTreeNode getRoot() {
return root;
}
public boolean search(DataElement searchItem) {
BinaryTreeNode current;
boolean found = false;
current = root;
while (current != null && !found) {
if (current.info.equals(searchItem)) {
found = true;
} else if (current.info.compareTo(searchItem) > 0) {
current = current.llink;
} else {
current = current.rlink;
}
}
return found;
}
public int countEven() {
return countEven(root);
}
public void insert(DataElement insertItem) {
BinaryTreeNode current;
BinaryTreeNode trailCurrent = null;
BinaryTreeNode newNode;
newNode = new BinaryTreeNode();
newNode.info = insertItem.getCopy();
newNode.llink = null;
newNode.rlink = null;
if (root == null) {
root = newNode;
} else {
current = root;
while (current != null) {
trailCurrent = current;
if (current.info.equals(insertItem)) {
System.out.println("The insert item is already in" + "the list -- duplicates are" + "not allowed.");
return;
} else if (current.info.compareTo(insertItem) > 0) {
current = current.llink;
} else {
current = current.rlink;
}
}
if (trailCurrent.info.compareTo(insertItem) > 0) {
trailCurrent.llink = newNode;
} else {
trailCurrent.rlink = newNode;
}
}
}
Traverse down to the left end of one tree, compare it with the root node of the other tree. If found equal, insert it into your third tree. If unequal, then check if it's less than or greater than the root of second tree. If less than, then traverse to the left child of the second tree and call your search method again, else, traverse to the right child of the second tree and call your search method again. Then repeat the whole process with the right node of the opposing starting node of first tree that you chose and call the search method again. Keep moving up the first tree as you repeat the process.
Here's a sample code(keeping in mind you have not provided any details about your trees whatsoever):
void search(Node node1, Node root2){
if(root2 == null)
return;
if(node1.data == root2.data){
//copy to your third tree
return;
}
else{
if(node1.data < root2.data){
root2 = root2.left;
search(node1, root2);
}
else{
root2 = root2.right;
search(node1, root2);
}
}
}
void common(Node root1, Node root2){
if(root1 != null){
common(root1.left, root2);
search(root1, root2);
common(root1.right, root2);
}
}
I'm assuming you need to modify the BinarySearchTree class, so the following is written with that assumption.
You can traverse the tree by first calling getRoot() which will return the root of the tree (a BinaryTreeNode) then access the nodes' left and right children by calling getLLink() and getRLink(), respectively. From each node you can get its value via getInfo that you can search for in the other tree (by calling the search() method on the second tree).
Note: as it is, you can only call methods on the nodes from within methods of BinarySearchTree as access to BinaryTreeNode is restricted to BinaryTree and classes deriving from it (for which BinarySearchTree qualifies)

Problem in binary tree; need help

can you help me please? I am making a binary tree with node insertion. How can I insert the new node to the current node in respect of BST rule?
Example: first the root is empty.
Input number: 50
This will display "Success!"
Insert number: 40
Successfully inserted in the left subtree of 50
insert number: 20
successfully inserted in the left subtree of 40
insert number: 80
successfully inserted in the right subtree of 50
Can you help me please? Thank you in advance hoping for your positive response...
Here's my code:
class Node
{
public int num;
public Node llink;
public Node rlink;
}
public class BinaryTreeOperations
{
//public Node llink=null;
// public Node rlink=null;
private Node temp;
private Node current;
private Node root;
public boolean isEmpty()
{
return root==null;
}
public void insertNum(int n)
{
temp=null;
current=null;
Node newNode = new Node();
newNode.num=n;
newNode.llink=null;
newNode.rlink=null;
if(isEmpty())
{
root=newNode;
System.out.println("Successfully inserted!");
}
else
{
temp=root;
while(temp!=null)
{
current = temp;
root = current;
temp=null;
}
if(n<current.num)
{
current.llink=newNode;
//current.llink=temp;
System.out.println("inserted on the left subtree " +current.num);
}
else
{
newNode.rlink=newNode;
System.out.println("inserted on the right subtree "+current.num );
}
}
}
Your while loop seems wrong. What you really want to do is start at the root and traverse the tree until you reach the node which will serve as the parent of your new node. Below you are not doing any traversal or inspection to find where the new node should go. That is what you really need to be doing.
while(temp!=null) {
current = temp;
root = current;
temp=null;
}
Should be something like this:
while(parent not found) {
if (new node is smaller than current) {
if (current has left child) {
assign left child to current and loop
} else {
make current the parent of the new node
}
} else {
....
}
}
else
{
temp=root;
while(temp!=null)
{
current = temp;
root = current;
temp=null;
}
This loop will only ever run once. Probably not what you intended. :)
if(n<current.num)
{
current.llink=newNode;
//current.llink=temp;
System.out.println("inserted on the left subtree " +current.num);
}
else
{
newNode.rlink=newNode;
System.out.println("inserted on the right subtree "+current.num );
}
In one branch you are assigning to current.llink, in the other branch you are assigning to newNode.rlink. Oops. :)
Your methodology for adding to the binary search tree seems incorrect.
You need to read up on Binary Search Trees to figure out how you will maintain the structure of your tree.
Below is some code that shows how to add to a Binary Search Tree, but I define a tree as empty if it contains no data.
public boolean isEmpty() {
return data == null;
}
The rest of the code is pretty self explanatory and should help you figure out how to add to the tree to maintain order.
public boolean add(Comparable target) {
if(isEmpty()) {
setData(target);
this.setLeftTree(new BinarySearchTree());
this.setRightTree(new BinarySearchTree());
return true;
} else {
int comparison = getData().compareTo(target);
if(comparison == 0)
return false;
else {
if(comparison < 0) {
return getRightTree().add(target);
} else {
return getLeftTree().add(target);
}
}
}
}
Let me know if you have any other questions.

Categories