Generic Binary Search Tree Implementation in Java - calling generic methods, and cast exceptions

confundido :

I am attempting to implement a Binary Search Tree as an assignment. We are experiencing resolving issues of derivation, inheritance, etc, so the tree can potentially hold data of a variety of different types.

In past assignments I had used base class pointers as the data type, and utilized RTTI/upcasting/downcasting to access data, I read that this is inefficient and bad practice, so I'm trying to move away from it.

To that end, I'm trying to implement a generic node/tree structure, but am having a couple of issues. First, when it comes to calling a "display" method from a generic data member, is that even possible? Since the node may not know what type of data is being stored, or if the method even exists to be called?

Second, when trying to cast an object to I get an unchecked cast exception, is there a way to resolve this? Or conduct a checked cast?

Any advice or helpful solutions would be appreciated.

EDIT: Here is some code... BSTNode implementation:

/**
 * Class declaration for a BST Node. Holds left and right pointers, and a data
 * member. Contains methods for getting/setting left, right, and data.
 */
public class BSTNode<T extends Comparable<T>> {

    private BSTNode<T> left, right, next; //ptr for left/right/next nodes.
    private T data; //Integer for data.

    public BSTNode(){
        left = right = next = null; //Set left/right/next ptrs to NULL.
    }

    public BSTNode(T n){
        left = right = next = null; //Sets left/right/next ptr to NULL.
        this.data = n; //Init data to passed in arg.
    }

    public BSTNode<T> getLeft() {return this.left;}

    public BSTNode<T> getRight() {return this.right;}

    public BSTNode<T> getNext() {return this.next;}

    public T getData() {return this.data;}

    public void setLeft(BSTNode<T> left) {this.left = left;}

    public void setRight(BSTNode<T> right) {this.right = right;}

    public void setNext(BSTNode<T> n) {this.next = n;}

    public void setData(T d) {this.data = d;}
}

Insertion:

public class Interface<T extends Comparable<T>> extends Utility {

...
        case 1:
          Subjob sub1 = new Subjob("Test");
          BSTNode<T> temp = new BSTNode<T>(sub1); //ERROR ON THIS LINE
          exit = true;
          return 1;
Solubris :

Sounds like an interface should be used as the basis for the BST. The interface would need to be comparable (as required by the BST) and provided the method display(). Then it is up to implementations to provide that method properly.

The interface might look like:

public interface Displayable<T> extends Comparable<T> {
    void display();
}

Internally to the BST, it might have a node as follows:

public class Node<T> {
    Displayable<T> value;
    Node<T> left;
    Node<T> right;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generic Binary Search - JAVA -

From Dev

Java Generic Binary Search Tree cannot compare T types

From Dev

Generic Binary Search Tree Java type argument is not within bounds

From Dev

Is Valid Generic Binary Search Tree with Recursion

From Java

Binary Search Tree - Java Implementation

From Dev

Compiler error with calling Generic methods java

From Dev

Writing a generic iterator in Java for Binary Tree

From Java

Calling static generic methods

From Dev

Does the implementation of the .NET Generic.Dictionary use a balanced binary tree?

From Java

Generic binary search tree isn't adding new nodes correctly (Java)

From Dev

Java Making a binary search tree generic (it works but I know I'm not doing it right)

From Dev

Generic binary search in C

From Dev

remove method implementation for binary search tree in Java?

From Dev

Binary search tree implementation in java with traversal

From Dev

ArrayindexOutOfBoundsException with Optimal Binary Search Tree implementation java

From Dev

Binary Search Tree, In-order transversal return generic array

From Dev

Binary Search Tree find max node and delete it (Generic classes)

From Java

Java generic methods cast to parameter type at runtime, is it possible?

From Dev

Syntax shortcut for calling generic methods

From Java

Traversing through a generic binary tree

From Java

Java Generics - calling specific methods from generic-typed ones

From Java

Generic Iterator implementation in java

From Dev

A generic MergeSort implementation in java

From Dev

Java generic factory implementation

From Dev

Binary search in list with generic elements

From Java

Java Overriding Generic Methods

From Java

Java: Generic methods and numbers

From Java

Invoking Java Generic Methods

From Dev

Binary tree in Java implementation

Related Related

  1. 1

    Generic Binary Search - JAVA -

  2. 2

    Java Generic Binary Search Tree cannot compare T types

  3. 3

    Generic Binary Search Tree Java type argument is not within bounds

  4. 4

    Is Valid Generic Binary Search Tree with Recursion

  5. 5

    Binary Search Tree - Java Implementation

  6. 6

    Compiler error with calling Generic methods java

  7. 7

    Writing a generic iterator in Java for Binary Tree

  8. 8

    Calling static generic methods

  9. 9

    Does the implementation of the .NET Generic.Dictionary use a balanced binary tree?

  10. 10

    Generic binary search tree isn't adding new nodes correctly (Java)

  11. 11

    Java Making a binary search tree generic (it works but I know I'm not doing it right)

  12. 12

    Generic binary search in C

  13. 13

    remove method implementation for binary search tree in Java?

  14. 14

    Binary search tree implementation in java with traversal

  15. 15

    ArrayindexOutOfBoundsException with Optimal Binary Search Tree implementation java

  16. 16

    Binary Search Tree, In-order transversal return generic array

  17. 17

    Binary Search Tree find max node and delete it (Generic classes)

  18. 18

    Java generic methods cast to parameter type at runtime, is it possible?

  19. 19

    Syntax shortcut for calling generic methods

  20. 20

    Traversing through a generic binary tree

  21. 21

    Java Generics - calling specific methods from generic-typed ones

  22. 22

    Generic Iterator implementation in java

  23. 23

    A generic MergeSort implementation in java

  24. 24

    Java generic factory implementation

  25. 25

    Binary search in list with generic elements

  26. 26

    Java Overriding Generic Methods

  27. 27

    Java: Generic methods and numbers

  28. 28

    Invoking Java Generic Methods

  29. 29

    Binary tree in Java implementation

HotTag

Archive