Created
April 23, 2019 17:59
-
-
Save santosgabriel/de03108219e3aa9f2a320a65fe38ae01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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