gpt4 book ai didi

java - Java SE 1.7 中的原始类型二叉搜索树问题

转载 作者:行者123 更新时间:2023-12-04 05:24:09 25 4
gpt4 key购买 nike

我正在尝试构建原始类型 BST , 与 Comparable<T> .事情是不知何故我的声明做错了,因为我在 Node 中使用类型Comparable<type>并在 BST将其分类为错误

The method setParent(Node<Comparable<Comparable<type>>>) in the type
Node<Comparable<type>> is not applicable for the arguments (Node<Comparable<type>>)
BinarySearchTree.java /lab2/src line 22 Java Problem

Node.java :
public class Node <type> {

private Comparable<type> key;
private Node <Comparable<type>> parent;
private Node <Comparable<type>> leftChild;
private Node <Comparable<type>> rightChild;

public Node(Comparable<type> key, Node <Comparable<type>> leftChild, Node <Comparable<type>> rightChild) {
this.setKey(key);
this.setLeftChild(leftChild);
this.setRightChild(rightChild);
}

public void setKey(Comparable<type> key) {
this.key = key;
}

public Comparable<type> getKey() {
return key;
}

public void setParent(Node<Comparable<type>> y) {
this.parent = y;
}

public Node <Comparable<type>> getParent() {
return parent;
}

public void setLeftChild(Node <Comparable<type>> leftChild) {
this.leftChild = leftChild;
}

public Node <Comparable<type>> getLeftChild() {
return leftChild;
}

public void setRightChild(Node <Comparable<type>> rightChild) {
this.rightChild = rightChild;
}

public Node <Comparable<type>> getRightChild() {
return rightChild;
}
}

BinarySearchTree.java :
import java.util.Iterator;

public class BinarySearchTree<type> implements SortedSet<type> {

private Node <Comparable<type>> root;

public void insert(Node <Comparable<type>> z) {

Node <Comparable<type>> y = null;
Node <Comparable<type>> x = root;

while (x != null) {
y = x;

if (z.getKey() < x.getKey()) { // ERROR '<' is undefined for type...
x = x.getLeftChild(); // PARAM TYPE ERROR
} else {
x = x.getRightChild(); // PARAM TYPE ERROR
}
}

z.setParent(y);

if (y == null) {
root = z;
} else if (z.getKey() < y.getKey()) {
y.setLeftChild(z);
} else {
y.setRightChild(z);
}
}

最佳答案

考虑重构为以下代码

import java.util.SortedSet;

public abstract class BinarySearchTree<T extends Comparable<T>> implements SortedSet<T> {
private Node<T> root;

class Node<T extends Comparable<T>> {

private T key;
private Node<T> parent;
private Node<T> leftChild;
private Node<T> rightChild;

public Node(T key, Node<T> leftChild, Node<T> rightChild) {
this.setKey(key);
this.setLeftChild(leftChild);
this.setRightChild(rightChild);
}

public void setKey(T key) {
this.key = key;
}

public T getKey() {
return key;
}

public void setParent(Node<T> y) {
this.parent = y;
}

public Node <T> getParent() {
return parent;
}

public void setLeftChild(Node <T> leftChild) {
this.leftChild = leftChild;
}

public Node <T> getLeftChild() {
return leftChild;
}

public void setRightChild(Node <T> rightChild) {
this.rightChild = rightChild;
}

public Node <T> getRightChild() {
return rightChild;
}
}

public void insert(Node<T> z) {

Node<T> y = null;
Node<T> x = root;

while (x != null) {
y = x;

if (z.getKey().compareTo(x.getKey()) < 0) {
x = x.getLeftChild();
} else {
x = x.getRightChild();
}
}

z.setParent(y);

if (y == null) {
root = z;
} else if (z.getKey().compareTo((T) y.getKey()) <0) {
y.setLeftChild(z);
} else {
y.setRightChild(z);
}
}
}

关于java - Java SE 1.7 中的原始类型二叉搜索树问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13398030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com