简体   繁体   中英

Custom treeSet class, how to write iterator

I am trying to create my own binary search tree. But I can't think of any way to implement a working iterator that has hasNext(), next(). I got the idea the only way to traverse a Binary search tree was through recursion. But how could I possibly save a recursion call if I am trying to use next, so it resumes when next gets called again and returns the value? Is there any other way

import java.util.Iterator;

public class TreeWordSet implements WordSetInterface {
    private BST root = null;

    private class BST {
        Word value;
        BST left = null;
        BST right = null;

        BST(Word word) {
            value = word;
        }

        void add(Word newWord) {
            if (newWord.compareTo(value) < 0) {
                if(left == null) {
                    left = new BST(newWord);
                } else {
                    left.add(newWord);
                }
            } else if (newWord.compareTo(value) > 0) {
                if (right == null) {
                    right = new BST(newWord);
                } else {
                    right.add(newWord);
                }
            }
        }
    }

    @Override
    public void add(Word word) {
        if (root == null) {
            root = new BST(word);
        } else {
            root.add(word);
        }
    }

    @Override
    public boolean contains(Word word) {
        return false;
    }

    @Override
    public int size() {
        return 0;
    }

    private class TreeWordSetIterator implements Iterator<Word> {

        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public Word next() {
            return null;
        }
    }

    @Override
    public Iterator<Word> iterator() {
        return new TreeWordSetIterator();
    }

}

If this is an exercise for the purpose of learning, maybe the best way is to just go look how TreeSet does it. If this is an exercise for production use, stop it right here right now and extend TreeSet if you really need to.

I solved it this way, it's not glamourous. Until someone can provide a better solution

    private class TreeWordSetIterator implements Iterator<Word> {
    Word arr[] = new Word[size()];
    int i = 0;
    TreeWordSetIterator(){
        traverse(root);
        i = 0;
    }

    private void traverse(BST currentNode) {
        if (currentNode == null) {
            return;
        }
        traverse(currentNode.left);
        arr[i] = currentNode.value;
        i++;
        traverse(currentNode.right);
    }

    @Override
    public boolean hasNext() {
        if (i < size()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Word next() {
        Word current = arr[i];
        i++;
        return current;

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM