I'm trying to make a tree, in a way such that the left child of a terminal(leaf node) node.
link to see what tree should look like.
I've gotten an inorder version of the code i want, its a simple insert function that threads the tree ,but now my problem is changing this code into a preOrder insert.
This i can do, but my main problem is finding the preorder successor, which is all the way in a other sub tree, do any of you guys know a simple way to get the preorder successor?
//in-order insert
if (root == null) { // tree is empty
root = newNode;
return;
}
PreNode<T> p = root, prev = null;
while (p != null) { // find a place to insert newNode;
prev = p;
if (info.compareTo(p.info) < 0)
p = p.left;
else if (!p.hasThread) // go to the right node only if it is
p = p.right; // a descendant, not a successor;
else break; // don't follow successor link;
}
if (info.compareTo(prev.info) < 0) { // if newNode is left child of
prev.left = newNode; // its parent, the parent becomes
newNode.hasThread = true; // also its successor;
newNode.right = prev;
}
else if (prev.hasThread) { // if parent of the newNode
newNode.hasThread = true; // is not the right-most node,
prev.hasThread = false; // make parent's successor
newNode.right = prev.right; // newNode's successor,
prev.right = newNode;
}
else prev.right = newNode; // otherwise has no successor;
This will work, assuming that there is a Node class which has right&left references to its children as well as a boolean variable ht which indicates if it has a thread or not.
public void insert(T info) {
PreNode<T> newNode = new PreNode<T>(info);
if (root == null) {
root = newNode;
return;
}
PreNode<T> curr = root, r = null, l = null;
while (true) {
if (info.compareTo(curr.info) < 0) {
if (curr.right != null)
r = curr.right;
if (curr.left == null || curr.ht) {
newNode.left = r;
curr.left = newNode;
curr.ht = false;
return;
} else
curr = curr.left;
} else {
if (curr.left != null && !curr.ht)
l = curr.left;
if (curr.right == null) {
if (curr.ht) {
newNode.left = curr.left;
newNode.ht = true;
curr.left = null;
curr.right = newNode;
curr.ht = false;
return;
} else
newNode.left = r;
curr.right = newNode;
curr.ht = false;
if (l != null) {
while (!l.ht) {
if (l.right != null)
l = l.right;
else
l = l.left;
}
l.left = newNode;
l.ht = true;
return;
}
} else
curr = curr.right;
}
}
}
Related
I am trying to delete an specific element from the linkedlist, however i am getting null pointer exception. Could any one pls fix my below mentioned code...
public void deleteElement(T num)
{
Node<T> ele = new Node<T>(num);
if(head == null){
System.out.println("Underflow");
return;
}
Node<T> temp = head;
while(temp != null)
{
if(temp.data == num){
temp.previous.next = temp.next;
return;
}
else
temp = temp.next;
}
size--;
}
You should modify inside your while loop like this:
while(temp != null)
{
if(temp.data == num) {
if(temp.previous != null) {
temp.previous.next = temp.next;
}
// you have to link-up the next's previous with temp's previous too
if(temp.next != null) {
temp.next.previous = temp.previous;
}
temp = null; // to deference the node and let garbage collector to delete/clear this node
break; // don't return here otherwise size-- won't execute
}
temp = temp.next;
}
Before referencing temp.next and temp.previous as lvalue you should check whether they are null otherwise it will throw NullPointerException.
Hope it helps!
You have to find Node object whose data is equal to T num. Use such loop:
for (Node<T> x = first; x != null; x = x.next) {
if (num.equals(x.data)) {
unlink(x);
return true;
}
}
Where first is pointer to first node. In method remove you have to unlink found Node x from linked list:
T remove(Node<T> x) {
// assert x != null;
final T element = x.data;
final Node<T> next = x.next;
final Node<T> prev = x.prev;
if (prev == null) {
//if x is first node(head)
first = next;
} else {
// link x.next to x.prev.next
prev.next = next;
//unlink x
x.prev = null;
}
if (next == null) {
//if x is last node(tail)
last = prev;
} else {
// link x.prev to x.next.prev
next.prev = prev;
//unlink x
x.next = null;
}
// reset data
x.data = null;
size--;
return element;
}
I am trying to remove nodes from a Binary Search Tree. I can successfully remove any other node on the tree except for one particular case. If the targeted node has 2 children, and the left child has a right subtree, I can locate the correct replacement Node and switch the value to the targeted Node, but then the replacement node is never deleted.
Looking at the picture above, if I try to delete 17, the program will correctly navigate to 13 and replace 17 with 13, but it will not then delete the original 13 as it is supposed to.
I have attached my remove methods and those referenced within.
public Node root;
public void delete(int value){
Node node = new Node<>(value);
Node temp;
if(root == null) {
System.out.println("The tree is already empty!"); //tree is empty
return;
}
if (root.value == node.value) { //Root is target value
temp = node.left;
if(temp.right == null){
node.value = temp.value;
temp = null;
}
else{
while(temp.right != null){
temp = temp.right;
}
node.value = temp.value;
temp = null;
}
return;
}
deleteRec(root, node);
}
private void deleteRec(Node lastRoot, Node node){
Node temp;
if (lastRoot.value >= node.value){
if (lastRoot.left.value == node.value){
node = lastRoot.left;
if(node.left == null && node.right == null){ //No children
node = null;
lastRoot.left = null;
}
else if(node.left == null && node.right != null){ //Right Child
lastRoot.left = node.right;
node = null;
lastRoot.left = null;
}
else if(node.left != null && node.right == null){ //Left Child
lastRoot.left = node.left;
node = null;
}
else{ //Two Children
if(node.left.right == null){
node.value = node.left.value;
node.left = node.left.left;
node.left = null;
}
else{
node = findReplacement(node.left);
lastRoot.left.value = node.value;
node.left = null;
}
}
}
else{
deleteRec(lastRoot.left, node);
}
}
else{
if (lastRoot.right.value == node.value){
node = lastRoot.right;
if(node.left == null && node.right == null){ //No Children
node = null;
lastRoot.right = null;
}
else if(node.left == null && node.right != null){ //Right Child
lastRoot.left = node.right;
node = null;
lastRoot.right = null;
}
else if(node.left != null && node.right == null){ //Left Child
lastRoot.right = node.left;
node = null;
}
else{ //Two Children
if(node.left.right == null){
node.value = node.left.value;
node.left = node.left.left;
node.left = null;
}
else{
node = findReplacement(node.left);
lastRoot.left.value = node.value;
node.left = null;
}
}
}
else{
deleteRec(lastRoot.right, node);
}
}
}
private Node findReplacement(Node node) {
while(node.right != null){
node = node.right;
}
return node;
}
And here is my Node class:
public class Node<T> {
public int value;
public Node left;
public Node right;
public Node parent;
public Node(int value) {
this.value = value;
}
}
Consider this part of your code:
Node rep = findReplacement(node.left);
node.value = rep.value;
rep = null;
You're finding the replacement, and making rep point to it. Then, essentially what you're doing is making rep point to null. This doesn't remove the node! The parent is still pointing to it!
There are several places in your code where you're doing something along these lines. The way you're expected to remove nodes from a tree in this Java implementation is by changing what parents point to. The garbage collector takes care of the other details. I hope addressing this issue helps you resolve your problem!
I'm trying to build a binary tree recursively for an AI I'm developing.
I try to build a tree but everything comes back null. The language is Java and I'm using Eclipse. Also, I'm on a Mac if that means anything. The tree should be returned as a binary tree with nodes instantiated but without any content.
public class DecisionTree {
//build a generic, empty, tree
//building binary
Root r = new Root();
public void build() //ok
{
Node lhs = new Node();
Node rhs = new Node();
lhs = new Node();
rhs = new Node();
r.lhs = lhs;
r.rhs = rhs;
lhs.parent = r;
rhs.parent = r;
builtRecursion(lhs, 1);
builtRecursion(rhs, 1);
outputTree();
int ctr = 1; //levels of tree
}
public int builtRecursion(Node n, int ctr)
{
Node lhs = new Node();
Node rhs = new Node();
ctr++;
System.out.println("built recursion ctr is " + ctr);
if (ctr > 10)
{
//leaf node
Behaviors behavior = new Behaviors();
Node node = behavior;
n.b = behavior;
return 0;
}
n.lhs = lhs;
n.rhs = rhs;
lhs.parent = n;
rhs.parent = n;
builtRecursion(lhs, ctr);
builtRecursion(rhs, ctr);
return ctr;
}
public void outputTree()
{
if (r != null)
{
System.out.println("Root");
}
outputTreeRecursive(r);
}
public void outputTreeRecursive(Node n)
{
if (n.lhs != null)
{
System.out.println("------------------");
System.out.println("LHS");
outputTreeRecursive(n.lhs);
}
else { System.out.println("LHS is null");}
if (n.rhs != null)
{
System.out.println("-----------------");
System.out.println("RHS");
outputTreeRecursive(n.rhs);
}
else { System.out.println("RHS is null");}
System.out.println("-----------------");
}
}
ROOT CLASSS
package FLINCH;
public class Root extends Node {
Node lhs = new Node();
Node rhs = new Node();
}
NODE CLASS
package FLINCH;
import java.util.ArrayList;
import java.util.LinkedList;
public class Node {
Node lhs = null;
Node rhs = null;
Node parent = null;
Decider d = new Decider(this);
Behaviors b = null;
public LinkedList getSuccessors()
{
LinkedList list = new LinkedList();
list.add(lhs);
list.add(rhs);
return list;
}
}
OUTPUT
GetAction Running
Iterating through open list
Size of open list is 1
Peeked openLIst size is 1
Peeking throguh open list
Popping Open List
LHS is null
RHS is null
Number of children is 2
Children equals 2
Decider childrens loop
Child node is null
Iterating through children
Exception in thread "main" java.lang.NullPointerException
at FLINCH.A_Star_Search.search3(A_Star_Search.java:81)
at FLINCH.Soldier.search_behavior(Soldier.java:28)
at FLINCH.Main.getAction(Main.java:54)
at tests.GameVisualSimulationTest.main(GameVisualSimulationTest.java:52)
I hope this helps...
I have a piece of code which you can use for BinaryTree
public class BinarySearchTree {
public static Node root;
public BinarySearchTree(){
this.root = null;
}
public void insert(int id){
Node newNode = new Node(id);
if(root==null){
root = newNode;
return;
}
Node current = root;
Node parent = null;
while(true){
parent = current;
if(id < current.data){
current = current.left;
if(current==null){
parent.left = newNode;
return;
}
}else{
current = current.right;
if(current==null){
parent.right = newNode;
return;
}
}
}
}
public boolean find(int id){
Node current = root;
while(current!=null){
if(current.data==id){
return true;
}else if(current.data > id){
current = current.left;
}else{
current = current.right;
}
}
return false;
}
public boolean delete(int id){
Node parent = root;
Node current = root;
boolean isLeftChild = false;
while(current.data!=id){
parent = current;
if(current.data > id){
isLeftChild = true;
current = current.left;
}else{
isLeftChild = false;
current = current.right;
}
if(current ==null){
return false;
}
}
//if i am here that means we have found the node
//Case 1: if node to be deleted has no children
if(current.left==null && current.right==null){
if(current==root){
root = null;
}
if(isLeftChild ==true){
parent.left = null;
}else{
parent.right = null;
}
}
//Case 2 : if node to be deleted has only one child
else if(current.right==null){
if(current==root){
root = current.left;
}else if(isLeftChild){
parent.left = current.left;
}else{
parent.right = current.left;
}
}
else if(current.left==null){
if(current==root){
root = current.right;
}else if(isLeftChild){
parent.left = current.right;
}else{
parent.right = current.right;
}
}else if(current.left!=null && current.right!=null){
//now we have found the minimum element in the right sub tree
Node successor = getSuccessor(current);
if(current==root){
root = successor;
}else if(isLeftChild){
parent.left = successor;
}else{
parent.right = successor;
}
successor.left = current.left;
}
return true;
}
public Node getSuccessor(Node deleleNode){
Node successsor =null;
Node successsorParent =null;
Node current = deleleNode.right;
while(current!=null){
successsorParent = successsor;
successsor = current;
current = current.left;
}
//check if successor has the right child, it cannot have left child for sure
// if it does have the right child, add it to the left of successorParent.
// successsorParent
if(successsor!=deleleNode.right){
successsorParent.left = successsor.right;
successsor.right = deleleNode.right;
}
return successsor;
}
public void display(Node root){
if(root!=null){
display(root.left);
System.out.print(" " + root.data);
display(root.right);
}
}
public static void printInOrder(Node root){
if(root == null){
return;
}
printInOrder(root.left);
System.out.print(root.data+" ");
printInOrder(root.right);
}
public static void printPreOrder(Node root){
if(root == null){
return;
}
System.out.print(root.data+" ");
printPreOrder(root.left);
printPreOrder(root.right);
}
public static void printPostOrder(Node root){
if(root == null){
return;
}
printPostOrder(root.left);
printPostOrder(root.right);
System.out.print(root.data+" ");
}
public static void main(String arg[]){
BinarySearchTree b = new BinarySearchTree();
b.insert(3);b.insert(8);
b.insert(1);b.insert(4);b.insert(6);b.insert(2);b.insert(10);b.insert(9);
b.insert(20);b.insert(25);b.insert(15);b.insert(16);
System.out.println("Original Tree : ");
b.display(b.root);
System.out.println("");
System.out.println("Check whether Node with value 4 exists : " + b.find(4));
System.out.println("Delete Node with no children (2) : " + b.delete(2));
b.display(root);
System.out.println("\n Delete Node with one child (4) : " + b.delete(4));
b.display(root);
System.out.println("\n Delete Node with Two children (10) : " + b.delete(10));
b.display(root);
System.out.println();
System.out.println("********* Printing In Order *********");
printInOrder(root);
System.out.println();
System.out.println("********* Printing Pre Order *********");
printPreOrder(root);
System.out.println();
System.out.println("********* Printing Post Order *********");
printPostOrder(root);
}
}
class Node{
int data;
Node left;
Node right;
public Node(int data){
this.data = data;
left = null;
right = null;
}
}
when you call your buildRecursion with ctr = 1, you probably mean you want to build a tree with just one extra level and from your comment where you expect to build a leaf node, the condition needs to be modified. in your case the condition should be:
if (ctr == 1)
I made some changes to your function for better output:
public void builtRecursion(Node n, int ctr)
{
System.out.println("built recursion ctr is " + ctr);
if (ctr == 1)
{
//leaf node
Behaviors behavior = new Behaviors();
n.b = behavior;
return;
}
Node lhs = new Node();
Node rhs = new Node();
n.lhs = lhs;
n.rhs = rhs;
lhs.parent = n;
rhs.parent = n;
builtRecursion(lhs, ctr--);
builtRecursion(rhs, ctr--);
}
About the problem regarding I try to build a tree but everything comes back null, well in your outputTree and outputRecursion you don't print anything instead of "-----", "LHS", "RHS" and in case you reach where there is no left or right node you will print "LHS/RHS is null" but you should know that with a leaf node this is an accepted behavior so when you reach the leaf nodes you should print their values instead. However you can change the outputTreeRecursive to the following:
public void outputTreeRecursive(Node n)
{
if (n.lhs != null)
{
System.out.println("------------------");
System.out.println("LHS");
outputTreeRecursive(n.lhs);
}
else {
System.out.println("LHS is null");
if(n.b != null)
System.out.println("Leaf node");
}
if (n.rhs != null)
{
System.out.println("-----------------");
System.out.println("RHS");
outputTreeRecursive(n.rhs);
}
else {
System.out.println("RHS is null");
if(n.b != null)
System.out.println("Leaf node");
}
System.out.println("-----------------");
}
now probably you get better idea about your tree
I'm trying to delete multiples nodes that meets a criteria from a linked list. The program is a bit complex so I'll get state the gist of it. The nodes in my linked list has the following characteristics (a name associated with a number):
Name Number
Dog 1
Cat 1
Rat 2
Donkey 3
Fish 1
I want to be able to delete the nodes with the number 1. My delete function:
public void Delete(Int N) {
Node current = Head;
Node previous = Head;
while (current.getNum() != N) {
if (current.getNextNode() == null) {
System.out.print("Not found");
} else {
previous = current;
current = current.getNextNode();
}
}
if (current == Head) {
Head = Head.getNextNode();
} else {
Node A = current.getNextNode();
previous.setNextNode(A);
}
}
This works but it only removes the first occurrence. I know it may be due to the lack of or appropriate loop structure but I've been working on this for hours and I'm getting confused along the way. I've tried doing a trace table manually but that's not working either.
How can the function be edited so it loops through the entire linked lists and removes the nodes that matches the criteria?
This should remove the matching Node instances from the linked list:
public void delete(int n) {
int count = 0;
Node prev = null, next;
for (Node current = head; current != null; current = next) {
next = current.getNextNode();
if (current.getNum() == n) {
count++;
if (prev != null) {
prev.setNextNode(next);
} else {
head = next;
}
} else {
prev = current;
}
}
System.out.print(count > 0 ? ("Number deleted: " + count) : "Not found");
}
Your loop while (current.getNum() != N) will end after the first occurrence of a node that has the number N. If you want to go through the entire list then the loop should look like
while (current != null) {
//do something with the list
current = current.getNextNode();
}
Specifically for this case you want to remove a node.
Node prev = null;
while (current != null) {
Node next = current.getNextNode()
if(current.getNum() == N){
//condition to remove current node has been found.
if(prev == null){
Head = next;
} else {
prev.setNextNode(next);
}
} else {
//only advance prev if we haven't deleted something
prev = current;
}
current = current.getNextNode();
}
If you want to delete a node in your linkedlist, you can use any of the following ways
a new linked list only with the nodes in which number is not equals to N
or modify the existing one.
I have used the first way, creating a new linked list with element in which number is not equals to N.
class Node {
public String name;
public int number;
public Node next;
}
public class LinkedListTest {
public static void main(String[] args) {
LinkedListTest obj = new LinkedListTest();
Node head = obj.createLinkList();
Node startPointer = head;
while (startPointer != null) {
System.out.println(startPointer.name + " " + startPointer.number);
startPointer = startPointer.next;
}
System.out.println("***********");
Node newNodeHead = obj.deleteNode(1, head);
startPointer = newNodeHead;
while (startPointer != null) {
System.out.println(startPointer.name + " " + startPointer.number);
startPointer = startPointer.next;
}
}
public Node deleteNode(int n, Node head) {
Node current = head;
Node newNodestartPointer = null;
Node newNodeCurrent = null;
while (current != null) {
if (!(current.number == n)) {
if (newNodestartPointer == null) {
newNodestartPointer = new Node();
newNodestartPointer.name = current.name;
newNodestartPointer.number = current.number;
newNodestartPointer.next = null;
newNodeCurrent = newNodestartPointer;
} else {
Node newNode = new Node();
newNode.name = current.name;
newNode.number = current.number;
newNodeCurrent.next = newNode;
newNodeCurrent = newNode;
}
}
current = current.next;
}
return newNodestartPointer;
}
public Node createLinkList() {
Node head = null;
Node newNode = new Node();
newNode.name = "Dog";
newNode.number = 1;
newNode.next = null;
head = newNode;
newNode = new Node();
newNode.name = "Cat";
newNode.number = 1;
newNode.next = null;
head.next = newNode;
Node prevNode = newNode;
newNode = new Node();
newNode.name = "Rat";
newNode.number = 2;
newNode.next = null;
prevNode.next = newNode;
prevNode = newNode;
newNode = new Node();
newNode.name = "Donkey";
newNode.number = 3;
newNode.next = null;
prevNode.next = newNode;
return head;
}
}
I am having some problems printing an inOrder traversal of my binary tree. Even after inserting many items into the tree it's only printing 3 items.
public class BinaryTree {
private TreeNode root;
private int size;
public BinaryTree(){
this.size = 0;
}
public boolean insert(TreeNode node){
if( root == null)
root = node;
else{
TreeNode parent = null;
TreeNode current = root;
while( current != null){
if( node.getData().getValue().compareTo(current.getData().getValue()) <0){
parent = current;
current = current.getLeft();
}
else if( node.getData().getValue().compareTo(current.getData().getValue()) >0){
parent = current;
current = current.getRight();
}
else
return false;
if(node.getData().getValue().compareTo(parent.getData().getValue()) < 0)
parent.setLeft(node);
else
parent.setRight(node);
}
}
size++;
return true;
}
/**
*
*/
public void inOrder(){
inOrder(root);
}
private void inOrder(TreeNode root){
if( root.getLeft() !=null)
this.inOrder(root.getLeft());
System.out.println(root.getData().getValue());
if( root.getRight() != null)
this.inOrder(root.getRight());
}
}
It seems that you are not traversing the tree properly upon insertion, to find the right place for the new node. Right now you are always inserting at one child of the root, because the insertion code is inside the while loop - it should be after it:
public boolean insert(TreeNode node){
if( root == null)
root = node;
else{
TreeNode parent = null;
TreeNode current = root;
while( current != null){
if( node.getData().getValue().compareTo(current.getData().getValue()) <0){
parent = current;
current = current.getLeft();
}
else if( node.getData().getValue().compareTo(current.getData().getValue()) >0){
parent = current;
current = current.getRight();
}
else
return false;
}
if(node.getData().getValue().compareTo(parent.getData().getValue()) < 0)
parent.setLeft(node);
else
parent.setRight(node);
}
size++;
return true;
}
You insert method has a problem. It finds the right parent node to attach the new element to, but on the way it messes up the whole tree. You should move the insertion code out of the while loop:
public boolean insert(TreeNode node){
if( root == null)
root = node;
else{
TreeNode parent = null;
TreeNode current = root;
while( current != null){
if( node.getData().getValue().compareTo(current.getData().getValue()) <0){
parent = current;
current = current.getLeft();
}
else if( node.getData().getValue().compareTo(current.getData().getValue()) >0){
parent = current;
current = current.getRight();
}
else
return false;
}
if(node.getData().getValue().compareTo(parent.getData().getValue()) < 0)
parent.setLeft(node);
else
parent.setRight(node);
}
size++;
return true;
}
}
hey fellows here is one simple one.. try this out.. it works for me well...
public void levelOrderTraversal(Node root){
Queue<Node> queue = new ArrayDeque<Node>();
if(root == null) {
return;
}
Node tempNode = root;
while(tempNode != null) {
System.out.print(tempNode.data + " ");
if(tempNode.left != null) queue.add(tempNode.left);
if(tempNode.right != null) queue.add(tempNode.right);
tempNode = queue.poll();
}
}