java:实现二叉搜索树和重写iterable
我希望能够为每个循环使用一个循环,但我最终会进入一个无限循环。java:实现二叉搜索树和重写iterable,java,iterator,iterable,Java,Iterator,Iterable,我希望能够为每个循环使用一个循环,但我最终会进入一个无限循环。 我没有使用递归,调试器也没有给我任何提示。如果是,我就不明白了。这是我的测试仪: Student stud1 = new Student("nic", "aichael", "1234", 75, 90); Student stud2 = new Student("nic", "bichael", "1234", 75, 90); Student stud3 = new Student("nic", "cic
我没有使用递归,调试器也没有给我任何提示。如果是,我就不明白了。
这是我的测试仪:
Student stud1 = new Student("nic", "aichael", "1234", 75, 90);
Student stud2 = new Student("nic", "bichael", "1234", 75, 90);
Student stud3 = new Student("nic", "cichael", "1234", 75, 90);
Student stud4 = new Student("nic", "dichael", "1234", 75, 90);
AVLPersonTree tree = new AVLPersonTree();
tree.add(stud1);
tree.add(stud2);
tree.add(stud3);
tree.add(stud4);
for(Node node: tree){
node.toString();
}
这是我的AVLPersonTree课程:
public class AVLPersonTree implements Iterable<Node>{
private Node root;
private int size;
public AVLPersonTree(){
super();
root = null;
}
public void add(Person newPerson){
Node newNode = new Node(newPerson);
if(root == null){
root = newNode;
}else{
root.addNode(newNode);
}
size++;
}
public int size(){
return size;
}
@Override
public Iterator iterator() {
Iterator<Node> iterate = new Iterator(){
@Override
public boolean hasNext() {
if(root == null){
return false;
}
if(root.getLeftNode() == null && root.getRightNode() == null){
return false;
}
return true;
}
@Override
public Node next() {
if (!hasNext()) {
throw new java.util.NoSuchElementException("no more elements");
}
return preorderNext();
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
};
return iterate;
}
private Node preorderNext() {
Stack<Node> visiting = new Stack<>();
if (visiting.empty()) { // at beginning of iterator
visiting.push(root);
}
Node node = visiting.pop();
// need to visit the left subtree first, then the right
// since a stack is a LIFO, push the right subtree first, then
// the left. Only push non-null trees
if (node.getRightNode() != null) {
visiting.push(node.getRightNode());
}
if (node.getLeftNode() != null) {
visiting.push(node.getLeftNode());
}
// may not have pushed anything. If so, we are at the end
if (visiting.empty()) { // no more nodes to visit
root = null;
}
return node;
}
公共类AVLPersonTree实现了Iterable{
私有节点根;
私有整数大小;
公共AVLPersonTree(){
超级();
root=null;
}
公共无效添加(人-新人){
节点newNode=新节点(newPerson);
if(root==null){
根=新节点;
}否则{
root.addNode(newNode);
}
大小++;
}
公共整数大小(){
返回大小;
}
@凌驾
公共迭代器迭代器(){
迭代器迭代器=新迭代器(){
@凌驾
公共布尔hasNext(){
if(root==null){
返回false;
}
if(root.getLeftNode()==null&&root.getRightNode()==null){
返回false;
}
返回true;
}
@凌驾
公共节点下一步(){
如果(!hasNext()){
抛出新的java.util.NoSuchElementException(“不再有元素”);
}
返回preorderNext();
}
@凌驾
公共空间删除(){
抛出新的UnsupportedOperationException(“尚未受支持”);//若要更改生成的方法体,请选择“工具”“模板”。
}
};
返回迭代;
}
专用节点预订单下一页(){
堆栈访问=新堆栈();
如果(访问.empty()){//在迭代器的开头
访问。推(根);
}
Node=visting.pop();
//需要先访问左子树,然后访问右子树
//因为堆栈是后进先出的,所以先推右子树,然后
//左边。只推非空树
if(node.getRightNode()!=null){
访问.push(node.getRightNode());
}
if(node.getLeftNode()!=null){
访问.push(node.getLeftNode());
}
//可能没有推动任何事情。如果是这样,我们就完了
if(vising.empty()){//没有更多节点可访问
root=null;
}
返回节点;
}
}您的“preorderNext”函数错误
开头的5行将始终显示“节点”中的“根”
堆栈访问=新堆栈();
如果(访问.empty()){//在迭代器的开头
访问。推(根);
}
Node=visting.pop();
因此,如果不进行真正的迭代,那么节点将永远是“根”那么,有什么问题吗?for each循环将无限期地进行
Stack<Node> visiting = new Stack<>();
if (visiting.empty()) { // at beginning of iterator
visiting.push(root);
}
Node node = visiting.pop();