Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
binary tree and nodes
class Node
attr_accessor :data, :left, :right
def initialize(data)
@data = data
end
include Comparable
def <=>(node)
data <=> node.data
end
def to_s
[left, data, right].join(" ")
end
end
class BinaryTree
attr_accessor :root
def insert(new_node, node = root)
if root.nil? # if root (first node in tree) does not exist
self.root = new_node #assign the new_node to root instance variable
elsif node.nil? #first statement doesnt fire and node = nil
new_node
else
if new_node < node
node.left = insert(new_node, node.left)
else
node.right = insert(new_node, node.right)
end
node
end
end
def nodes(current_node = self.root)
all_nodes = []
all_nodes << current_node
if current_node.left
all_nodes << current_node.left
all_nodes = all_nodes + nodes(current_node.left)
end
if current_node.right
all_nodes << current_node.right
all_nodes = all_nodes + nodes(current_node.right)
end
all_nodes
end
#def to_s
# self.root.to_s.squeeze(" ").strip
#end
end
@tree = BinaryTree.new
%w(Dan Barry Ted Daniel Alice Andy Sally).each do |name|
@tree.insert(Node.new(name))
end
puts @tree.nodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.