AVL 树上的迭代器实现 java

Iterator implement on AVL tree java

我正在尝试在 AVL 树上实现迭代器,我被困在这个特定的函数上:

 /**
     * @return an iterator  for the Avl Tree. The returned  iterator  iterates  over the tree nodes.
     * in an ascending order, and does NOT implement the remove() method.
     */
    public Iterator<Integer> iterator(){
        List myList=new ArrayList<>();
        Node counter=root;
        Node counter1=root.getChildernLeft();
        if(counter1==null){
            myList.add(counter1);
        }

        Node counter1=root.getChildernLeft();
    }

我添加了我的两个 classes:

完整代码,节点 class:

public class Node {

private Node parent;
private Node childernRight;
private Node childernLeft;
private int data;
private int height;


/**
 * constractor
 */
public Node(int data, Node childernLeft, Node childernRight) {
    this.data = data;
    this.childernLeft = childernLeft;
    this.childernRight = childernRight;
    this.childernRight.setParent(this);
    this.childernLeft.setParent(this);
}

public Node(int data) {
    this.data = data;
}

public Node getParent() {
    return parent;
}

public Node getChildernRight() {
    return childernRight;
}

public Node getChildernLeft() {
    return childernLeft;
}

public void setParent(Node parent) {
    this.parent = parent;
}

public void setChildernRight(Node childernRight) {
    this.childernRight = childernRight;
    this.childernRight.setParent(this);
}

public void setChildernLeft(Node childernLeft) {
    this.childernLeft = childernLeft;
    this.childernLeft.setParent(this);
}

public int getData() {
    return data;
}

public String toString() {
    if (this.childernLeft != null && this.getChildernRight() != null) {
        return childernLeft.toString() + "," + this.data + "," + childernRight.toString();
    } else if (this.childernLeft != null && this.getChildernRight() == null) {
        return childernLeft.toString() + "," + this.data;
    } else if (this.childernLeft == null && this.getChildernRight() != null) {
        return this.data + "," + childernRight.toString();
    } else {
        return this.data + "";
    }
}

public int balanceFactor() {
    int balanceFactor = childernRight.height - childernLeft.height;
    return balanceFactor;
}

public boolean hasChildrenRight() {

    if (this.childernRight != null) {
        return true;
    }
    else{
        return false;
    }
}

public boolean hasChildrenLeft() {

    if (this.childernLeft != null) {
        return true;
    }
    else{
        return false;
    }
}


/**
 * if we have many children in the tree
 */
public void addNode(Node val){
    if(hasChildrenRight()&&this.data>val.data){
        this.setChildernRight(val);
    }
    if(!hasChildrenLeft()&&this.data<val.data){
        this.setChildernLeft(val);
    }
    if(!hasChildrenLeft()&&!hasChildrenRight()){
        if(this.data>val.data){
        setChildernLeft(val);
        }
        else{
            setChildernRight(val);
        }
    }

    if(val.data>this.data){
        childernRight.addNode(val);
    }
    else{
        childernLeft.addNode(val);
    }

}
}

