Issue with Java Generics,collection interface and object class - java

I'm trying to create a b-tree class that implements the collection interface shown below. The Issue here is that the add method takes in an object and my node classes setleftNode takes in a Node of a generic type "T". Thus I'm getting method cant be applied to given type error when compiling. :
B-Tree Class:
public class BST<T> implements Collection<T>{
private Node<T> _root;
private Node<T> _current;
private Random _rnd = new Random();
public BST(Node<T> root) {
_root = root;
}
public Node<T> getRoot(){
return _root;
}
#Override
public Iterator iterator() {
throw new UnsupportedOperationException("Not supported yet.");
}
#Override
public boolean add(Object e) {
if (this._root != null){
if(this._root.getLeftNode() == null){
this._root.setLeftNode(e);
return true;
}
}
return false;
}
Node Class:
public class Node<T>{
private T _value;
private Node<T> _left;
private Node<T> _right;
public Node (T value){
_value = value;
}
public T getValue(){
return _value;
}
public void setLeftNode(Node<T> node){
_left= node;
}
public void setRightNode(Node<T> rNode){
_right = rNode;
}
public Node getRightNode(){
return _right;
}
public Node getLeftNode(){
return _left;
}
}

If BST<T> implements Collection<T> it should have add(T e). And inside setLeftNode(new Node<T>(e))):
#Override
public boolean add(T e) {
if (this._root != null){
if(this._root.getLeftNode() == null) {
this._root.setLeftNode(new Node<T>(e));
return true;
}
}
return false;
}

you want your BST class to work with the type T, so you should define the add method likewise
public boolean add(T e) {
...
}
BST<String> bst = new BST<String>();
bst.add(new String("Hello"))

Related

Linkedlist InsertInOrder method

