All Projects → piotrmurach → splay_tree

piotrmurach / splay_tree

Licence: MIT License
A self-balancing binary tree optimised for fast access to frequently used nodes.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to splay tree

tree.hh
An STL-like C++ header-only tree library
Stars: ✭ 79 (+216%)
Mutual labels:  tree
AhaAlgorithms
《啊哈算法》书上代码
Stars: ✭ 47 (+88%)
Mutual labels:  tree
lens-jekyll-theme
A Jekyll version of the "Lens" theme by HTML5 UP.
Stars: ✭ 56 (+124%)
Mutual labels:  ruby-gem
tacky
Primitive Object Memoization for Ruby
Stars: ✭ 14 (-44%)
Mutual labels:  ruby-gem
slack widgets
An abstraction of the JSON structure needed to create widgets in Slack message attachments
Stars: ✭ 14 (-44%)
Mutual labels:  ruby-gem
gitree
Print a directory tree that shows Git status and ignores files dictated by .gitignore.
Stars: ✭ 32 (+28%)
Mutual labels:  tree
AlgoDaily
just for fun
Stars: ✭ 118 (+372%)
Mutual labels:  tree
kdtree
A k-d tree implementation in Go.
Stars: ✭ 98 (+292%)
Mutual labels:  tree
AdTree
Accurate, Detailed, and Automatic Modelling of Laser-Scanned Trees
Stars: ✭ 88 (+252%)
Mutual labels:  tree
egis
Egis - a handy Ruby interface for AWS Athena
Stars: ✭ 38 (+52%)
Mutual labels:  ruby-gem
geeks-for-geeks-solutions
✅ My own Amazon, Microsoft and Google SDE Coding challenge Solutions (offered by GeeksForGeeks).
Stars: ✭ 246 (+884%)
Mutual labels:  tree
filtered
Filters ActiveRecord queries in a nice way
Stars: ✭ 28 (+12%)
Mutual labels:  ruby-gem
rspec n
A ruby gem that runs RSpec N times.
Stars: ✭ 37 (+48%)
Mutual labels:  ruby-gem
go-inet
A Go library for reading, formatting, sorting, lookup and converting IP-addresses and IP-blocks
Stars: ✭ 14 (-44%)
Mutual labels:  tree
arask
Automatic RAils taSKs.
Stars: ✭ 31 (+24%)
Mutual labels:  ruby-gem
vue-sortable-tree
vue tree draggable, drag item sort
Stars: ✭ 87 (+248%)
Mutual labels:  tree
xmas-tree
XMAS Tree from stacked ws2812 rings driven by a Digispark
Stars: ✭ 22 (-12%)
Mutual labels:  tree
ak-vue3
组件库包含了 AutoForm 自动表单、BackTop 返回顶部、Breadcrumb 面包屑、 Button 按钮、Cascader 级联选择器、Checkbox 多选框、Collapse 折叠面板、ColorPicker 颜色选择器、DataPicker 时间选择器、Dialog 弹层对话框、Alert 弹框、Echarts 图形图表、Form 表单、Input 输入框、Lazy 图片延时加载、Loading 加载等待、Menu 菜单、Pagination 分页、Progress 进度条、Radio 单选框、Select 选择器、Steps 步骤条、Swiper 图片轮播、Switch 开关、Table 表格、Tabs 标签页、Textarea 文本框、Tooltip 提示、Tr…
Stars: ✭ 24 (-4%)
Mutual labels:  tree
EurekaTrees
Visualizes the Random Forest debug string from the MLLib in Spark using D3.js
Stars: ✭ 37 (+48%)
Mutual labels:  tree
php-tools
Some code snippets that are often used in PHP
Stars: ✭ 25 (+0%)
Mutual labels:  tree

SplayTree

Gem Version Actions CI Build status Maintainability Coverage Status Inline docs

A self-balancing binary tree optimised for fast access to frequently used nodes. Useful for implementing caches and garbage collection algorithms.

Features

  • Familiar Hash like access
  • Easy instantiation with default value

Installation

Add this line to your application's Gemfile:

gem "splay_tree"

And then execute:

$ bundle

Or install it yourself as:

$ gem install splay_tree

Contents

1. Usage

SplayTree operations are similar to that of Hash:

tree = SplayTree.new
tree[:foo] = :bar

tree[:foo]  # => :bar
tree.size   # => 1

1.1 insert

To assign a value to a given key do the following:

tree = SplayTree.new
tree[:foo] = 1
tree[:bar] = 2

Note: The inserted key will be subjected to splaying, which means the tree will be rearranged to help with quicker access on subsequent calls.

1.2 fetch

To retrieve a value at a given key do:

tree = SplayTree.new
tree[:foo]  #  => nil

tree[:foo] = 1
tree[:foo]  #  => 1

Note: Frequently accessed keys will move nearer to the root where they can be accessed more quickly.

1.3 default

You can set a default value for a missing key. This can be done during initialization:

tree = SplayTree.new
tree.default  # => UndefinedValue

tree = SplayTree.new(1)
tree.default  # => 1
tree[:foo]    # => 1

Or using default method:

tree = SplayTree.new
tree.default = 1

tree[:foo] # => 1

You can also use a block to set the default value:

tree = SplayTree.new
tree.default_proc  # => nil

tree = SplayTree.new { 1 }
tree.default_proc  # => #<Proc...>
tree[:foo]         # => 1

1.4 delete

In order to remove an entry from a splay tree use delete method. If a key is not found, the default value is returned, otherwise nil.

tree = SplayTree.new
tree[:foo] = 1
tree.delete(:foo)  # => 1
tree.delete(:bar)  # => nil

1.5 empty?

Use empty? to check if a tree contains any elements:

tree = SplayTree.new
tree.empty?  # => true

tree[:foo] = 1
tree.empty?  # => false

1.6 each

Use each method to iterate over all tree nodes like so:

tree = SplayTree.new
tree[:foo] = 1
tree[:bar] = 2

tree.each { |key, value| puts "#{key}: #{value}" }
# =>
# bar: 2
# foo: 1

You can also use each_key and each_value to enumerate only keys or values:

tree.each_key { |key| ... }
tree.each_value { |value| ... }

If no block is given, an enumerator is returned instead.

Contributing

  1. Fork it ( https://github.com/piotrmurach/splay_tree/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Code of Conduct

Everyone interacting in the SplayTree project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Copyright

Copyright (c) 2014 Piotr Murach. See LICENSE for further details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].