I want to make a binary tree keep node as heap so that
I try to make a generic in generic but I got some problem about it.
1-)I could not figure out two layer generic is it possible in Java.
2-)I adding tree as number but when I try to return node I dont now assign what and can I reach it methods if it can be return .
Binary Search Tree Heap Tree
public class BSTHeapTree <E extends Comparable<? extends Comparable<?>>>{
BinarySearchTree<E<T>> root; //error
public BSTHeapTree() {
root = new BinarySearchTree<>(); // error
}
int add(E _data) {
= root.getRoot(); //Assign what
return 0;
}
}
Binary Search Tree
public class BinarySearchTree<E extends Comparable<E>>{
private Node<E> head;
public BinarySearchTree() {
head = null;
}
public Node<E> getRoot() {
return head;
}
private static class Node<E extends Comparable<E>>{
E data;
Node<E> lBranch;
Node<E> rBranch;
}
}
Heap
public class Heap<E extends Comparable<E>> implements Comparable<Heap<E>>{
private E[] heapData;
}
Related
I am designing some java objects to represent graphs and trees. For my use case I will be using both data types but I also want my graph algorithms to work on my trees.
import java.util.List;
public interface Node<T> {
T getValue();
List<? extends Node<T>> getNeighbors();
void addNodes(List<? extends Node<T>> nodes);
}
public interface TreeNode<T> extends Node<T> {
List<? extends TreeNode<T>> getChildren();
void addChildren(List<? extends TreeNode<T>> treeNodes);
#Override
default List<? extends Node<T>> getNeighbors() {
return getChildren();
}
#Override
default void addNodes(List<? extends Node<T>> nodes) {
if(nodes.getClass().isInstance(getChildren().getClass())) {
addChildren((List<? extends TreeNode<T>>) nodes);
} else {
throw new RuntimeException("Type error!");
}
}
}
My question is about how I'm dealing with addNodes method in the Node interface in the TreeNode interface. The addNodes method has to be in the Node interface because I want to allow people to write code that can add nodes to graphs. However, I also don't want people to add arbitrary nodes to a tree node(for example adding a graph node to a tree node).
In order to prevent this, I'm checking the type of nodes at runtime and throwing an exception if the type is not right. I'm just wondering if this is the best way to accomplish what I want or if there is a better practice?
Thanks for helping :)
If I understand correctly, what you want is (a variation on) the so-called curiously recurring template pattern. The Node type needs to be parameterized not only by its payload type (T) but also by the type of nodes it can be used with. So you want something like:
public interface Node<T, N extends Node<T, N>> {
T getValue();
List<N> getNeighbors();
void addNodes(List<N> nodes);
}
public interface TreeNode<T> extends Node<T, TreeNode<T>> {
List<TreeNode<T>> getChildren();
void addChildren(List<TreeNode<T>> treeNodes);
#Override
default List<TreeNode<T>> getNeighbors() {
return getChildren();
}
#Override
default void addNodes(List<TreeNode<T>> nodes) {
addChildren(nodes);
}
}
Demo (shows compilation only): https://ideone.com/44qrmX
The way I see it, Node is a container for some data. Tree and Graph are two ways to maintain relationships between Nodes. So perhaps three classes should be defined:
import java.util.List;
public class Node<T> {
private T value;
public Node(T value) { this.value = value; }
T getValue() { return value; }
}
public abstract class Tree<T> {
private Node<T> root;
public abstract List<? extends Node<T>> getChildren();
public abstract void addChildren(List<? extends Node<T>> nodes);
public Tree(Node<T> root) { this.root = root; }
}
public abstract class Graph<T> {
private Node<T> root;
public abstract List<? extends Node<T>> getNeighbors();
public abstract void addNeighbors(List<? extends Node<T>> nodes);
public Graph(Node<T> root) { this.root = root; }
}
EDIT: If you want to have shared traversal algorithms, you can put them in separate classes and have the Tree and Graph use similar semantics like this:
// common semantics for node containers
public interface NodeContainer<T> {
List<? extends Node<T>> getRelatedNodes();
}
public abstract class Tree<T> implements NodeContainer<T> {
... // same as above
#Override
public List<? extends Node<T>> getRelatedNodes() {
return getChildren();
}
}
public class NodeContainerTraversal {
public void bfs (NodeContainer<?> container) {
...
}
}
I am writing an iterator inner class that iterates through a list. Besides the remove method, I believe I have implemented all the methods of iterator correctly but I get an error saying "Bound mismatch: The type E is not a valid substitute for the bounded parameter > of the type List.Node". I believe this has to with having Node> implements Iterable at the top of my code but I do not want to change that if unneeded. Any possible suggestions on what I should do?
public class List<T extends Comparable<L>> implements Iterable<L> {
private class Node<N extends Comparable<N>> {
private N data;
private Node<N> next;
}
protected Node<L> head;
public Iterator<L> iterator() {
return new ListIterator<L>();
}
public class ListIterator<E extends Comparable<E>> implements Iterator<E> {
private Node<E> N = (Node<E>) head; //"Bound mismatch: The type E is not a valid substitute for the bounded parameter <D extends Comparable<D>> of the type List<T>.Node<D>"
public boolean hasNext() {
return (N.next != null);
}
public E next() {
if (!hasNext())
throw new NoSuchElementException();
else {
N = N.next;
return N.data;
}
}
public void remove() {
}
}
}
You should reduce the number of generic types. Because the inner classes know the generic type of their parent class, you should simplify the Node and ListIterator class:
public class List<L extends Comparable<L>> implements Iterable<L> {
private class Node {
private L data;
private Node next;
}
protected Node head;
public Iterator<L> iterator() {
return new ListIterator();
}
public class ListIterator implements Iterator<L> {
private Node N = head;
public boolean hasNext() {
return (N.next != null);
}
public L next() {
if (!hasNext())
throw new NoSuchElementException();
else {
N = N.next;
return N.data;
}
}
public void remove() {
}
}
}
The type parameter N is declared as
N extends Comparable<N>
ie. it has bounds. It must be Comparable to itself.
The type parameter E is declared as
E
ie. it has no bounds. It can be any type, but not necessarily a type that is Comparable to itself.
Therefore, you can't use E where an N is expected. Consider adding the same bounds as N to E.
I try to make a generic linked list.
The node of this linked list using <T extends Comparable <T>>. But when I use
LList<LList<Integer>> linkedlist = new LList<LList<Integer>>();
to create a new instance, there is an error:
Multiple markers at this line
- Bound mismatch: The type LList<Integer> is not a valid substitute
for the bounded parameter <T extends Comparable<T>> of the type LList<T>
- Bound mismatch: The type LList<Integer> is not a valid substitute
for the bounded parameter <T extends Comparable<T>> of the type
How do I fix this?
Node class:
public class Node <T extends Comparable <T>> {
// Members:
public T data;
public Node <T> next;
// Methods:
public Node () {
data =null;
next = null;
}
public Node (T data) {
this.data = data;
next = null;
}
}
LList class:
public class LList <T extends Comparable <T>> {
// Members:
public Node <T> head;
// Methods:
public LList () {
head = null;
}
// Add node.
public void addNode (T data) {
if (head == null) {
head = new Node <T> (data);
return;
}
Node <T> newNode = new Node <T> (data);
Node <T> tempNode = head;
while (tempNode.next != null) tempNode = tempNode.next;
tempNode.next = newNode;
}
// Show linked list.
public void showLLForInteger () {
if (head == null) return;
Node <T> tempNode = head;
while (tempNode != null) {
System.out.print(String.format("%-6d", tempNode.data));
tempNode = tempNode.next;
}
System.out.println();
}
}
Why are you requiring that T extends Comparable<T>? You do not seem to be using that anywhere.
Since you require that T extends Comparable<T>, that means the parameter of the list must be comparable to itself. LList<LList<Integer>> doesn't work because LList<Integer> is not comparable to itself (it does not extend Comparable<LList<Integer>>). Are you sure you don't just want LList<Integer>?
LList<T extends Comparable>
so LList accepts only classes that extends Comparable as type parameter.
LList <LList<Integer>> subArrsList = new LList <LList<Integer>>();
in this statement you are giving LList<Integer> class as type parameter. LList<Integer> does not extend Comparable.
Integer extends comparable but you are not using the Integer class as type parameter, You are using LList which does not extends Comparable.
So you are getting an error.
Change your LList class as follows:
public class LList <T extends Comparable <T>> implements Comparable<LList<T>>{
#Override public int compareTo(LList<T> list) {
//to do
}
...// your code
}
I am new to Java and trying to implement MyLinkedList which extends the GeneralList interface, I want to use the comparable interface for my Node to keep my list sorted,
When I try to create a head node it is giving me errors
Please find the error message below the following code
//List interface
public interface GeneralList<T>
{
public boolean addNode(T elem);
public boolean deleteNode(T elem);
public T containsNode(T elem);
public void printSll();
}
//ListImplementation
public class SLL2<T> implements GeneralList<T>
{
private static class Node<T extends Comparable<T>>
{
public T data;
public Node<T> next;
public Node()
{
data = null;
next = null;
}
}
public SLL2()
{
head = null;
}
/* 1. Error while creating a head referance*/
private Node<T> head;
#Override
public boolean addNode(T elem)
{
Node<T> tmp = new Node<T>();
tmp.data = elem;
if(head == null)
{
head = temp;
return true;
}
else
{
for(Node<T> cur = head; cur.next != null ; cur= cur.next)
{
/* iterate and add the node */
if(temp.elem.comparTo(cur.elem))
{
}
}
}
}
1. I am not able to create the head node with the declaration private Node<T> head;
It is giving error "Bound mismatch: The type T is not a valid substitute for the bounded parameter <T extends Comparable<T>> of the type SLL2<T>.Node<T>"
Please help me to resolve this error...
Your class SLL2<T> should also have a constraint about comparability of T. Like that:
public class SLL2<T extends Comparable<T>> implements GeneralList<T> {
// ...
I have the following design:
I have an Abstract class Instance,
I have a class Library that extends Instance and
I have a class File that also extends Instance
I've created my own linked list implementation and it's defined as follows:
public class List<T extends Instance> implements Iterable {
//some other code here
public Iterator iterator(){
return new ListIterator(this);
}
now I've created a class
public class ListIterator<T extends Instance> implements Iterator<T> {
private List thisList;
private Node current;
public ListIterator(List l){
thisList=l;
current=thisList.head.next;
}
#Override
public boolean hasNext() {
if(current==null)
return false;
return false;
}
#Override
public T next() {
Node temp=current;
current=current.next;
return temp.data;
}
}
Where Node is
public class Node<T extends Instance> {
public Node<T> next;
public Node<T> prev;
public T data;
public Node(T data,Node prev, Node next){
this.data=data;
this.prev=prev;
this.next=next;
}
}
so my problem is as follows: the line return temp.data rises an error:
Type mismatch - cannot convert from Instance to T.
What is wrong with this code?
I'd say that Node.data is a reference to an Instance object? If that is the case, the compiler can't automatically change an Instance to a T, because even though T is an Instance object (T extends Instance), any given Instance might not be a T.
The Java Generics tutorial explains it: http://docs.oracle.com/javase/tutorial/extra/generics/subtype.html
Also, in your List<T> class, you should be specifying Iterator and ListIterator as generic using Iterator<T> and ListIterator<T>, or else the compiler won't be able to handle the generics properly. Your Node reference also needs to be generic: Node<T>
Hence you should be using
private Node<T> current;
and
public T next() {
Node<T> temp=current;
current=current.next;
return temp.data;
}
The compiler will usually warn you when you're using a raw type for a generic class.
Did no one notice the bug:
public boolean hasNext() {
if(current==null)
return false;
return false;
}
This is an invariant. Unless I am missing something, the iterator will very quickly return 0 elements!