Class: BinarySearch::List
- Inherits:
-
Object
- Object
- BinarySearch::List
- Includes:
- Enumerable
- Defined in:
- lib/binary_search/list.rb
Overview
A self-balancing binary search tree implementation of a list
This class provides a list-like interface backed by a Red-Black Tree, which ensures O(log n) time complexity for most operations.
Instance Method Summary collapse
-
#&(other) ⇒ BinarySearch::List
Compute the intersection of two lists.
-
#+(other) ⇒ BinarySearch::List
Concatenate two lists.
-
#-(other) ⇒ BinarySearch::List
Compute the difference between two lists.
-
#==(other) ⇒ Boolean
Compare two lists for equality.
-
#[](arg) ⇒ Object, ...
Access elements by index or range.
-
#clear ⇒ BinarySearch::List
Clear all elements from the list.
-
#delete(value) ⇒ Boolean
Delete all occurrences of a value from the list.
-
#each {|Object| ... } ⇒ Enumerator
Iterate over each element in the list.
-
#empty? ⇒ Boolean
Check if the list is empty.
-
#find {|Object| ... } ⇒ Object?
Find the first element that satisfies the given condition.
-
#first ⇒ Object?
Get the first element in the list.
-
#include?(value) ⇒ Boolean
Check if a value is in the list.
-
#initialize(from = []) ⇒ List
constructor
Initialize a new BinarySearch::List.
-
#insert(value) ⇒ BinarySearch::List
(also: #append, #push, #<<)
Insert a value into the list.
-
#inspect ⇒ String
Provide a string representation of the list.
-
#last ⇒ Object?
Get the last element in the list.
-
#max ⇒ Object?
Get the maximum value in the list.
-
#min ⇒ Object?
Get the minimum value in the list.
-
#pop ⇒ Object?
Remove and return the last element in the list.
-
#shift ⇒ Object?
Remove and return the first element in the list.
-
#size ⇒ Integer
Get the size of the list.
-
#sum ⇒ Numeric
Calculate the sum of all elements in the list.
-
#to_a ⇒ Array
Convert the list to an array.
-
#uniq ⇒ BinarySearch::List
Create a new list with duplicate elements removed.
-
#unshift(value) ⇒ BinarySearch::List
Insert a value at the beginning of the list.
-
#|(other) ⇒ BinarySearch::List
Compute the union of two lists.
Constructor Details
#initialize(from = []) ⇒ List
Initialize a new BinarySearch::List
27 28 29 30 |
# File 'lib/binary_search/list.rb', line 27 def initialize(from = []) @tree = BinarySearch::RedBlackTree.new build_tree(from) end |
Instance Method Details
#&(other) ⇒ BinarySearch::List
Compute the intersection of two lists
This method returns a new list containing elements common to both lists, taking into account the number of occurrences of each element. It has a time complexity of O(n log n + m log m), where n and m are the sizes of the lists.
420 421 422 |
# File 'lib/binary_search/list.rb', line 420 def &(other) self.class.new(to_a & other.to_a) end |
#+(other) ⇒ BinarySearch::List
Concatenate two lists
This method returns a new list containing all elements from both lists. It has a time complexity of O(n + m), where n and m are the sizes of the lists.
374 375 376 |
# File 'lib/binary_search/list.rb', line 374 def +(other) self.class.new(to_a + other.to_a) end |
#-(other) ⇒ BinarySearch::List
Compute the difference between two lists
This method returns a new list containing elements that are in the current list but not in the other list, taking into account the number of occurrences of each element. It has a time complexity of O(n log n + m log m), where n and m are the sizes of the lists.
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/binary_search/list.rb', line 391 def -(other) result = self.class.new self_counts = Hash.new(0) other_counts = Hash.new(0) self.each { |item| self_counts[item] += 1 } other.each { |item| other_counts[item] += 1 } self_counts.each do |item, count| diff = count - other_counts[item] diff.times { result.insert(item) } if diff > 0 end result end |
#==(other) ⇒ Boolean
Compare two lists for equality
This method checks if two lists have the same elements in the same order. It has a time complexity of O(n), where n is the size of the lists.
454 455 456 457 458 |
# File 'lib/binary_search/list.rb', line 454 def ==(other) return false unless other.is_a?(BinarySearch::List) return true if self.equal?(other) self.to_a == other.to_a end |
#[](arg) ⇒ Object, ...
Access elements by index or range
This method allows accessing elements by index or range, similar to Array. It has a time complexity of O(n) in the worst case.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/binary_search/list.rb', line 149 def [](arg) case arg when Integer return nil if arg < 0 || arg >= size to_a[arg] when Range start = arg.begin finish = arg.end start = size + start if start < 0 finish = size + finish if finish < 0 finish -= 1 if arg.exclude_end? return nil if start < 0 || start >= size result = [] (start..finish).each do |i| break if i >= size result << to_a[i] end self.class.new(result) else raise ArgumentError, "Invalid argument type: #{arg.class}" end end |
#clear ⇒ BinarySearch::List
Clear all elements from the list
This method removes all elements from the list, leaving it empty. It has a time complexity of O(1).
200 201 202 203 204 |
# File 'lib/binary_search/list.rb', line 200 def clear @tree = BinarySearch::RedBlackTree.new @size = 0 self end |
#delete(value) ⇒ Boolean
Delete all occurrences of a value from the list
This method removes all instances of the specified value from the list. It has a time complexity of O(log n) for each deletion.
64 65 66 67 68 69 70 71 72 |
# File 'lib/binary_search/list.rb', line 64 def delete(value) deleted = false while @tree.find(value) @tree.remove(value) @size -= 1 if @size deleted = true end deleted end |
#each {|Object| ... } ⇒ Enumerator
Iterate over each element in the list
This method yields each element in the list to the given block. It has a time complexity of O(n).
185 186 187 |
# File 'lib/binary_search/list.rb', line 185 def each(&block) to_a.each(&block) end |
#empty? ⇒ Boolean
Check if the list is empty
This method checks if the list contains no elements. It has a time complexity of O(1).
130 131 132 |
# File 'lib/binary_search/list.rb', line 130 def empty? @tree.root.nil? end |
#find {|Object| ... } ⇒ Object?
Find the first element that satisfies the given condition
This method returns the first element for which the given block returns true. It has a time complexity of O(n) in the worst case.
343 344 345 346 |
# File 'lib/binary_search/list.rb', line 343 def find each { |element| return element if yield(element) } nil end |
#first ⇒ Object?
Get the first element in the list
This method returns the smallest element in the list. It has a time complexity of O(log n).
216 217 218 219 |
# File 'lib/binary_search/list.rb', line 216 def first return nil if empty? leftmost_node(@tree.root).key end |
#include?(value) ⇒ Boolean
Check if a value is in the list
This method checks if the list contains the specified value. It has a time complexity of O(log n).
86 87 88 |
# File 'lib/binary_search/list.rb', line 86 def include?(value) !@tree.find(value).nil? end |
#insert(value) ⇒ BinarySearch::List Also known as: append, push, <<
Insert a value into the list
This method inserts a value into the list, maintaining the sorted order. It has a time complexity of O(log n).
43 44 45 46 47 |
# File 'lib/binary_search/list.rb', line 43 def insert(value) @tree.insert(value) @size = nil self end |
#inspect ⇒ String
Provide a string representation of the list
This method returns a concise string representation of the list, showing the class name and the size of the list.
470 471 472 |
# File 'lib/binary_search/list.rb', line 470 def inspect "#<#{self.class}: (#{size} elements)>" end |
#last ⇒ Object?
Get the last element in the list
This method returns the largest element in the list. It has a time complexity of O(log n).
231 232 233 234 |
# File 'lib/binary_search/list.rb', line 231 def last return nil if empty? rightmost_node(@tree.root).key end |
#max ⇒ Object?
Get the maximum value in the list
This method returns the largest element in the list. It has a time complexity of O(log n).
300 301 302 |
# File 'lib/binary_search/list.rb', line 300 def max last end |
#min ⇒ Object?
Get the minimum value in the list
This method returns the smallest element in the list. It has a time complexity of O(log n).
314 315 316 |
# File 'lib/binary_search/list.rb', line 314 def min first end |
#pop ⇒ Object?
Remove and return the last element in the list
This method removes and returns the largest element in the list. It has a time complexity of O(log n).
247 248 249 250 251 252 253 |
# File 'lib/binary_search/list.rb', line 247 def pop return nil if empty? last_value = last @tree.remove(last_value) @size -= 1 if @size last_value end |
#shift ⇒ Object?
Remove and return the first element in the list
This method removes and returns the smallest element in the list. It has a time complexity of O(log n).
266 267 268 269 270 271 272 |
# File 'lib/binary_search/list.rb', line 266 def shift return nil if empty? first_value = first @tree.remove(first_value) @size -= 1 if @size first_value end |
#size ⇒ Integer
Get the size of the list
This method returns the number of elements in the list. It has a time complexity of O(1) if the size is cached, or O(n) otherwise.
114 115 116 |
# File 'lib/binary_search/list.rb', line 114 def size @size ||= inorder_traversal(@tree.root).size end |
#sum ⇒ Numeric
Calculate the sum of all elements in the list
This method returns the sum of all elements in the list. It has a time complexity of O(n).
328 329 330 |
# File 'lib/binary_search/list.rb', line 328 def sum to_a.sum end |
#to_a ⇒ Array
Convert the list to an array
This method returns an array representation of the list in sorted order. It has a time complexity of O(n).
100 101 102 |
# File 'lib/binary_search/list.rb', line 100 def to_a inorder_traversal(@tree.root) end |
#uniq ⇒ BinarySearch::List
Create a new list with duplicate elements removed
This method returns a new list with all duplicate elements removed. It has a time complexity of O(n log n).
358 359 360 |
# File 'lib/binary_search/list.rb', line 358 def uniq self.class.new(to_a.uniq) end |
#unshift(value) ⇒ BinarySearch::List
Insert a value at the beginning of the list
This method inserts a value at the beginning of the list. It has a time complexity of O(log n).
285 286 287 288 |
# File 'lib/binary_search/list.rb', line 285 def unshift(value) insert(value) self end |
#|(other) ⇒ BinarySearch::List
Compute the union of two lists
This method returns a new list containing unique elements from both lists. It has a time complexity of O(n log n + m log m), where n and m are the sizes of the lists.
436 437 438 |
# File 'lib/binary_search/list.rb', line 436 def |(other) self.class.new(to_a | other.to_a) end |