1

I'm implementing a generic AVL tree but I'm having some compilation error.

My AVL Tee has to deal with Node<T> of type T.

In the example, the type is Event class.I want to compare the node.

But each type of data has to be compared differently, for that, i'm passing the comparison to the data itself to do the comparison.

I tried to make the Node implementing the Comparable Interface and the same thing to Event Class.

My code structure looks as follow:

The Tree Structure:

public class AvlTree<T extends Comparable<T>> {

private Node<T> root;

public AvlTree() {
    root = null;
}
 //other method to insert dellete operation in the tree

    }

The node Structure:

public class Node<T extends Comparable<T>> implements Comparable<T> {
        private T data;
        private Node<T> left;
        private Node<T> right;

        public Node() {
        }
         @Override
        public int compareTo(T t) {
          return this.data.compareTo(t);
        }
     }

The event Class that will be put in a node Object:

public class Event implements Comparable<Event>{
    private Point point;
    public Event() {
    }
    @Override
    public int compareTo(Event t) {
      return 1;
    }
    }

The code that gave me a compilation error is when i'm declaring an AvlTree :

private  AvlTree<Node<Event>> treeOfEvents;

Error:

type argument Node<Event> is not within bounds of type-variable T
where T is a type-variable:
T extends Comparable<T> declared in class AvlTree

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
user3521250
  • 115
  • 3
  • 14
  • 2
    `Node> implements Comparable` should be `Node> implements Comparable>`. – Luiggi Mendoza Apr 17 '15 at 17:47
  • 1
    Also, by declaring `AvlTree>` it means the nodes in your tree will hold `Node`, you need `AvlTree` only. – Luiggi Mendoza Apr 17 '15 at 17:48
  • 1
    I think you actually want an `AvlTree`. In which case it's `Event` that needs to implement `Comparable`, not `Node`. – Paul Boddington Apr 17 '15 at 17:50
  • No i have a tree of node , and each node has a data , this data may be an event or something else – user3521250 Apr 17 '15 at 17:51
  • @user3521250 I don't know your project, obviously, but I'm 90% sure that's wrong. `java.util.LinkedList` is made up of nodes, but it's `LinkedList` not `LinkedList>`. – Paul Boddington Apr 17 '15 at 18:04
  • maybe it's a different implementation.why we can't have a tree of nodes and each node has many object.and why it's wrong? – user3521250 Apr 17 '15 at 18:21
  • As I say, I don't know your project, so I could be wrong. Is every `AvlTree` going to be made of `Node` objects or are there going to be some `AvlTree`s that aren't? – Paul Boddington Apr 17 '15 at 18:25
  • yes , every avlTree has only node element – user3521250 Apr 17 '15 at 18:26
  • hy pbabcdefp , i think you are right , i get an other error when i want to do some other stuff.may be i have to change the implementation , the probem is that i have to save the right and left child somewhere , if i use just event i have to put this information in the event and i think it's a bad idea, because i have other type that will use the AvlTree – user3521250 Apr 17 '15 at 20:03

1 Answers1

3

your Node has to implement Comparable like here:

public class Node<T extends Comparable<T>> implements Comparable<Node<T>> {

as your AvlTree expects something as generic Type which implements Comparable for exactly that type

Manuel Jain
  • 751
  • 1
  • 6
  • 16