This one our first class AbstractLinkedList<T>
public abstract class AbstractLinkedList<T> {
public class Node<T> {
public T value;
Node<T> next;
public Node(T value, Node<T> next) {
this.value = value;
this.next = next;
}
}
Node<T> head;
public Node<T> getHead() {
return head;
}
public void addFirst(T value) {
head = new Node<>(value, head);
}
public void addLast(T value){
if(head==null)
head = new Node<>(value, null);
else {
Node<T> node = head;
while (node.next!=null)
node = node.next;
node.next = new Node<>(value, null);
}
}
public void print(){
System.out.println(toString());
}
#Override
public String toString(){
if(head==null) return "boş";
String r="";
Node<T> node=head;
while(node!=null) {
r += node.value + (node.next!=null?" ":"");
node=node.next;
}
return r;
}
public abstract void insertInOrder(T value);
public abstract AbstractLinkedList<T> reverse();
public abstract AbstractLinkedList<T> concatenate(AbstractLinkedList<T> list);
}
This is second one Odev1LinkedList implements Comparable<T>> extends AbstractLinkedList<T>
public class Odev1LinkedList<T extends Comparable<T>> extends AbstractLinkedList<T> {
#Override
public AbstractLinkedList<T> reverse() {
Odev1LinkedList a = new Odev1LinkedList();
Node<T> root = new Node(this, head);
Node<T> iter = root;
Node<T> temp ;
a.head = null;
do {
temp = iter;
a.addFirst(temp.value);
iter = iter.next;
}while (temp !=root );
return a;
}
When I run this code example.reverse should make reverse and return LinkedList without changing ORIGINAL LinkedList.
But its return without reversing
You can't use the < operator on arbitrary objects. It's only defined for primitive numbers. Take a look into the specification.
If your object of type T implements Comparable<T> you can use head.value.compareTo(value) < 0.
If you want your objects of type T to implement Comparable<T> you have to go to your class declaration and write
public class MyLinkedList<T extends Comparable<T>> { ... }

Writing a generic iterator in Java for Binary Tree

I want to write a custom iterator for BinaryTree. This iterator should return Node<?> objects. I get compile error in file InorderIterator in lines with recursive call to fillList: fillList(currentNode.getLeft());
The error is: Error:(14, 37) java: incompatible types: rclib.Node cannot be converted to T
Can somebody explain me why my approach doesn't work? Or how to fix it
Node.java
package rclib;
public class Node<T extends Comparable<T>> {
T key;
Node<T> left;
Node<T> right;
public Node(T key, Node left, Node right) {
this.key = key;
this.left = left;
this.right = right;
}
public Node(T key) {
this(key, null, null);
}
public Node<T> getLeft() {
return left;
}
public Node<T> getRight() {
return right;
}
public T getKey() {
return key;
}
}
InorderIterator.java
package rclib;
import java.util.*;
public class InorderIterator<T extends Node<?>> implements Iterator<T> {
LinkedList<T> list;
public InorderIterator(T root) {
list = new LinkedList<T>();
fillList(root);
}
public void fillList(T currentNode) {
if (currentNode == null) return;
fillList(currentNode.getLeft());
list.add(currentNode);
fillList(currentNode.getRight());
}
#Override
public boolean hasNext() {
return !list.isEmpty();
}
#Override
public T next() {
return list.removeFirst();
}
}
AVLTree.java
package rclib;
public class AVLTree<T extends Comparable<T>> implements Iterable<Node<T>>{
private Node<T> root;
#Override
public Iterator<Node<T>> iterator() {
return new InorderIterator<Node<T>>(root);
}
}
You should perhaps do something like that:
package rclib;
import java.util.*;
public class InorderIterator<T extends Comparable<T>> implements Iterator<Node<T>> {
LinkedList<Node<T>> list;
public InorderIterator(Node<T> root) {
list = new LinkedList<Node<T>>();
fillList(root);
}
public void fillList(Node<T> currentNode) {
if (currentNode == null) return;
fillList(currentNode.getLeft());
list.add(currentNode);
fillList(currentNode.getRight());
}
#Override
public boolean hasNext() {
return !list.isEmpty();
}
#Override
public Node<T> next() {
return list.removeFirst();
}
}
You have to explicitly specify that you use Node and not ? extends Node which might finally be not qualified for the correct usage.
public static class InorderIterator<T extends Comparable<T>> implements Iterator<Node<T>> {
LinkedList<Node<T>> list;
public InorderIterator(Node<T> root) {
list = new LinkedList<>();
fillList(root);
}
public void fillList(Node<T> currentNode) {
if (currentNode == null) return;
fillList(currentNode.getLeft());
list.add(currentNode);
fillList(currentNode.getRight());
}
#Override
public boolean hasNext() {
return !list.isEmpty();
}
#Override
public Node<T> next() {
return list.removeFirst();
}
}

The type Lista is not generic; My list is correct?

Hi i having some trouble with: error
The type Lista is not generic; it cannot be parameterized with arguments "Integer"
My files tests:
public class testy {
public static void main(String[] args) {
Lista<Integer> l = new Lista<Integer>();
l.add(5);
l.add(7);
//System.out.println(l.removeFirst());
}
}
List :
public class Lista implements Serializable{
ListaS<T> Lista;
public Lista() {
Lista = new ListaS<T>(null, Lista, Lista);
Lista.setNext(Lista);
Lista.setPrevious(Lista);
}
public int Rozmiar(){
int counter = 0;
if(nastepny == null)
return counter;
else{
ListaS<T> tmp = counter;
do{
next = next.wezNastepny();
counter++;
}while(tmp != previous);
}
return counter;
}
public boolean Empty(){
if(Size()== 0 )
return true;
else
return false;
}
public void addStart(T wartosc){
ListaS<T> nowy = new ListaS<T>(wartosc);
if(next == null){
next=newnode;
next=newnode;
nowy.setNext(null);
nowy.setPrevious(null);
}else{
nowy.setNext(nastepny);
next.setPrevious(nowy);
next = nowy;
}
}
public void addEnd(T wartosc){
ListS<T> nowy = new ListaS<T>(value);
if(previous == null){
next=nowy;
previous =nowy;
newnode.setNext(null);
newnode.setPrevious(null);
}else{
newnode.setPrevious(previous);
previous.setNext(nowy);
previous=nowy;
}
public E removeFirst() {
if (size == 0) throw new NoSuchElementException();
ListS tmp = next;
next = next.getNext();
next.setNext = null;
System.out.println("deleted: "+tmp.value);
return tmp.wezWartosc();
}
}
And the last one
public class ListaS<T> implements Serializable{
T value;
ListaS<T> next;
ListaS<T> previous;
ListaS(T value, ListaS<T> next, ListaS<T> poprzedni) {
this.value = value;
this.next = next;
this.previous = previous;
}
T getValue() {
return value;
}
public void setNext(ListaS<T> nastepny) {
this.next = next;
}
public ListaS<T> getPrevious() {
return previous;
}
public void setPrevious(ListaS<T> poprzedni) {
this.previous = previous;
}
public void setValue(T value) {
this.value = value;
}
public ListaS<T> getNext() {
return next;
}
}
Ok if we solve first problem could some one tell me my function are ok?
And how i should write removeLast()?
Just convert your Lista class definition in public class Lista<T> implements Serializable

JAVA - Polymorphism trouble

As a homework I'm creating a SingleLinkedNode class that implements a Node interface.
Node Interface
public interface Node <T> {
static final String NULL_NODE_ERROR = "Node can't be null";
public void setNextNode(Node nextNode) throws NullPointerException;
public void setIndex(int index);
public int getIndex();
public T getData();
public Node getNextNode();
}
SingleLinkedNode class
public class SingleLinkedNode<T> implements Node<T> {
private int index;
private T data;
private SingleLinkedNode nextNode;
public SingleLinkedNode() {
}
public SingleLinkedNode(int index) {
this.index = index;
}
public SingleLinkedNode(int index, SingleLinkedNode nextNode) {
this.index = index;
this.nextNode = nextNode;
}
#Override
public void setNextNode(SingleLinkedNode nextNode) throws NullPointerException{
if (nextNode == null){
throw new NullPointerException(NULL_NODE_ERROR);
}
this.nextNode = nextNode;
}
#Override
public void setIndex (int index){
this.index = index;
}
#Override
public int getIndex() {
return this.index;
}
#Override
public T getData() {
return this.data;
}
#Override
public SingleLinkedNode getNextNode() {
return this.nextNode;
}
}
In the setNextNode(SingleLinkedNode nextNode) method of the class it says that I'm not implementing the setNextNode(Node nextNode) method of the interface even though SingleLinkedNode implements Node.
But if I leave the class method exactly as the interface one it says that Node can't be converted to SingleLinkedNode, pretty obvious.
Is there any right way of overriding this method and being sure that the method only accepts SingleLinkedNode objects as arguments?
I thought about casting but I'm not sure if that the right way.
Thank you all the solution was as follows
#Override
public void setNextNode(Node nextNode) throws NullPointerException, IllegalArgumentException{
if (nextNode == null){
throw new NullPointerException(NULL_NODE_ERROR);
}
if ( ! (nextNode instanceof SingleLinkedNode) ) {
throw new IllegalArgumentException ("Node is not SingleLinkedNode");
}
this.nextNode = (SingleLinkedNode) nextNode;
}

How to implements Iterable

In my program, I write my own LinkedList class. And an instance, llist.
To use it in foreach loop as following, LinkedList needs to implement Iterable?
for(Node node : llist) {
System.out.print(node.getData() + " ");
}
Here following is my LinkedList class. Please let me know how can I make it Iterable?
public class LinkedList implements Iterable {
private Node head = null;
private int length = 0;
public LinkedList() {
this.head = null;
this.length = 0;
}
LinkedList (Node head) {
this.head = head;
this.length = 1;
}
LinkedList (LinkedList ll) {
this.head = ll.getHead();
this.length = ll.getLength();
}
public void appendToTail(int d) {
...
}
public void appendToTail(Node node) {
...
}
public void deleteOne(int d) {
...
}
public void deleteAll(int d){
...
}
public void display() {
...
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public boolean isEmpty() {
if(this.length == 0)
return true;
return false;
}
}
Implement the only method of the Iterable interface, iterator().
You will need to return an instance of Iterator in this method. Typically this is done by creating an inner class that implements Iterator, and implementing iterator by creating an instance of that inner class and returning it.

Categories