Skip to content

Instantly share code, notes, and snippets.

@neight-allen
Created December 9, 2016 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neight-allen/df4e835c41a89816005e388c5ada8c9e to your computer and use it in GitHub Desktop.
Save neight-allen/df4e835c41a89816005e388c5ada8c9e to your computer and use it in GitHub Desktop.
My ruby implementation of Turing School's Binary Search Tree project
require_relative 'node'
class BinarySearchTree
attr_accessor :root
def initialize
@root = Node.new
end
def insert(value, movie)
if @root.value == nil
@root.value = value
@root.movie = movie
0
else
@root.insert value, movie
end
end
def include_value?(value)
find_node_with_value(value).nil? ? false : true
end
def min
current_node = @root
while !current_node.left.nil?
current_node = current_node.left
end
current_node.value
end
def max
current_node = @root
while !current_node.right.nil?
current_node = current_node.right
end
current_node.value
end
def depth_of(value)
node = find_node_with_value(value)
node.depth unless node.nil?
end
def sort
@root.as_array
end
def load(filename)
num_movies = 0
File.open(filename, 'r') do |file|
while line = file.readline.strip
score, movie = line.split(', ')
insert score.to_i, movie
num_movies += 1
end
end
num_movies
end
def health(depth)
nodes_by_depth[depth].map do |node|
node.health(size)
end
end
private
def find_node_with_value(value)
@root.find_node_with_value value
end
def nodes_by_depth
nodes = []
sort.each do |hash|
node = find_node_with_value hash.values[0]
nodes[node.depth] = [] if nodes[node.depth] == nil
nodes[node.depth] << node
end
nodes
end
def size
sort.count
end
end
class Node
attr_accessor :value, :left, :right, :parent, :movie
def initialize(init_value = nil, init_movie = nil, init_parent = nil)
@value = init_value
@movie = init_movie
@parent = init_parent
end
def insert(new_value, movie)
if new_value < value
insert_or_create_left new_value, movie
else
insert_or_create_right new_value, movie
end
end
def depth
current_node = self
depth_count = 0
while !current_node.parent.nil?
current_node = current_node.parent
depth_count += 1
end
depth_count
end
def find_node_with_value(value)
return self if @value == value
if value < @value
return nil if @left.nil?
return @left.find_node_with_value value
else
return nil if @right.nil?
return @right.find_node_with_value value
end
end
def as_array
if left.nil? && right.nil?
[to_hash]
elsif left.nil?
[to_hash] + right.as_array
elsif right.nil?
left.as_array + [to_hash]
else
left.as_array + [to_hash] + right.as_array
end
end
def health(total_nodes)
[value, num_children + 1, (num_children+1)*100/total_nodes]
end
def num_children
children = 0
children += 1 + @left.num_children unless @left.nil?
children += 1 + @right.num_children unless @right.nil?
children
end
def to_hash
{@movie => @value}
end
private
def create_child(value)
Node.new value, nil, self
end
def insert_or_create_left(value, movie)
if @left.nil?
@left = create_child value
@left.movie = movie
@left.depth
else
@left.insert value, movie
end
end
def insert_or_create_right(value, movie)
if @right.nil?
@right = create_child value
@right.movie = movie
@right.depth
else
@right.insert value, movie
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment