首页 > 解决方案 > Java中的两层通用结构

问题描述

我想让一个二叉树将节点保持为堆,以便我尝试在泛型中创建一个泛型,但我遇到了一些问题。

1-)我想不出两层通用在Java中是否可行。

2-)我将树添加为数字,但是当我尝试返回节点时,我现在没有分配什么,如果可以返回,我可以达到它的方法。

二叉搜索树堆树

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

}

二叉搜索树

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

public class Heap<E extends Comparable<E>> implements Comparable<Heap<E>>{

   private E[] heapData;
}

标签: javagenericsdata-structuresbinary-search-treeheap

解决方案


推荐阅读