From b0b5d3c60b57eba97103009a15009e386f655815 Mon Sep 17 00:00:00 2001 From: Joshua Young Date: Mon, 8 Jul 2024 14:34:28 +0530 Subject: [PATCH] first commit --- lib/red_black_tree.rb | 14 ++++++++++++-- red-black-tree.gemspec | 6 ------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/red_black_tree.rb b/lib/red_black_tree.rb index 12b549f..df0f2f4 100644 --- a/lib/red_black_tree.rb +++ b/lib/red_black_tree.rb @@ -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. @@ -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 @@ -73,6 +81,8 @@ def insert! node, target_parent = nil, direction = nil @root.black! end + @size += 1 + self end @@ -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 diff --git a/red-black-tree.gemspec b/red-black-tree.gemspec index 79aa640..1bb1c79 100644 --- a/red-black-tree.gemspec +++ b/red-black-tree.gemspec @@ -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