Kaninchen
Simple implementation of data structures (and algorithms in future) in Ruby programming language.
Currently support data structures:
Currently working on data structures:
Installation
Add this line to your application's Gemfile:
gem 'kaninchen'
And then execute:
$ bundle
Or install it yourself as:
$ gem install kaninchen
Stack
Basic Usage
stack = Kaninchen::DataStructure::Stack.new
stack.push(1)
stack.push(2).push(3).push(4)
stack << 5 << 6 << 7
stack.size
stack.pop
stack.pop
stack.pop
empty_stack = Kaninchen::DataStructure::Stack.new
empty_stack.pop
empty_stack.pop!
Tree
Basic Usage
tree = Kaninchen::DataStructure::Tree.new 'ROOT'
tree.class
tree.root.class
tree.root.value
tree.root.root?
tree.root.tree_node?
tree.root.nil?
node = Kaninchen::DataStructure::Node.new 'CHILD'
node.tree_node?
node.nil?
tree.root << node
tree.root.children.size
tree.root.children[0] === node
node.parent === tree.root
node.nil?
node.tree_node?
node.tree === tree
node.root?
Attaching Nodes
Chained #<<
method:
tree.root << n1 << n2 << n3
tree.root.children.size
tree.root.children[0] === n1
tree.root.children[1] === n2
tree.root.children[2] === n3
n1.parent === tree.root
n2.parent === tree.root
n3.parent === tree.root
Array of nodes case:
tree.root << [n1, n2, n3]
tree.root.children.size
tree.root.children[0] === n1
tree.root.children[1] === n2
tree.root.children[2] === n3
n1.parent === tree.root
n2.parent === tree.root
n3.parent === tree.root
Hash of nodes case:
tree.node << { n1 => n2, n3 => nil }
tree.root.children.size
tree.root.children[0] === n1
tree.root.children[1] === n3
tree.root.children[0].size
tree.root.children[1].size
tree.root.children[0].children.size
tree.root.children[0].children[0] === n2
n1.parent === tree.root
n2.parent === n1
n3.parent === tree.root
Mixed structure case (array and hash composition):
tree.node << [n1, { n2 => [n3, n4], { n5 => nil } }]
Tree Node Order & Traversal
Available methods associated with tree node order and traversal is listed below:
Tree#nodes
: Default returns an array of Node
type objects which represents the preorder of the tree nodesTree#node_values
: Default returns an array of Node
type objects' values which represents the preorder of the tree nodesTree#each_node
: Default iterates the tree nodes in preorder format which requires a given blockTree#each_node_with_index
: Default iterates the tree nodes with index specified in preorder format which requires a given block
These methods can pass in optional parameter, when pass in:
:preorder
or :depth_first
: Preorder mode:inorder
: Inorder mode:postorder
: Postorder mode:levelorder
or :breadth_first
: Level order mode
Properties of Tree
Tree#height
: The height of the treeTree#leaves
: The leaves of the tree
Properties of Tree Nodes
Node#type
: Returns the symbol object :tree
Node#degree
: The degree of the tree node (which is the count of the subtrees of the node)Node#depth
: The depth of the tree node (which is the distance between the node and the tree root of the node)Node#height
: The height of the tree node (which is the longest path to the leaves of the subtree of the node)Node#subtree
: Returns the new tree type object which is the subtree of the nodeNode#path
: Returns the path from the node to the root node as an arrayNode#parent
: Returns the parent node of the nodeNode#children
: Returns the child nodes of the nodeNode#left_child
: Returns the left child node of the nodeNode#right_child
: Returns the right child node of the node. (If there are over two children, it will return the second child node in default)Node#leaf?
: Returns true
if the node is the leaf of the tree
License
The gem is available as open source under the terms of the MIT License.