Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuay03 committed Jul 14, 2024
1 parent 0348986 commit b0b5d3c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
14 changes: 12 additions & 2 deletions lib/red_black_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
class RedBlackTree
include Utils

attr_reader :root
attr_reader :root, :size

def initialize
@size = 0
end

# Traverses the tree, comparing existing nodes to the node to be added,
# and inserts the node with the appropriate parent and direction.
Expand All @@ -25,6 +29,10 @@ def << node
end
alias_method :add, :<<

def empty?
@size == 0
end

def insert! node, target_parent = nil, direction = nil
if target_parent.nil?
raise ArgumentError, "Target parent must be provided for tree with root" if @root
Expand Down Expand Up @@ -73,6 +81,8 @@ def insert! node, target_parent = nil, direction = nil
@root.black!
end

@size += 1

self
end

Expand All @@ -96,6 +106,6 @@ def rotate_sub_tree! node, direction
opp_direction_child[direction] = node
node.parent = opp_direction_child

opp_direction_child # new root
opp_direction_child # new sub-tree root
end
end
6 changes: 0 additions & 6 deletions red-black-tree.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,4 @@ Gem::Specification.new do |spec|
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
end

0 comments on commit b0b5d3c

Please sign in to comment.