Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Tree
- attr_accessor :root, :data, :children
- def initialize(parent = nil,data=nil)
- @parent = parent
- @data = data
- @children = []
- end
- def add_subtree(subtree)
- subtree.parent = self
- children.push(subtree)
- end
- def add_node(data)
- children.push(Tree.new(self,data))
- end
- def root?()
- return not parent.nil?
- end
- def to_a
- return @children.map{|t| t.to_a}
- end
- def walk
- end
- def prune(subtree)
- i = @children.index(subtree)
- if i then
- st = @children[i]
- @children = [0..(i-1)] + [(i+1)..-1]
- end
- end
- def leaf?()
- return @children.empty?
- end
- end
Add Comment
Please, Sign In to add comment