Introduction
Provided are some toy implementations for some basic computer science problems.
Tree data structures
Tree- enforces number of children per nodeTree::Node- references parent and children nodesBinaryTree- subclass ofTree; child_slots == 2CompleteBinaryTree- efficient Array implementation
Heap data structure
Implemented with a CompleteBinaryTree for storage using simple arithmetic to
determine array indices for parent and children. See the
heap example
which can be executed (among other examples) via rake examples.
Both minheaps and maxheaps are supported. The primary operations are
Heap#push and Heap#pop. My basic Vagrant VM gets over 350k pushes per
second, constant up past 1M pushes.
Fibonacci functions
Fibonacci.classic(n)- naive, recursiveFibonacci.cache_recursive(n)- as above, caching already computed resultsFibonacci.cache_iterative(n)- as above but iterativeFibonacci.dynamic(n)- as above but without a cache structure
Timer functions
Timer.now- usesProcess::CLOCK_MONOTONICif availableTimer.elapsed- provides the elapsed time to run a blockTimer.loop_average- runs a block repeatedly and provides the mean elapsed timeTimer.since- provides the elapsed time since a prior time
Fit functions
Fit.sigma- sums the result of a block applied to array valuesFit.error- returns a generic r^2 value, the coefficient of determinationFit.constant- fitsy = a + 0x; returns the mean and varianceFit.logarithmic- fitsy = a + b*ln(x); returns a, b, r^2Fit.linear- fitsy = a + bx; returns a, b, r^2Fit.exponentialfitsy = ae^(bx); returns a, b, r^2Fit.powerfitsy = ax^b; returns a, b, r^2