Removing/undoing the only node in a doubly linked list - java

My code undos the added elements one by one starting with the latest add. It does everything correctly unless there is only one node left in a list and my code does not undo/remove it from the list. For example:
[A, B, C, D, E] //call undo()
[A, B, C, D] //call undo()
[A, B, C] //call undo()
[A, B] //call undo()
[A] //call undo() and it throws Exception here <------------------
Exception in thread "main" java.lang.NullPointerException
If I undo [A] it should return empty list [].
NOTE: I HAVE TO USE A DUMMY NODE called beginMarker and endMarker which have a value of null, so the last element looks like this:
beginMarker <-> "A" <-> endMarker
For the last element, the code checks if the size equals to 1 and it's true and proceeds further but does not empty out the list. Any help would be appreciated!
public void add(x){
.........
undoStack.push(newNode);
}
public void undo(){
if(undoStack.isEmpty()){
throw new RuntimeException("Undo history is empty");
}
else{
Node<T> object = undoStack.topAndPop();
redoStack.push(object);
if(this.size() == 1){
beginMarker = object.next;
beginMarker.next = null;
//beginMarker.next = null;
}
if(object.prev == beginMarker){
beginMarker.next = object.next.prev;
object.next.prev = beginMarker;
}
else if(object.next == null){
object.prev.next = null;
}
else{
object.next.prev = object.prev;
object.prev.next = object.next;
}
theSize--;
modCount--;
countUndone++;
}
Class SimpleStack
public class SimpleStack<AnyType>{
// Tracks the top node of stack.
private Node<AnyType> topOfStack;
// Construct the stack.
public SimpleStack( ) {
topOfStack = null;
}
// Test if the stack is logically empty.
// #return true if empty, false otherwise.
public boolean isEmpty( ) {
return topOfStack == null;
}
// Make the stack logically empty.
public void clear( ) {
topOfStack = null;
}
// Insert a new item into the stack.
// #param x the item to insert.
public void push( AnyType x ) {
topOfStack = new Node<AnyType>( x, topOfStack );
}
// Remove the most recently inserted item from the stack.
// #throws UnderflowException if the stack is empty.
public void pop( ) {
if( isEmpty( ) )
throw new RuntimeException( "SimpleStack pop" );
topOfStack = topOfStack.next;
}
// Get the most recently inserted item in the stack.
// Does not alter the stack.
// #return the most recently inserted item in the stack.
// #throws UnderflowException if the stack is empty.
public AnyType getTop( ) {
if( isEmpty( ) )
throw new RuntimeException( "SimpleStack empty in getTop" );
return topOfStack.data;
}
// Return and remove the most recently inserted item
// from the stack.
// #return the most recently inserted item in the stack.
// #throws UnderflowException if the stack is empty.
public AnyType topAndPop( ) {
if( isEmpty( ) )
throw new RuntimeException( "SimpleStack empty in topAndPop" );
AnyType topItem = topOfStack.data;
topOfStack = topOfStack.next;
return topItem;
}
// A singly linked Node which contains data and a link to another
public class Node<T>{
public T data;
public Node<T> next;
public Node(T d, Node<T> n ){
this.data = d;
this.next = n;
}
}
}

I would not be surprised the NPE occurs here :
if(object.prev == beginMarker){
beginMarker.next = object.next.prev;
// ^^^^
// is it here ? object.next is null in your case
object.next.prev = beginMarker;
}
It should fix it to simply write
if(object.prev == beginMarker && object.next != null) {
beginMarker.next = object.next.prev;
object.next.prev = beginMarker;
}
However, you don't really have to particularize the case size() == 1. A simpler implementation would be (assuming you add a pointer to the end of the list) :
public void undo() {
if (undoStack.isEmpty()) {
throw new NoSuchElementException("Undo history is empty");
} else {
Node<T> object = undoStack.topAndPop();
redoStack.push(object);
object.prev.next = object.next;
object.next.prev = object.prev;
theSize--;
modCount--;
countUndone++;
}
}
This should work since using an endMarker will prevent you from having any null node in the list.

Related

Eclipse debugger not working ( Skip breakpoints disabled )

This is my first time running the debugger to check the values of my binary search tree but it seems to skip through all the debugger and it has a weird arrow beside the blue dot. By the way, the skip all breakpoints is disabled I made sure of it.
This is a short GIF of me running the program
https://gyazo.com/e236c1bd75ac746bf9982871ca847233
Added my other class
public class BinaryTree<T extends Comparable<T>> {
private class Node{
private T data;
private Node left;
private Node right;
// left and right child do not have to nessary exist
public Node ( T data) {
this.data = data;
this.left = null;
this.right = null;
}}
private Node root;
private int count = 0;
public void add( T data) {
if ( isEmpty()) {
root = new Node(data);
count++;
}
else {
insert(data, root);
count++;
}
}
public boolean isEmpty() {
return root == null;
}
public T getRoot() {
if ( root.data == null) {
System.out.println("Root is empty");
return null;
}
else {
return root.data;
}}
/*
* Checking if the data is larger or lesser than the parent's data
* If the data is smaller than the parent's data, node.left is created
* If the data is bigger than the parent's data, node.right is created
*/
private void insert( T data, Node node) {
/*
* If 1st obj is less than the 2nd obj return a neg
* if 1st obj is more than the 2nd obj return a pos
* if equal return 0
*/
int compare = data.compareTo(node.data);
if ( compare < 1 ){
if (node.left == null ) {
node.left = new Node(data);
}
// make node.left if it is filled
else {
insert(data, node.left);
}
}
else {
if ( node.right == null) {
node.right = new Node(data);
}
else {
insert( data, node.right);
}
}
}
public int getSize() {
return count;
}
private void removeInner( T data, Node node ) {
Node temp;
while ( node.data!=data) {
int compare = data.compareTo(node.data);
if ( compare < 1 ){
node = node.left;
}
// make node.left if it is filled
if ( compare > 1 ){
node = node.right;
}
if ( compare == 0) {
node = null;
}
}
}
}
You have one or more so-called Trigger Points (decorated with a T) somewhere else from which one must be hit first to activate the other regular breakpoints. This can be seen by the regular breakpoint in line 9 being decorated with a crossed-out T.
Solution: Delete or deactivate all Trigger Points (those decorated with T), e.g. in the Breakpoints view.

Recursively Find and Remove Node from LinkedList

Given a string to search for, I want to write a recursive function that takes in only one parameter (The string to search for). The function will search for the value recursively and if it is found then it will remove the item and return it. If it is not found then, the function will reach the end of the list and return null. What I have so far I think is the right idea except it is not functioning properly:
Main Test Class
public static void main(String[] args) {
RecLinkedList list = new RecLinkedList();
list.add("A");
list.add("B");
list.add("D");
list.add("C", 2);
list.add("E", 4);
list.add("G", 6); //this should be invalid
System.out.println( list );
System.out.println( list.remove( 1 ).getValue() );
System.out.println( list.remove("D").getValue() );
System.out.println( list.remove("G").getValue() );
System.out.println( list.size() );
System.out.println( list );
}
Linked List Class (Showing only what I need help on)
public class RecLinkedList {
private Node first;
private int size = 0;
public RecLinkedList(){
first = null;
}
public boolean isEmpty() {
return first == null;
}
public Node remove( String s ){
return remove( s, 0, first );
}
private Node remove( String s, int count, Node list ){
if( list == null ){
return null;
}else if( s.equals(s) ){
first = list.getNext();
return list;
}else if( s.equals(count+1) ){
Node n = list.getNext();
if( list.getNext() != null ){
list.setNext( list.getNext().getNext() );
}
return n;
}else{
return remove( s, count+1, list.getNext() );
}
}
So far, I am able to remove the item but as of now the item "A" is getting removed when it should not be. The final list should be A,C,E. (G should return and print null because it does not exist). I think I am close, but off by something minor, but I can not seem to figure it out.
There are several errors in your code (see comments below) :
private Node remove( String s, int count, Node list ){
if( list == null ){
return null;
}else if( s.equals(s) ){ // comparing s to itself means you always remove
// the first element from the list (since this
// condition is always true)
first = list.getNext();
return list;
}else if( s.equals(count+1) ){ // comparing the String s to an int - makes
// no sense, will never be true
Node n = list.getNext();
if( list.getNext() != null ){
list.setNext( list.getNext().getNext() );
}
return n;
}else{
return remove( s, count+1, list.getNext() );
}
}
It seems to me like there is some ambiguity in your question. I understand that your method should search for an element, remove it if present, and return the same object. If the element is not present, the method should return null. That seems pretty straight-forward since most of your utility methods are already implemented in LinkedList. I thus recommend to extend that class:
public class RecLinkedList<E>
extends LinkedList<E>
{
public E removeAndReturn(E element)
{
E result;
if (this.contains(element)) {
remove(element);
result = element;
}
else {
result = null;
}
return result;
}
}
I don't see why you would want to implement this recursively.
This could clearly be written more concisely, but the explicit if-else should make it clearer.
EDIT: The more concise and probably better implementation would be:
public E removeAndReturn(E element)
{
return remove(element) ? element : null;
}

How to get the level of a node in a generic tree?

Given a generic tree implemented as a root node with a list of sons, which sons are nodes and again each node has a list of its sons.
__A__
/ | \
B C D
| / \
E F G
The node A has a list of its sons: B, C, D
B, C, D also have a list of their sons: B --> E ; C --> F, G ; D --> null ;
I will explain my idea of the algorithm, you can fix it or give me another completely new idea.
public Integer level(T dato) {...}
Traverse the tree adding to the queue each node of the tree or adding a "null" if the last node added is the last node of the level. Null is an identifier in the queue to know where the level has ended.
My problem is that I don't know exactly where to put the identifier after the first time.
Here is some of the code:
public Integer level(T data){
int inclu= this.include(data);
if (inclu==-1) { // if the tree doesn't include the data
return -1;
} else {
return inclu; // returns the level
}
}
public Integer include( T data ) { // returns the level where the data is
Integer inclu = -1; // -1 if the data is not included
if (this.getDataRoot()==data){
return 0; // The root of the tree has the data
}
else {
LinkedList<GenericNode<T>> queue = new LinkedList<GenericNode<T>>();
GenericNode<T> tree = new GenericNode<T>();
int level=1;
queue.addAtBeginning(this.getRoot());
queue.addAtBeginning(null);
while (queue.size()>0 && inclu==-1) {
if(queue.element(queue.size())!=null) { // if it is not the end of the level then dequeue
tree.setData(queue.element(queue.size()).getData()); //queue.element(position) returns the element in that position
tree.setListOfSons(queue.element(queue.size()).getSons());
if (tree.getSons()!=null) { // if the tree has sons
int i=1;
while(i<=tree.getSons().size() && inclu==-1) {
queue.addAtBeginning(tree.getSons().element(i));
if (tree.getSons().element(i).getData()==data) // if I found the data I'm looking for
inclu=level;
i++; // counter
}
}
} else { // if it is the end of the level (means the queue gave me a null)
level++;
}
queue.delete(queue.size()); //ending the dequeue process
} //end while
} // end main else
return inclu; //returns the summation of the levels or 0 if it was found at the root of the tree or -1 if the data was not found
}
I wrote a class that returns the level of target node in specific tree.
import java.util.LinkedList;
import java.util.List;
public class TreeLevel {
public static class Node {
public Node(String data) { this.data = data ; };
public String data;
public List<Node> childs = new LinkedList<Node>();
}
public static Integer level(Node tree, Node target){
return level(tree, target, 0);
}
private static Integer level(Node tree, Node target, int currentLevel) {
Integer returnLevel = -1;
if(tree.data.equals(target.data)) {
returnLevel = currentLevel;
} else {
for(Node child : tree.childs) {
if((returnLevel = level(child, target, currentLevel + 1)) != -1){
break;
}
}
}
return returnLevel;
}
public static void main(String[] args) {
Node a = new Node("A");
Node b = new Node("B");
Node c = new Node("C");
Node d = new Node("D");
Node e = new Node("E");
Node f = new Node("F");
Node g = new Node("G");
// childs of a:
a.childs.add(b);
a.childs.add(c);
a.childs.add(d);
// childs of b:
b.childs.add(e);
// childs of c:
c.childs.add(f);
c.childs.add(g);
// childs of d:
// d.childs = null or simply d.childs.length() is 0
Node target = new Node("G");
Integer level = level(a, target);
System.out.println("level [" + level + "]");
}
}
I think I can give you a simple code for this question. You can change the it according to your code.
public Integer include( T data ) { // returns the level where the data is
Integer inclu = -1; // -1 if the data is not included
if (this.getDataRoot() == data){
return 0; // The root of the tree has the data
}
return level(this.getRoot(), data, 1);
}
//Find data in a tree whose root is Node
//If not found, return -1
public int level(T node, T data, int level) {
if (!node.hasChildren()) {
return -1;
}
for (T child : node.getChildren()) {
if (child.getData == data) {
return level; //Aha!!! found it
} else {
int l = level(child, data, level + 1); /// find in this sub-tree
if (l != -1) {
return l;
}
}
}
return -1; /// Not found in this sub-tree.
}
P.S : == is used to compare, which is not good. .equals() should be used.

Java Linked List Sequence insertFirst method inserts object twice

I am trying to implement a Linked List Sequence in Java however my method for inserting an object at the start of the sequence inserts the object twice and I don't understand why.
If anyone can help it would be really appreciated.
The current output of my test program is
30
30
but I want it to be 30
Thanks in advance
Below is the code for the sequence and the method for insertFirst
public class Sequence implements SequenceInterface{
private Node listHead;
private Node listTail;
protected class Node{
protected Object datum;
protected Node next;
public Node(Object o, Node n) {
datum = o;
next = n;
}
}
//Constructor
public Sequence(){
listHead = null;
listTail = null;
}
public void insertFirst(Object o) {
if(listHead == null){
listHead = new Node(o, listTail);
listTail = listHead;
}else{
Node oldLH = listHead;
listHead = new Node(o, oldLH);
}
}
}
Here is my test program
public class SequenceTest {
/**
* #param args
* #throws Exception
*/
public static void main(String[] args) throws Exception {
Sequence s = new Sequence();
s.insertFirst(new Integer(30));
for(int i=0; i<2; i++){
System.out.println(s.element(i));
}
}
}
It looks like it is only added once, but is being printed twice.
If you unroll the loop:
for(int i=0; i<2; i++){
System.out.println(s.element(i));
}
you essentially get:
System.out.println(s.element(0));
System.out.println(s.element(1));
so if this is unexpected behaviour, what we really need to look at to diagnose it is the element() method.
What do you want to happen when s.element(2) is called and 's' only contains one element? Should it throw an index out of range exception? Should it wrap or crop to within range when it is indexed past the lists range (presumably the current behaviour)?
Assuming you are trying to create a singly linked list, you've got this backwards:
listHead = new Node(o, listTail);
listTail = listHead;
When you execute that, you are assigning the new node to both listHead and listTail. You should have
listTail = listHead;
listHead = new Node(o, listTail);
public void insertFirst(Object o) {
Node newNode = new Node(o, listHead);
if (listHead == null) {
listHead = newNode;
listTail = listHead;
} else {
listHead = newNode;
}
}
This is the final version tested that works perfectly! Hope this helps:
class SequenceDLListException extends Exception {
SequenceDLListException() {
super();
}
SequenceDLListException(String s) {
super(s);
}
}
/**
* <dl>
* <dt>Purpose: Implementation of Sequence ADT.
* <dd>
*
* <dt>Description:
* <dd>This class is an implementation of the Sequence using an linked list as
* the underlying data structure. The capacity is therefore unlimited and
* overflow does not need to be checked.
* </dl>
*
*
* #version $Date: 2015/01/30
*/
public class SequenceDLList {
/**
* Member class Node encapsulates the nodes of the linked list in
* which the stack is stored. Each node contains a data item and a
* reference to another node - the next in the linked list.
*
*
*/
public Node lastNode() {
return listTail;
}
public Node firstNode() {
return listHead;
}
protected class Node {
public Node(Object o) {
this(o, null, null);
}
/**
* I added another null because it has to point at the beginning and at the end
* to some kind of terminator to facilitate the traversal of the list
*/
public Node(Object o, Node p, Node n) {
datum = o;
prev =p;
next = n;
}
/**
* I added another Node because every node has two links now with the Doubly Linked List
*/
//The Node data structure consists of two object references.
//One for the datum contained in the node and the other for
//the next node in the list.
protected Object datum;
protected Node next;
protected Node prev;
}
//We use object references to the head and tail of the list (the head
//and tail of the sequence, respectively).
private Node listHead;
private Node listTail;
//Only require a single constructor, which sets both object
//references to null.
/**
* Constructs an empty sequence object.
*/
public SequenceDLList() {
listHead = null;
listTail = null;
}
/**
* Adds a new item at the start of the sequence.
*/
public void insertFirst(Object o) {
//There is a special case when the sequence is empty.
//Then the both the head and tail pointers needs to be
//initialized to reference the new node.
if (listHead == null) {
listHead = new Node(o, listHead, null);
listTail = listHead;
}
//In the general case, we simply add a new node at the start
//of the list via the head pointer.
else {
listHead = new Node(o, listHead, null);
}
}
/**
* Adds a new item at the end of the sequence.
*/
public void insertLast(Object o) {
//There is a special case when the sequence is empty.
//Then the both the head and tail pointers needs to be
//initialized to reference the new node.
if (listHead == null) {
listHead = new Node(o, listHead, null);
listTail = listHead;
}
//In the general case, we simply add a new node to the end
//of the list via the tail pointer.
else {
listTail.next = new Node(o, listTail, null);
listTail = listTail.next;
}
}
/**
* Adds a new item at a specified position in the sequence.
*/
public void insert(Object o, int index) throws SequenceDLListException {
//Check the index is positive.
if (index < 0) {
throw new SequenceDLListException("Indexed Element out of Range");
}
//There is a special case when the sequence is empty.
//Then the both the head and tail pointers needs to be
//initialized to reference the new node.
if (listHead == null) {
if (index == 0) {
listHead = new Node(o, listHead, null);
listTail = listHead;
} else {
throw new SequenceDLListException("Indexed element is out of range");
}
}
//There is another special case for insertion at the head of
//the sequence.
else if (index == 0) {
listHead = new Node(o, listHead, null);
}
//In the general case, we need to chain down the linked list
//from the head until we find the location for the new
//list node. If we reach the end of the list before finding
//the specified location, we know that the given index was out
//of range and throw an exception.
else {
Node nodePointer = listHead;
int i = 1;
while (i < index) {
nodePointer = nodePointer.next;
i += 1;
if (nodePointer == null) {
throw new SequenceDLListException("Indexed Element out of Range");
}
}
Node newNode = new Node(o, nodePointer, nodePointer.next);
if (nodePointer.next != null) {
nodePointer.next.prev = newNode;
}
//Now we've found the node before the position of the
//new one, so we 'hook in' the new Node.
nodePointer.next = newNode;
//Finally we need to check that the tail pointer is
//correct. Another special case occurs if the new
//node was inserted at the end, in which case, we need
//to update the tail pointer.
if (nodePointer == listTail) {
listTail = newNode;
}
}
}
/**
* Removes the item at the start of the sequence.
*/
public void deleteFirst() throws SequenceDLListException {
//Check there is something in the sequence to delete.
if (listHead == null) {
throw new SequenceDLListException("Sequence Underflow");
}
//There is a special case when there is just one item in the
//sequence. Both pointers then need to be reset to null.
if (listHead.next == null) {
listHead = null;
listTail = null;
}
//In the general case, we just unlink the first node of the
//list.
else {
listHead = listHead.next;
listHead.prev = null;
}
}
/**
* Removes the item at the end of the sequence.
*/
public void deleteLast() throws SequenceDLListException {
//Check there is something in the sequence to delete.
if (listHead == null) {
throw new SequenceDLListException("Sequence Underflow");
}
//There is a special case when there is just one item in the
//sequence. Both pointers then need to be reset to null.
if (listHead.next == null) {
listHead = null;
listTail = null;
}
//In the general case, we need to chain all the way down the
//list in order to reset the link of the second to last
//element to null.
else {
Node nodePointer = listHead;
while (nodePointer.next != listTail) {
nodePointer = nodePointer.next;
}
//Unlink the last node and reset the tail pointer.
nodePointer.next = null;
listTail = nodePointer;
}
}
/**
* Removes the item at the specified position in the sequence.
*/
public void delete(int index) throws SequenceDLListException {
//Check there is something in the sequence to delete.
if (listHead == null) {
throw new SequenceDLListException("Sequence Underflow");
}
//Check the index is positive.
if (index < 0) {
throw new SequenceDLListException("Indexed Element out of Range");
}
//There is a special case when there is just one item in the
//sequence. Both pointers then need to be reset to null.
if (listHead.next == null) {
if (index == 0) {
listHead = null;
listTail = null;
} else {
throw new SequenceDLListException("Indexed element is out of range.");
}
}
//There is also a special case when the first element has to
//be removed.
else if (index == 0) {
deleteFirst();
}
//In the general case, we need to chain down the list to find
//the node in the indexed position.
else {
Node nodePointer = listHead;
int i = 1;
while (i < index) {
nodePointer = nodePointer.next;
i += 1;
if (nodePointer.next == null) {
throw new SequenceDLListException("Indexed Element out of Range");
}
}
//Unlink the node and reset the tail pointer if that
//node was the last one.
if (nodePointer.next == listTail) {
listTail = nodePointer;
}
//When we remove the node we have to change the reference as well!
if (nodePointer.next.next != null) {
nodePointer.next.next.prev = nodePointer;
}
nodePointer.next = nodePointer.next.next;
}
}
/**
* Returns the item at the start of the sequence.
*/
public Object first() throws SequenceDLListException {
if (listHead != null) {
return listHead.datum;
} else {
throw new SequenceDLListException("Indexed Element out of Range");
}
}
/**
* Returns the item at the end of the sequence.
*/
public Object last() throws SequenceDLListException {
if (listTail != null) {
return listTail.datum;
} else {
throw new SequenceDLListException("Indexed Element out of Range");
}
}
/**
* Returns the item at the specified position in the sequence.
*/
public Object element(int index) throws SequenceDLListException {
//Check the index is positive.
if (index < 0) {
throw new SequenceDLListException("Indexed Element out of Range");
}
//We need to chain down the list until we reach the indexed
//position
Node nodePointer = listHead;
int i = 0;
while (i < index) {
if (nodePointer.next == null) {
throw new SequenceDLListException("Indexed Element out of Range");
} else {
nodePointer = nodePointer.next;
i += 1;
}
}
return nodePointer.datum;
}
/**
* Tests whether there are any items in the sequence.
*/
public boolean empty() {
return (listHead == null);
}
/**
* Returns the number of items in the sequence.
*/
public int size() {
//Chain down the list counting the elements
Node nodePointer = listHead;
int size = 0;
while (nodePointer != null) {
size += 1;
nodePointer = nodePointer.next;
}
return size;
}
/**
* Empties the sequence.
*/
public void clear() {
listHead = null;
listTail = null;
}
}

remove method in java BST

I do have a hw question...I have to write a remove method for a binary search tree, so far what I have is below but I keep getting a bunch of errors associated with my remove method and I'm not sure why...would someone please be able to check my code. Thank You. I also tried to create a find method but I'm having some trouble with that as well...that is all the way at the bottom of my remove code.
import java.util.*;
class TreeNode383<E extends Comparable> {
private E data;
private TreeNode383<E> left;
private TreeNode383<E> right;
private TreeNode383<E> parent;
public TreeNode383( ) { left = right = parent = null; }
public TreeNode383( E d, TreeNode383 <E> l, TreeNode383 <E> r,
TreeNode383 <E> p) {
data = d;
left = l;
right = r;
parent = p;
}
public E getData( ) { return data; }
public void setData(E d) { data = d; }
public TreeNode383<E> getLeft( ) { return left; }
public void setLeft(TreeNode383<E> l) { left = l; }
public TreeNode383<E> getRight( ) { return right; }
public void setRight(TreeNode383<E> r) { right = r; }
public TreeNode383<E> getParent( ) { return parent; }
public void setParent(TreeNode383<E> p) { parent = p; }
public String toString( ) {
String answer = "";
if (left != null) answer += left.toString( );
answer += data + " ";
if (right != null) answer += right.toString( );
return answer;
}
}
**The start of my remove method**
boolean remove (E obj)
{
if(root == obj)
return false;
//when deleting a leaf just delete it
else if(obj.getleft == NULL && obj.getright == NULL)
parent = obj = NULL;
//when deleting an interior node with 1 child
//replace that node with the child
else if(obj.getleft == NULL && obj.getright != NULL)
obj.setright = new TreeNode383<E>(newData, null, null, null);
else if(obj.getleft != NULL && obj.getright == NULL
obj.setleft = new TreeNode383<E>(newData, null, null, null);
//when deleting an interior node with 2 children
//find left most node in right subtree,
//promote it to replace the deleted node
//promote its child to replace where it was
/*
private BinaryNode findMin( BinaryNode t )
{
if( t == null )
return null;
else if( t.left == null )
return t;
return findMin( t.left );
}
*/
obj is an instance of E not TreeNode383<E> so it has no getLeft() or getRight() method. And even if it did, you spelled it wrong.
And what's root? I can't see a declaration for that anywhere.
This syntax makes no sense either:
obj.setright = new TreeNode383<E>(newData, null, null, null);
setRight() is a method not a field (Java does not have properties like C#) Plus you need a capital 'R' in the name.
So maybe that should be
obj.setRight(new TreeNode383<E>(newData, null, null, null));
that is, if newData was declared, which it isn't.
There are too many errors here to make sense of your code. Try implementing one function at a time.
ya..there are some errors...basically, to remove a node N from a BST, replace N with the minimum element in the right subtree of N.

Categories

Resources