【发布时间】:2021-10-30 01:14:42
【问题描述】:
我需要构建一个异构(不同类型的元素)BST,并且能够对元素进行排序,但我不知道如何解决这个问题。
我这里有二叉树代码。
This is the node class
public class Node<T> {
T data;
Node<T> left;
Node<T> right;
Node(T data) {
this.data = data;
left = null;
right = null;
}
}
这是树类。
public class Tree<T extends Comparable<T>> {
private Node<T> root;
StringBuilder result = new StringBuilder();
public Tree() {
root = null;
}
public Node<T> getRoot() {
return root;
}
/**
* Method that inserts nodes into the binary tree. If the tree is empty , a new root node is
* initialized.
*
* @param root A node object.
* @param dataBeingInserted The object to be inserted on the tree.
* @return The root node object
*/
private Node<T> insertNode(Node<T> root, T dataBeingInserted) {
if (root == null) {
root = new Node<>(dataBeingInserted);
return root;
}
if (dataBeingInserted.compareTo(root.data) < 0) {
root.left = insertNode(root.left, dataBeingInserted);
} else if (dataBeingInserted.compareTo(root.data) > 0) {
root.right = insertNode(root.right, dataBeingInserted);
}
return root;
}
public void insertNode(T dataBeingInserted) {
root = insertNode(root, dataBeingInserted);
}
/**
* Method that recursively searches for our element through the tree. If the value is present in
* the root node , or there aren't any nodes in the tree , the method returns the root node. If
* the value we're looking for is smaller than the root node's value , we search for our value in
* the left subtree , otherwise we search for it in the right subtree.
*
* @param root A node object.
* @param dataBeingSearched User's value.
* @return Recursive call of the method.
*/
private Node<T> searchTree(Node<T> root, T dataBeingSearched) {
if (root == null || dataBeingSearched.compareTo(root.data) == 0) {
return root;
}
if ((dataBeingSearched.compareTo(root.data) > 0)) {
return searchTree(root.left, dataBeingSearched);
}
return searchTree(root.right, dataBeingSearched);
}
public Node searchTree(T dataBeingSearched) {
return searchTree(root, dataBeingSearched);
}
/**
* An implementation of the In-order traversal. First the left subtree is visited and printed
* accordingly, then we visit and print the root and after that we visit and print the right
* subtree.
*
* @param root The root node object.
*/
private String inorderTraversal(Node root) {
if (root == null) {
return "";
}
inorderTraversal(root.left);
result.append(root.data).append(" ");
inorderTraversal(root.right);
return result.toString();
}
public void inorderTraversal() {
inorderTraversal(root);
}
}
我的树现在的问题是,每当任何元素与 root 不同时,我都会收到 ClassCastException ,因为发生的情况是 root 定义了树的类型,我无法修复它。
附言 这是给我错误的sn-p(为方便起见,我将发布整个主要方法。)
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Scanner;
public class Main {
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
Tree tree = new Tree<>();
tree.insertNode(50);
tree.insertNode("30");
tree.insertNode('b');
tree.insertNode(69.3);
tree.inorderTraversal();
LOGGER.info("{}", tree.result);
}
}
例如,第一个插入是一个 Integer ,之后我尝试插入一个 String 并且在那里它给了我 ClassCastException ,说 String 与 Integer 无法比较。
【问题讨论】:
-
你能添加一个产生上述
ClassCastException的sn-p吗? -
@Izruo 你去。 :)
-
public class Tree<T extends Comparable<T>>没有为异构树定义类。它为每个 T 类型的树定义一个类。 -
@Stef 是的,所以我理解它的方式是泛型类型允许我构建任何类型的树,但是树中的多种类型仍然看不见,我想不出办法不幸的是,要实现这一点。 (我还是个编程初学者)
-
现在你已经定义了类 Tree
,我想你可以创建一个类 Tree
标签: java data-structures binary-search-tree