Guest User

Untitled

a guest
Mar 17th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.60 KB | None | 0 0
  1. class Node
  2.   include Enumerable
  3.  
  4.   attr_accessor :data, :left, :right
  5.   def initialize(data)
  6.     @data = data
  7.   end
  8.  
  9.   def each(&block) # turns block into a lambda(proc) object so it can get passed into the method
  10.     left.each(&block) if left # turns the lambda back into a block so it can be used by each
  11.     block.call(self)
  12.     right.each(&block) if right
  13.   end
  14. end
  15.  
  16. root = Node.new(7)
  17. root.left = Node.new(3) # Nodes to the left of root should be smaller
  18. root.right = Node.new(12) # Nodes to the right of the root should be larger
  19. root.left.left = Node.new(2)
  20. root.left.right = Node.new(6)
Advertisement
Add Comment
Please, Sign In to add comment