最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Ruby實(shí)現(xiàn)的最優(yōu)二叉查找樹算法

 更新時(shí)間:2015年05月22日 11:14:33   投稿:junjie  
這篇文章主要介紹了Ruby實(shí)現(xiàn)的最優(yōu)二叉查找樹算法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下

算法導(dǎo)論上的偽碼改寫而成,加上導(dǎo)論的課后練習(xí)第一題的解的構(gòu)造函數(shù)。

復(fù)制代碼 代碼如下:

#encoding: utf-8
=begin
author: xu jin
date: Nov 11, 2012
Optimal Binary Search Tree
to find by using EditDistance algorithm
refer to <<introduction to algorithms>>
example output:
"k2 is the root of the tree."
"k1 is the left child of k2."
"d0 is the left child of k1."
"d1 is the right child of k1."
"k5 is the right child of k2."
"k4 is the left child of k5."
"k3 is the left child of k4."
"d2 is the left child of k3."
"d3 is the right child of k3."
"d4 is the right child of k4."
"d5 is the right child of k5."

The expected cost is 2.75. 
=end

INFINTIY = 1 / 0.0
a = ['', 'k1', 'k2', 'k3', 'k4', 'k5']
p = [0, 0.15, 0.10, 0.05, 0.10, 0.20]
q = [0.05, 0.10, 0.05, 0.05, 0.05 ,0.10]
e = Array.new(a.size + 1){Array.new(a.size + 1)}
root = Array.new(a.size + 1){Array.new(a.size + 1)}

def optimalBST(p, q, n, e, root)
  w = Array.new(p.size + 1){Array.new(p.size + 1)}
  for i in (1..n + 1)
    e[i][i - 1] = q[i - 1]
    w[i][i - 1] = q[i - 1]
  end
  for l in (1..n)
    for i in (1..n - l + 1)
      j = i + l -1
      e[i][j] = 1 / 0.0
      w[i][j] = w[i][j - 1] + p[j] + q[j]
      for r in (i..j)
        t = e[i][r - 1] + e[r + 1][j] + w[i][j]
        if t < e[i][j]
          e[i][j] = t
          root[i][j] = r
        end
      end
    end
  end
end

def printBST(root, i ,j, signal)
  return if i > j
  if signal == 0
   p "k#{root[i][j]} is the root of the tree."
   signal = 1
  end
  r = root[i][j]
  #left child
  if r - 1< i
    p "d#{r - 1} is the left child of k#{r}."
  else
    p "k#{root[i][r - 1]} is the left child of k#{r}."
    printBST(root, i, r - 1, 1 )
  end
  #right child
  if r >= j
     p "d#{r} is the right child of k#{r}."
  else
    p "k#{root[r + 1][j]} is the right child of k#{r}."
    printBST(root, r + 1, j, 1)
  end
 
end

optimalBST(p, q, p.size - 1, e, root)
printBST(root, 1, a.size-1, 0)
puts "\nThe expected cost is #{e[1][a.size-1]}."

相關(guān)文章

  • Ruby是什么以及如何使用

    Ruby是什么以及如何使用

    這篇文章主要介紹了Ruby是什么以及如何使用,Ruby和Ruby on Rails 在早期就出現(xiàn)在Web開發(fā)領(lǐng)域了,然而,雖然現(xiàn)在JavaScript和Python都占據(jù)了主導(dǎo)地位,Ruby還是仍然占有一席之地
    2017-04-04
  • ruby on rails 代碼技巧

    ruby on rails 代碼技巧

    對(duì)于rails的一些使用技巧的代碼
    2009-01-01
  • Ruby簡明教程之方法(Method)介紹

    Ruby簡明教程之方法(Method)介紹

    這篇文章主要介紹了Ruby簡明教程之方法(Method)介紹,ruby的方法分為實(shí)例方法、類方法、函數(shù)方法等,本文分別做了講解,需要的朋友可以參考下
    2014-06-06
  • Ruby中proc和lambda的兩個(gè)區(qū)別

    Ruby中proc和lambda的兩個(gè)區(qū)別

    這篇文章主要介紹了Ruby中proc和lambda的兩個(gè)區(qū)別,本文講解了在proc和lambda中,return關(guān)鍵字有不同含義、檢查參數(shù)的方式不同兩個(gè)重要區(qū)別,需要的朋友可以參考下
    2015-05-05
  • Ruby中嵌套對(duì)象轉(zhuǎn)換成json的方法

    Ruby中嵌套對(duì)象轉(zhuǎn)換成json的方法

    這篇文章主要介紹了Ruby中嵌套對(duì)象轉(zhuǎn)換成json的方法,同時(shí)介紹了普通對(duì)象to_json的方法,需要的朋友可以參考下
    2014-06-06
  • Ruby 多線程的潛力和弱點(diǎn)分析

    Ruby 多線程的潛力和弱點(diǎn)分析

    這篇文章主要介紹了Ruby 多線程的潛力和弱點(diǎn)分析,本文講解了Ruby 多線程和 IO Block、Ruby GIL 的影響、JRuby 去除了 GIL、Ruby 多線程總結(jié)等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • 實(shí)例解析Ruby設(shè)計(jì)模式編程中Strategy策略模式的使用

    實(shí)例解析Ruby設(shè)計(jì)模式編程中Strategy策略模式的使用

    這篇文章主要介紹了Ruby設(shè)計(jì)模式編程中Strategy策略模式的使用實(shí)例,Strategy模式在Ruby on Rails框架開發(fā)中也經(jīng)常用到,需要的朋友可以參考下
    2016-03-03
  • ruby 簡單例子

    ruby 簡單例子

    ruby 簡單例子...
    2007-11-11
  • 深入理解Ruby中的block概念

    深入理解Ruby中的block概念

    這篇文章主要介紹了深入理解Ruby中的block概念,文中給出了Javascript代碼塊與Ruby代碼塊的對(duì)比,需要的朋友可以參考下
    2015-04-04
  • Ruby 迭代器知識(shí)匯總

    Ruby 迭代器知識(shí)匯總

    這篇文章主要介紹了Ruby 迭代器的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06

最新評(píng)論

墨脱县| 米脂县| 天祝| 东兴市| 巴塘县| 安多县| 麦盖提县| 灵宝市| 德昌县| 梓潼县| 东兴市| 新邵县| 淳安县| 沈阳市| 阳泉市| 舟曲县| 韶关市| 蕲春县| 宣恩县| 五原县| 施秉县| 双柏县| 松潘县| 宜城市| 慈溪市| 阳东县| 车致| 洪雅县| 阳曲县| 榆中县| 宜章县| 富裕县| 弥渡县| 长治县| 沁阳市| 华池县| 光泽县| 横山县| 沽源县| 吉水县| 信丰县|