gem algorithms010 Containers::RubyDeque 使用

gem algorithms010 Containers::RubyDeque 使用
算法部分大致看了一下,进入具体的数据结构中去,先翻了deque的源码,摸不着头绪,发现,还是要先了解用法,再了解如何实现。如是,还是读spec下的内容。
打开spec文件,再在irb中进行测试。
发现deque不知是啥玩意儿。
irb(main):042:0> deque.push_front(1)
=> 1
irb(main):043:0> deque.push_front(2)
=> 2
irb(main):044:0> deque.pop_front
=> 2
irb(main):045:0> deque.pop_front
=> 1
好象是一个栈。
irb(main):046:0> deque.push_back(10)
=> 10
irb(main):047:0> deque.push_back(20)
=> 20
irb(main):048:0> deque.pop_back
=> 20
irb(main):049:0> deque.pop_back
=> 10
再从后面写数据,看到没,也是一个栈。
就是两头都是栈的数据结构。
不知这种结构有何用,我还是先去翻翻书吧。

CBst不知是啥,可惜居然报错了。先不管它。
irb(main):050:0> bst=Containers::CBst.new
Traceback (most recent call last):
        2: from /usr/bin/irb:11:in `<main>'
        1: from (irb):50
NameError (uninitialized constant Containers::CBst)

这个是堆,堆排序中的堆。很好玩的。你看。
irb(main):052:0> heap=Containers::MaxHeap.new
=> #<Containers::MaxHeap:0x00007ffff27d00d8 @compare_fn=#<Proc:0x00007ffff27d0010@/var/lib/gems/2.5.0/gems/algorithms-0.6.1/lib/containers/heap.rb:433>, @next=nil, @size=0, @stored={}>
irb(main):053:0> heap.push(3)
=> 3
irb(main):054:0> heap.push(2)
=> 2
irb(main):055:0> heap.push(25)
=> 25
irb(main):056:0> heap.push(4)
=> 4
irb(main):057:0> heap.pop
=> 25
irb(main):058:0> heap.pop
=> 4
irb(main):059:0> heap.pop
=> 3
irb(main):060:0> heap.pop
=> 2
PriorityQueue好象是优先队列,就是有个优先级,进去以后,会按排行级自动排序,这样取出数据时,优先级高的先取出来。测试例子如下:
irb(main):002:0> q=Containers::PriorityQueue.new
=> #<Containers::PriorityQueue:0x00007fffeebc4bc0 @heap=#<Containers::Heap:0x00007fffeebc4af8 @compare_fn=#<Proc:0x00007fffeebc4b48@/var/lib/gems/2.5.0/gems/algorithms-0.6.1/lib/containers/priority_queue.rb:18 (lambda)>, @next=nil, @size=0, @stored={}>>
irb(main):003:0> q.size
=> 0
irb(main):004:0> q.push("a",1)
=> "a"
irb(main):005:0> q.push("b",3)
=> "b"
irb(main):006:0> q.push("c",2)
=> "c"
irb(main):007:0> q.size
=> 3
irb(main):008:0> q.pop
=> "b"
irb(main):009:0> q.pop
=> "c"
irb(main):010:0> q.pop
=> "a"

RubyRBTreeMap不知是什么东西?平衡二叉树?不像。
irb(main):011:0> tree=Containers::RubyRBTreeMap.new
=> #<Containers::RubyRBTreeMap:0x00007fffeeb88648 @root=nil, @height_black=0>

irb(main):014:0> a=[1,3,2]
=> [1, 3, 2]
irb(main):015:0> a.each {|x| tree[x]=x}
=> [1, 3, 2]
irb(main):016:0> tree
=> #<Containers::RubyRBTreeMap:0x00007fffeeb88648 @root=#<Containers::RubyRBTreeMap::Node:0x00007fffeeb2d3d8 @key=2, @value=2, @color=:black, @left=#<Containers::RubyRBTreeMap::Node:0x00007fffeeb2d360 @key=1, @value=1, @color=:black, @left=nil, @right=nil, @size=1, @height=1>, @right=#<Containers::RubyRBTreeMap::Node:0x00007fffeeb2d3b0 @key=3, @value=3, @color=:black, @left=nil, @right=nil, @size=1, @height=1>, @size=3, @height=2>, @height_black=2>
irb(main):017:0> tree.height
=> 2
irb(main):018:0> tree.size
=> 3
irb(main):019:0> tree.max_key
=> 3
irb(main):020:0> tree.min_key
=> 1

trie也不知是什么高级玩意儿。

irb(main):023:0> trie=Containers::Trie.new
=> #<Containers::Trie:0x00007fffee73f460 @root=nil>
irb(main):024:0> trie.get("anything")
=> nil
irb(main):025:0> trie.push("Hello","world")
=> "world"
irb(main):026:0> trie.push("Hilly","world")
=> "world"
irb(main):027:0> trie.push("Hello,brother","world")
=> "world"
irb(main):028:0> trie.push("Hello,bob","world")
=> "world"
irb(main):029:0> trie.longest_prefix("Hel")
=> ""
irb(main):030:0> trie.longest_prefix("H*ll.")
=> ""
irb(main):031:0> trie.wildcard("H*ll.")
=> ["Hello", "Hilly"]
irb(main):032:0> trie.wildcard("Hel")
=> []
我以为,作者先写测试用例,很好。因为学习者通过读用例,学会了软件的使用。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
苹果公司所用字体大全苹果公司所用字体大全苹果公司所用字体大全苹果公司所用字体大全苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值