/RubyStructures

A Ruby gem for implementations of common data structures.

Primary LanguageRubyMIT LicenseMIT

Ruby Structures

Ruby implementations of common data structures.

Gem Version

Installation & Usage

Install Ruby Structures:

gem install ruby_structures

Or add it to your Gemfile:

gem 'ruby_structures'

Require Ruby Structures in your file:

require 'ruby_structures'

Available Data Structures

  • Stack
  • Queue
  • Linked List
  • Binary Tree
  • LRU Cache
  • Heap
  • Priority Queue
  • Graph
  • Weighted Graph

More to come...

Stack

A Stack is a LIFO (last in, first out) container.

Stack API

  1. #to_a
  2. #==
  3. #empty?
  4. #push(el)
  5. #<<(el)
  6. #pop
  7. #peek
  8. #length
  9. #include?(el)

Queue

A Queue is a FIFO (first in, first out) container.

Queue API

  1. #to_a
  2. #==
  3. #empty?
  4. #enqueue(el)
  5. #<<(el)
  6. #dequeue
  7. #peek
  8. #length
  9. #include?(el)

Linked List

A Linked List is an ordered collection of items, or nodes, where the ordering is determined by links between the nodes. The Ruby Structures implementation of a Linked List is doubly linked, meaning that each node has a link to the node after it as well as the node preceding it.

Linked List API

  1. #to_a
  2. #empty?
  3. #length
  4. #first
  5. #last
  6. #append(key, val)
  7. #prepend(key, val)
  8. #add_after_key(ref_key, key, val)
  9. #add_before_key(ref_key, key, val)
  10. #find_by_key(key)
  11. #find_by_val(val)
  12. #include_key?(key)
  13. #include_val?(val)
  14. #remove(key)
  15. #update(key, new_val)
  16. #each(&prc)

Binary Tree

A Binary Tree is a tree in which each Node may have a maximum of two children.

Binary Tree API

  1. ::from_array(array)
  2. #depth_first_search
  3. #breadth_first_search

LRU Cache

An LRU Cache is an ordered container that combines a Hash and a Linked List to provide insertion, deletion and inclusion methods in constant time.

LRU Cache API

  1. #to_a
  2. #empty?
  3. #length
  4. #first
  5. #last
  6. #append(key, val)
  7. #prepend(key, val)
  8. #add_after_key(ref_key, key, val)
  9. #add_before_key(ref_key, key, val)
  10. #remove(key)
  11. #find(key)
  12. #include?(key)

Heap

A Heap is a tree-based data structure that adheres to the Heap principle. Ruby Structures implements a Min Heap, which means that each parent element in the heap is of lesser value than each of its child elements. A Min Heap always has access to its minimum element.

Heap API

  1. ::from_array(array)
  2. #to_a
  3. #empty?
  4. #length
  5. #peek
  6. #insert(el)
  7. #insert_multiple(array)
  8. #extract
  9. #find(el)
  10. #include?(el)
  11. #merge(other_heap)

Priority Queue

A Priority Queue is a specialized queue where each element has a 'priority' attribute. A Priority Queue always has access to its highest priority element.

Priority Queue API

  1. ::from_array(array)
  2. ::from_hash(hash)
  3. #to_a
  4. #empty?
  5. #length
  6. #peek
  7. #insert(data, priority)
  8. #extract
  9. #find(data)
  10. #include?(data)
  11. #merge(other_queue)

Graph

A Graph is a set of vertices and a set vertex pairs, or edges, that connect vertices to one another. Ruby Structures implements both an Undirected Graph (unordered edge pairs) and a Directed Graph (ordered edge pairs).

Graph & Directed Graph API

  1. #add_vertex(id)
  2. #delete_vertex(id)
  3. #create_edge(id1, id2)
  4. #delete_edge(id1, id2)
  5. #adjacent?(id1, id2)
  6. #adjacent_vertices(id)
  7. #depth_first_search(target_id, start_id, &prc)
  8. #breadth_first_search(target_id, start_id, &prc)

Weighted Graph

A Weighted Graph is a Graph in which each edge is assigned a weight. Ruby Structures implements both the undirected and directed varieties of a Weighted Graph.

Weighted Graph & Weighted Directed Graph API

  1. #add_vertex(id)
  2. #delete_vertex(id)
  3. #create_edge(id1, id2, weight)
  4. #delete_edge(id1, id2, weight)
  5. #adjacent?(id1, id2)
  6. #adjacent_vertices(id)
  7. #highest_weight_adjacent(id)
  8. #lowest_weight_adjacent(id)