name clash when trying to overwrite method - java

this is my first post here, but I need help trying to figure out why my child class will not let me override the parent method. I was tasked to create a generic Binary Search Tree with some functionality. The next task was to create a generic AVL Tree and I decided to extend it from my custom Binary Search Tree to reuse code and simply add the rotation required to make it functional. However when trying to overwrite the method, I keep getting a name clash error. And if I get rid of my Comparable interface extension in my AVLTree class and make my insert method generic, I get an argument mismatch error saying T cannot be converted to Comparable. This is where I am stuck at, if anyone can offer any type of input it would be much appreciated.
public class BinaryTree <T extends Comparable<T>>{
Node Root;
public BinaryTree(){
this.Root = null;
}
public boolean isEmpty(){
return this.Root == null;
}
public void insert(T data){
this.insert(data, this.Root);
}
protected void insert(T data, Node<T> n){
if(this.Root == null){
this.Root = new Node(data);
}
if(data.equals(n.getData())){
n.occurances++;
}
else if(data.compareTo(n.data) < 0){
if(n.left != null)
insert(data, n.left);
else
n.left = new Node(data);
}
else if(data.compareTo(n.data) > 0){
if(n.right != null)
insert(data, n.right);
else
n.right = new Node(data);
}
}
First attempt:
public class AVLTree <T extends Comparable<T>> extends BinaryTree{
private static final int ALLOWED_IMBALANCE = 1;
public void insert(T data){
this.insert(data, this.Root);
}
Second attempt:
public class AVLTree extends BinaryTree{
private static final int ALLOWED_IMBALANCE = 1;
public <T>void insert(T data){
this.insert(data, this.Root);
}
error for overwriting insert method

Try passing the type parameter of BinaryTree:
// vvv
public class AVLTree <T extends Comparable<T>> extends BinaryTree<T> {
private static final int ALLOWED_IMBALANCE = 1;
#Override
public void insert(T data){
this.insert(data, this.Root);
}
}

Related

Two Layer Generic Structure in Java

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;
}

Generic in Java throwing errors when methods are defined

I am learning Generics in Java. For that, I tried out a simple LinkedList like that.
class Node {
private int age;
private String name;
private Node next;
public Node(int age, String name) {
this.age = age;
this.name = name;
this.next = null;
}
public int getAge() {
return this.age;
}
public String getName() {
return this.name;
}
public Node getNext() {
return this.next;
}
public void setNext(Node next) {
this.next = next;
}
}
class LinkedList<T> {
private T head;
private T current;
public LinkedList() {
head = null;
current = null;
}
public void append(T x) {
if (head == null) {
head = x;
current = x;
}
else {
current.setNext(x);
current = x;
}
}
public T getAt(int index) {
T ptr = head;
for(int i = 0; i < index; i++) {
ptr = ptr.getNext();
}
return ptr;
}
}
class Main {
public static void main(String[] args) {
LinkedList<Node> list = new LinkedList<Node>();
list.append(new Node(39, "John"));
list.append(new Node(43, "Josh"));
Node x = list.getAt(1);
System.out.println(String.format("%d, %s", x.getAge(), x.getName()));
}
}
But I get this error, while all the methods do exist in the Node class. What mistake am I doing?
LinkedList.java:16: error: cannot find symbol
current.setNext(x);
^
symbol: method setNext(T)
location: variable current of type T
where T is a type-variable:
T extends Object declared in class LinkedList
LinkedList.java:24: error: cannot find symbol
ptr = ptr.getNext();
^
symbol: method getNext()
location: variable ptr of type T
where T is a type-variable:
T extends Object declared in class LinkedList
2 errors
If current is of type T, you can't call methods of the Node class (such as setNext()) on current, since T can be substituted by any class when you instantiate your LinkedList.
Your Node class shouldn't be the generic type argument of LinkedList. A LinkedList should always be made of Nodes. The type of the data stored in each Node should be a generic type.
class Node<T> {
private T data;
private Node next;
public Node(T data) {
this.data = data;
this.next = null;
}
}
And the LinkedList should contain Node<T> nodes:
class LinkedList<T> {
private Node<T> head;
private Node<T> current;
}
Compiler is unable to understand T type. You have used t.setNext() , however it is isn't present in T definition unless actually used. I might sound a bit confusing here but try this:
Create an Interface Contract having setNext and getNext method.
Implement Node extending above interface. Node implements Contract.
In Linked List change generics to T extends Contract
There is no hasNext for any given generic T, so the code doesn't compile
You'd have to make sure that the LinkedList only holds Node classes or its subtypes
class LinkedList<T extends Node>
But note: That T is not the same as the generic stored within the nodes, so this seems better
class LinkedList<T> {
Node<T> head;
private T current; is a generic type and you are calling setNext and getNext on it. How come T know that it always have these methods? That's the reason it's not working.
Hence, you need to ensure that your generic type T knows that it has setNext and getNext methods.
Hence the fix is:
T extends NodeAbstract in class definition where NodeAbstract is the interface declaring signature of these methods. now this ensures that anything T gets is always going to have these two methods.
You must make Node<T> and LinkedList<Node>.
public void append(T x) { // Here x is of Type T
if (head == null) {
head = x;
current = x; //Assigning x to current, current is also of Type T
}
else {
current.setNext(x); // T.setNext is not defined! Because T can be a String/Integer/whatever. They do not have setNext method
current = x;
}
}

generic type scope does not apply to inner class?

I have the following code where type T extends Comparable, but I get a compile error when I try to do
root.node.compareTo(min).
Saying compareTo does not apply. It seems generic type scope does not apply to inner class?
public class Tree<T extends Comparable> {
public class Node<T>{
private T node;
private Node<T> left;
private Node<T> right;
public Node(T node, Node<T> left, Node<T> right) {
super();
this.node = node;
this.left = left;
this.right = right;
}
}
public boolean isBalanced(Node<T> root){
return isBalanced(root, Integer.MIN, Integer.MAX);
}
private boolean isBalanced(Node<T> root, T min, T max){
if(root == null){
return true;
}
if(root.node.compareTo(min) < 0 || root.node.compareTo(max) > 0){
return false;
}
return isBalanced(root.left, min, root.node) || isBalanced(root.right, root.node, max);
}
}
When you say public class Node<T>, you're shadowing the <T> from the outer class. You should either remove the <T> from the inner class declaration or, better, make the inner class a static nested class (since there's no inherent reason the node needs to know about the tree it's attached to).
(Note: Also use <T extends Comparable<T>>.)

How do I implement a list of lists which extends Comparable?

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
}

comparable implementation in Java

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> {
// ...

Categories