二进制搜索树迭代器实现,不使用集合类

时间:2016-08-19 15:00:10

标签: java algorithm iterator binary-search-tree

我无法弄清楚如何实现二进制搜索迭代器。 我的问题是如何实现迭代器遍历"顺序"而不使用集合类?

嗯,问题是我不知道如何添加"父母"因为我在倒下时找到迭代器中的前4个项目,但我似乎无法上升,因为"父母"抛出nullpointer或者没有得到正确的项目。

所以如何添加" parent"?

    void add(String string) {
    if (string.compareTo(value) < 0) {
            if(left.left == null){
                left.parent = left;
            }
            if (left == null) {
                size++;

                left = new Node(string);
            if...

感谢。

1 个答案:

答案 0 :(得分:1)

我建议设计三个类:

  • BinarySearchTree:公共类,它表示二进制搜索树。它包含树根作为BinaryTreeNode。
  • BinaryTreeNode:私有嵌套类,它代表一个节点,它具有密钥和引用:对于子节点及其父节点。
  • BinarySearchTreeIterator:私有嵌套类,它表示一个迭代器,它使用对BinaryTreeNode的引用来知道当前元素。

    public class BinarySearchTree implements Iterable<String> {
    
        private BinaryTreeNode root = null;
        private int elements;
    
        @Override
        public Iterator<String> iterator() {
            return new BinarySearchTreeIterator(root);
        }
    
        private static class BinarySearchTreeIterator implements Iterator<String> {
    
            private BinaryTreeNode node;
    
            public BinarySearchTreeIterator(BinaryTreeNode node) {
                if (node != null) {
                    this.node = smallest(node);
                } else {
                    this.node = node;
                }
            }
    
            @Override
            public boolean hasNext() {
                return node != null;
            }
    
            private static BinaryTreeNode smallest(BinaryTreeNode n) {
                if (n.left != null) {
                    return smallest(n.left);
                } else {
                    return n;
                }
            }
    
            @Override
            public String next() {
                String result = node.key;
                if (node.right != null) {
                    node = smallest(node.right);
                } else {
                    while (node.parent != null && node.parent.left != node) {
                        node = node.parent;
                    }
                    node = node.parent;
                }
                return result;
            }
        }
    
        private static class BinaryTreeNode {
    
            private String key;
            private BinaryTreeNode parent;
            private BinaryTreeNode left;
            private BinaryTreeNode right;
    
            public BinaryTreeNode(String key) {
                this.key = key;
            }
        }
    
        public boolean insert(String key) {
            if (key == null) {
                return false;
            }
            int lastElements = elements;
            this.root = insert(key, root, null);
            return lastElements < elements;
        }
    
        private BinaryTreeNode insert(String key, BinaryTreeNode node, BinaryTreeNode parent) {
            BinaryTreeNode result = node;
            if (node == null) {
                result = new BinaryTreeNode(key);
                result.parent = parent;
                this.elements++;
            } else {
                int compare = key.compareTo(node.key);
                if (compare < 0) {
                    result.left = insert(key, node.left, node);
                } else if (compare > 0) {
                    result.right = insert(key, node.right, node);
                }
            }
            return result;
        }
    
        public static void main(String[] args) {
            BinarySearchTree tree = new BinarySearchTree();
            String[] strings = {"l", "f", "t", "c", "g", "p", "u"};
            for (String string : strings) {
                System.out.println("insert: '" + string + "' " + tree.insert(string));
            }
            System.out.println("--");
    
            for (String s : tree) {
                System.out.println(s);
            }
        }
    }
    

打印:

insert: 'l' true
insert: 'f' true
insert: 't' true
insert: 'c' true
insert: 'g' true
insert: 'p' true
insert: 'u' true
--
c
f
g
l
p
t
u