class BinTree
attr_accessor :left, :right, :val
def initialize(val)
@left = nil
@right = nil
@val = val
end
def insert_node(new_val)
if new_val < @val
if(@left == nil)
@left = BinTree.new(new_val)
else
@left.insert_node(new_val)
end
elsif(new_val > @val)
if(@right == nil)
@right = BinTree.new(new_val)
else
@right.insert_node(new_val)
end
end
end
# Searching the Tree for search_val
def search_tree(search_val)
# Start our queue and Visited list
queue = Queue.new
visited = []
# adds root tree to queue
# adds to visited list
queue.enq(self)
visited.push(self)
while !queue.empty?
current_node = queue.deq
# Check if current_node val == search_val
if current_node.val == search_val
return current_node
end
if current_node.left && !visited.include?(current_node.left)
queue.enq(current_node.left)
visited.push(current_node.left)
end
if current_node.right && !visited.include?(current_node.right)
queue.enq(current_node.right)
visited.push(current_node.right)
end
end
"none!"
end
end