我还在学习,试图从保加利亚的java书中运行代码,但它无效。有人可以看看吗?
public class BinaryTree {
public static class BinaryTreeNode<T> {
private T value;
private boolean hasParent;
private BinaryTreeNode<T> leftChild;
private BinaryTreeNode<T> rightChild;
/**
* Constructs a binary tree node.
*
* @param value - the value of the node.
* @param leftChild - the left child of the node.
* @param rightChild - the right child of the node.
*/
public BinaryTreeNode(T value,
BinaryTreeNode<T> leftChild,
BinaryTreeNode<T> rightChild) {
if (value == null) {
throw new IllegalArgumentException(
"Cannot insert null value!");
}
this.value = value;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
/**
* Constructs a binary tree node with no children.
*
* @param value - the value of the node.
*/
public BinaryTreeNode(T value) {
this(value, null, null);
}
public T getValue() {
return this.value;
}
public void setValue(T value) {
this.value = value;
}
public BinaryTreeNode<T> getLeftChild() {
return this.leftChild;
}
public void setLeftChild(BinaryTreeNode<T> value) {
if (value == null || value.hasParent) {
throw new IllegalArgumentException();
}
value.hasParent = true;
this.leftChild = value;
}
public BinaryTreeNode<T> getRightChild() {
return this.rightChild;
}
public void setRightChild(BinaryTreeNode<T> value) {
if (value == null || value.hasParent) {
throw new IllegalArgumentException();
}
value.hasParent = true;
this.rightChild = value;
}
}
// The root of the tree
private BinaryTreeNode<T> root;
/**
* Constructs the tree.
* @param value - the value of the node.
* @param children - the children of the node.
*/
public BinaryTree(T value, BinaryTree<T> leftChild,
BinaryTree<T> rightChild) {
if (value == null) {
throw new IllegalArgumentException();
}
BinaryTreeNode<T> leftChildNode =
leftChild != null ? leftChild.root : null;
BinaryTreeNode<T> rightChildNode =
rightChild != null ? rightChild.root : null;
this.root = new BinaryTreeNode<T>(
value, leftChildNode, rightChildNode);
}
/**
* Constructs the tree.
* @param value - the value of the node.
*/
public BinaryTreeNode<T> getRoot() {
return this.root;
}
/**
* @return the left child of the root.
*/
public BinaryTreeNode<T> getLeftChildNode() {
if (this.root != null) {
return this.root.getLeftChild();
}
return null;
}
/**
* Traverses binary tree in pre-order manner.
* @param root - the binary tree to be traversed.
*/
private void printPreOrder(BinaryTreeNode<T> root) {
if (root == null) {
return;
}
printPreOrder(root.getLeftChild());
System.out.print(root.getValue() + " ");
printPreOrder(root.getRightChild());
}
/**
* Traverses and prints the binary
* tree in pre-order manner.
*/
public void printPreOrder() {
printPreOrder(this.root);
System.out.println();
}
}
这里我在“树的根”下面的行“无法解析符号”T“”。
第二部分 - 测试:
public class BinaryTreeExample {
public static void main(String[] args) {
// Create the binary tree from the sample.
BinaryTree<Integer> binaryTree =
new BinaryTree<Integer>(14,
new BinaryTree<Integer>(19,
new BinaryTree<Integer>(23),
new BinaryTree<Integer>(6,
new BinaryTree<Integer>(10),
new BinaryTree<Integer>(21))),
new BinaryTree<Integer>(15,
new BinaryTree<Integer>(3),
null));
// Traverse and print the tree in pre-order manner.
binaryTree.printPreOrder();
}
}
我得到“BinaryTree没有类型参数”。这是在编译之前从IntelliJ编辑器。当我尝试运行程序时,我也会遇到很多错误。如何解决这个问题?
答案 0 :(得分:0)
此处BinaryTree<Integer> binaryTree
您使用通用,但在声明BinaryTree
(public class BinaryTree {
)时,您没有使用通用。因此,您必须在BinaryTree
声明中添加泛型或从创建BinaryTree
对象中删除泛型。
答案 1 :(得分:0)
要纠正错误,您应该将BinaryTree Integer的所有用法替换为BinaryTree,但将BinaryTree声明为泛型BinaryTree会更有意义。您的代码将树的节点声明为通用,但除非您希望在同一个树中使用不同类型的节点,否则应将树声明为通用树并使其节点遵循此类型。
答案 2 :(得分:0)
这是工作代码:
public class BinaryTree<T> {
public static class BinaryTreeNode<T>
{
// Contains the value of the node
private T value;
// Shows whether the current node has parent
private boolean hasParent;
// Contains the left child of the node
private BinaryTreeNode<T> leftChild;
// Contains the right child of the node
private BinaryTreeNode<T> rightChild;
/**
* Constructs a binary tree node.
* @param value - the value of the node.
* @param leftChild - the left child of the node.
* @param rightChild - the right child of the node.
*/
public BinaryTreeNode(T value,
BinaryTreeNode<T> leftChild,
BinaryTreeNode<T> rightChild)
{
if (value == null) {
throw new IllegalArgumentException(
"Cannot insert null value!");
}
this.value = value;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
public BinaryTreeNode(T value)
{
this(value, null, null);
}
/**
* @return the value of the node.
*/
public T getValue() {
return this.value;
}
/**
* Sets the value of the node.
* @param value - the value to be set.
*/
public void setValue(T value) {
this.value = value;
}
/**
* @return the left child or null if it does not exists.
*/
public BinaryTreeNode<T> getLeftChild() {
return this.leftChild;
}
/**
* Sets the left child.
* @param value - the new left child to be set.
*/
public void setLeftChild(BinaryTreeNode<T> value) {
if (value == null || value.hasParent) {
throw new IllegalArgumentException();
}
value.hasParent = true;
this.leftChild = value;
}
/**
* @return the right child or null if it does not exists.
*/
public BinaryTreeNode<T> getRightChild() {
return this.rightChild;
}
/**
* Sets the right child.
* @param value - the new right child to be set.
*/
public void setRightChild(BinaryTreeNode<T> value) {
if (value == null || value.hasParent) {throw new IllegalArgumentException();
}
value.hasParent = true;
this.rightChild = value;
}
}
// The root of the tree
private BinaryTreeNode<T> root;
/**
* Constructs the tree.
* @param value - the value of the node.
* @param children - the children of the node.
*/
public BinaryTree(T value, BinaryTree<T> leftChild,
BinaryTree<T> rightChild) {
if (value == null) {
throw new IllegalArgumentException();
}
BinaryTreeNode<T> leftChildNode =
leftChild != null ? leftChild.root : null;
BinaryTreeNode<T> rightChildNode =
rightChild != null ? rightChild.root : null;
this.root = new BinaryTreeNode<T>(
value, leftChildNode, rightChildNode);
}
/**
* Constructs the tree.
* @param value - the value of the node.
*/
public BinaryTree(T value) {
this(value, null, null);
}
/**
* @return the root of the tree.
*/
public BinaryTreeNode<T> getRoot()
{
return this.root;
}
/**
* @return the left child of the root.
* */
public BinaryTreeNode<T> getLeftChildNode()
{
if (this.root != null)
{
return this.root.getLeftChild();
}
return null;
}
/**
* @return the right child of the root.
*/
public BinaryTreeNode<T> getRightChildNode()
{
if (this.root != null)
{
return this.root.getRightChild();
}
return null;
}
/**
* Traverses binary tree in pre-order manner.
* @param root - the binary tree to be traversed.
*/
private void printPreOrder(BinaryTreeNode<T> root) {
if (root == null) {
return;
}
// 1. Visit the left child.
printPreOrder(root.getLeftChild());
// 2. Visit the root of this subtree.
System.out.print(root.getValue() + " ");
// 3. Visit the right child.
printPreOrder(root.getRightChild());
}
/**
* Traverses and prints the binary
* tree in pre-order manner.
*/
public void printPreOrder() {
printPreOrder(this.root);
System.out.println();
}
}