Skip to content

Instantly share code, notes, and snippets.

@santosgabriel
Created April 23, 2019 17:59
Show Gist options
  • Save santosgabriel/de03108219e3aa9f2a320a65fe38ae01 to your computer and use it in GitHub Desktop.
Save santosgabriel/de03108219e3aa9f2a320a65fe38ae01 to your computer and use it in GitHub Desktop.
class Node
attr_accessor :data, :left, :right
def initialize(data = nil)
@data = data
@left = nil
@right = nil
end
end
class TreeManager
def insert(root, data)
if root.nil?
root = Node.new
root.data = data
elsif data < root.data
root.left = insert(root.left, data)
else
root.right = insert(root.right, data)
end
root
end
end
@tree_manager = TreeManager.new
@root = nil
(Array.new(100) { rand(1..100) }).each do |number|
@root = @tree_manager.insert @root, number
end
p @root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment