Guest User

Untitled

a guest
Jan 13th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. class Tree
  2. attr_accessor :root, :data, :children
  3. def initialize(parent = nil,data=nil)
  4. @parent = parent
  5. @data = data
  6. @children = []
  7. end
  8. def add_subtree(subtree)
  9. subtree.parent = self
  10. children.push(subtree)
  11. end
  12.  
  13. def add_node(data)
  14. children.push(Tree.new(self,data))
  15. end
  16.  
  17. def root?()
  18. return not parent.nil?
  19. end
  20.  
  21. def to_a
  22. return @children.map{|t| t.to_a}
  23. end
  24.  
  25. def walk
  26. end
  27.  
  28. def prune(subtree)
  29. i = @children.index(subtree)
  30. if i then
  31. st = @children[i]
  32. @children = [0..(i-1)] + [(i+1)..-1]
  33. end
  34. end
  35.  
  36. def leaf?()
  37. return @children.empty?
  38. end
  39. end
Add Comment
Please, Sign In to add comment