Sha256: 05ccf0c03ffbc14de97df465003295e497b2fe15923c9dcb23eeaf4af3054ac1

Contents?: true

Size: 703 Bytes

Versions: 11

Compression:

Stored size: 703 Bytes

Contents

class Binary_tree_iterator
    include Enumerable

    attr_reader :root

    def initialize(root)
        self.root = root
    end

    def each(&block)
        self.enumerator.each(&block)
    end

    private
    attr_writer :root

    def enumerator
        Enumerator.new do |yielder|
            stack = []
            push_left_branch(yielder, self.root, stack)
        end
    end

    def push_left_branch(yielder, node, stack)
        while node
            yielder << node.value
            stack.push(node)
            node = node.left
        end

        until stack.empty?
            current = stack.pop
            push_left_branch(yielder, current.right, stack)
        end
    end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
student_mvp-0.1.10 lib/models/binary_tree/binary_tree_iterator.rb
student_mvp-0.1.9 lib/models/binary_tree/binary_tree_iterator.rb
student_mvp-0.1.8 lib/models/binary_tree/binary_tree_iterator.rb
student_mvp-0.1.7 lib/models/binary_tree/binary_tree_iterator.rb
student_mvp-0.1.6 lib/models/binary_tree/binary_tree_iterator.rb
student_mvp-0.1.5 lib/models/binary_tree/binary_tree_iterator.rb
student_mvp-0.1.4 lib/models/binary_tree/binary_tree_iterator.rb
student_mvp-0.1.3 lib/models/binary_tree/binary_tree_iterator.rb
student_mvp-0.1.2 lib/models/binary_tree/binary_tree_iterator.rb
student_mvp-0.1.1 lib/models/binary_tree/binary_tree_iterator.rb
student_mvp-0.1.0 lib/models/binary_tree/binary_tree_iterator.rb