Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node
- include Enumerable
- attr_accessor :data, :left, :right
- def initialize(data)
- @data = data
- end
- def each(&block) # turns block into a lambda(proc) object so it can get passed into the method
- left.each(&block) if left # turns the lambda back into a block so it can be used by each
- block.call(self)
- right.each(&block) if right
- end
- end
- root = Node.new(7)
- root.left = Node.new(3) # Nodes to the left of root should be smaller
- root.right = Node.new(12) # Nodes to the right of the root should be larger
- root.left.left = Node.new(2)
- root.left.right = Node.new(6)
Advertisement
Add Comment
Please, Sign In to add comment