A bit of a newbie to computing science.
I have the basics for a binary tree in Ruby:
class Node
attr_accessor :left, :right, :value
def initialize(value)
@value = value
end
end
This works fine if I construct it manually, for example, if I want tom to be a child node of ben:
ben = Node.new('Ben')
tom = Node.new('Tom')
ben.left = tom
One of the challenges I need to figure out is how to construct a tree for inputted parent/child pairs. Here is an example input string:
peter tom
peter marie
marie john
tom oscar
My binary tree would look something like this:
peter
|
tom marie
| |
oscar john
I am wondering if I can get some direction into converting multiple strings in the following format "[parent] [child]" into a binary tree.
Thanks :)
[parent] [child]format, how do you decide whether a child goes intoleftorright? Likewise, what happens if the input contains more than two child entries for a given parent?