AvlTree 的完整代码:`

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class AvlTree {
    private Node root;
    private int size;


    /**
     * default constructor.
     */
    public AvlTree() {
        this.root = null;
        this.size = 0;
    }

    /**
     * A constructor that  builds a new AVL tree containing  all unique values in the input
     * array
     *
     * @param data the values to add to tree
     */
    public AvlTree(int[] data) {
        for (int i = 0; i < data.length; i++) {
            add(data[i]);
        }
    }

    /**
     * A copy constructor that  creates a deep copy of the given oop.ex4.data_structures.AvlTree. The new tree
     * contains  all the values of the given tree, but  not necessarily in the same structure.
     *
     * @param avlTree an AVL tree.
     */
    public AvlTree(AvlTree avlTree) {

    }


    /**
     * Add a new node with the given key to the tree.
     *
     * @param newValue the value of the new node to add.
     * @return true if the value to add is not already in the tree and it was successfully added,
     * false otherwise.
     */
    public boolean add(int newValue) {
        if (root == null) {
            root = new Node(newValue);
            size++;
            return true;
        }
        Node current = root;
        Node parent = null;
        while (current != null) {
            if (current.getData() == newValue) {
                return false;
            }
            parent = current;
            if (newValue < current.getData()) {
                current = current.getChildernLeft();
            } else {
                current = current.getChildernRight();
            }
        }
        size++;
        // when we here the new NODE Need to be chiled of Node hashmor in parent.
        // addHelper is adding the new Node.2
//        addHelper(newNodeToAdd, current);
        return true;
    }


    private void addHelper(Node newNodeToAdd, Node current) {
//        if (newNodeToAdd.getData() > current.getData()) {
//            if (current.getChildernRight() == null) {
//                current.setChildernRight(newNodeToAdd);
//            } else {
//                addHelper(newNodeToAdd, current.getChildernRight());
//            }
//        } else {
//            if (current.getChildernLeft() == null) {
//                current.setChildernLeft(newNodeToAdd);
//            } else {
//                addHelper(newNodeToAdd, current.getChildernLeft());
//            }
//        }
    }


    /**
     * Check whether the tree contains  the given input  value.
     *
     * @param searchVal the value to search for.
     * @return the depth  of the node (0 for the root) with the given value if it was found in the tree, −1 otherwise.
     */
    public int contains(int searchVal) {
        Node current = root;
        int depth = 0;
        while (current != null) {
            if (current.getData() == searchVal) {
                return depth;
            }
            depth++;
            if (searchVal < current.getData()) {
                current = current.getChildernLeft();
            } else {
                current = current.getChildernRight();
            }
        }
        return -1;
    }


    /**
     * Removes the node with the given value from the tree, if it exists.
     *
     * @param toDelete the value to remove from the tree.
     * @return true if the given value was found and deleted, false otherwise.
     */
    public boolean delete(int toDelete) {
        size -= 1;
        return true;
    }


    /**
     * @return the number of nodes in the tree.
     */
    public int size() {
        return size;
    }

    /**
     * @return an iterator  for the Avl Tree. The returned  iterator  iterates  over the tree nodes.
     * in an ascending order, and does NOT implement the remove() method.
     */
    public Iterator<Integer> iterator(){
        List myList=new ArrayList<>();
        Node counter=root;
        Node counter1=root.getChildernLeft();
        if(counter1==null){
            myList.add(counter1);
        }

        Node counter1=root.getChildernLeft();
    }



    /**
     * Calculates  the minimum number of nodes in an AVL tree of height h
     *
     * @param h the height of the tree (a non−negative  number)  in question.
     * @return the minimum number of nodes in an AVL tree of the given height.
     */
    public static int findMinNodes(int h) {
        // I SOVLE THIS WITH ROCKISA
        int counterMin = 0;
        if (h == 0) {
            return counterMin = 1;
        }
        if (h == 1) {
            return counterMin = 2;
        }
        return (findMinNodes(h - 1) + findMinNodes(h - 2) + 1);
    }


    /**
     * Calculates  the maximum  number of nodes in an AVL tree of height h.
     *
     * @param h the height of the tree (a non−negative  number)  in question.
     * @return the maximum  number of nodes in an AVL tree of the given height.
     */
    public static int findMaxNodes(int h) {
        int counterMax = 0;
        for (int i = 0; i < h; i++) {
            counterMax += Math.pow(h, i);
        }
        return counterMax;
    }

    /**
     * @return
     */
    public String toString() {
        if (root == null) {
            return "";
        }
        return root.toString();
    }

    //THIS IS OK
    public void RotationRr(Node valRotation) {
        if (valRotation == root) {
            Node keepRootChild = root.getChildernRight();
            root.setChildernRight(null);

        }
        Node parent1 = valRotation.getParent();
        Node keepRightChild = valRotation.getChildernRight();///4
        valRotation.setChildernRight(null);
        keepRightChild.setParent(parent1);
        if (!keepRightChild.hasChildrenLeft()) {
            keepRightChild.setChildernLeft(valRotation);
            valRotation.setParent(keepRightChild);
        } else {
            keepRightChild.getChildernLeft().addNode(valRotation);

        }
    }

    public void RotationLr(Node valRotation) {
        RotationLl(valRotation);

    }

    /**
     * ........CHECK.....!!!!TODO
     *
     * @param valRotation
     */
    public void RotationLl(Node valRotation) {
        Node parent2 = valRotation.getParent();
        Node keepLeftChild = valRotation.getChildernLeft();
        valRotation.setChildernLeft(null);
        keepLeftChild.setParent(parent2);
        if (!keepLeftChild.hasChildrenRight()) {
            keepLeftChild.setParent(keepLeftChild);
        } else {
            keepLeftChild.getChildernRight().addNode(valRotation);
        }


    }


    public void RotationRl(Node valRotation) {
        RotationRr(valRotation);
    }




    public static void main(String[] args) {
        AvlTree avlTree = new AvlTree();
        avlTree.add(7);
        avlTree.add(2);
        System.out.println(avlTree.contains(2));
//        System.out.println(avlTree.contains(5));
        avlTree = new AvlTree();
        avlTree.add(2);
        avlTree.add(7);
        System.out.println(avlTree.contains(2));
//        System.out.println(avlTree);
    }


}

`

您可以使用堆栈实现迭代器

public Iterator<V> iterator() {
        return new Itr();
    }

    private class Itr<V> implements Iterator<V> {
        TreeNode<V> localNode = (TreeNode<V>) root;
        Stack<TreeNode<V>> stack;

        public Itr() {
            stack = new ArrayStack<TreeNode<V>>();

            while (localNode != null) {
                stack.push(localNode);
                localNode = localNode.getLeftChield();
            }
        }

        public boolean hasNext() {
            return !stack.empty();
        }

        public V next() {
            TreeNode<V> node = stack.pop();
            V v = node.getValue();
            if (node.getRightChield() != null) {
                node = node.getRightChield();
                while (node != null) {
                    stack.push(node);
                    node = node.getLeftChield();
                }
            }
            return v;
        }

        public void remove() {
            throw new UnsupportedOperationException();

        }

    }