好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Ruby實現的最優二叉查找樹算法

Ruby實現的最優二叉查找樹算法

熱門標簽:高德地圖標注客服 白銀外呼paas系統 百度地圖標注自定義圖片 徐州網絡外呼系統哪個好 地圖標注賺錢項目注冊 常德電銷平臺外呼系統軟件價格 電銷機器人廠商代理 滴滴外呼系統 湖州u友防封電銷卡

算法導論上的偽碼改寫而成,加上導論的課后練習第一題的解的構造函數。

復制代碼 代碼如下:

#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]}."

您可能感興趣的文章:
  • Ruby實現的各種排序算法
  • ruby實現的插入排序和冒泡排序算法
  • Ruby實現的矩陣連乘算法
  • Ruby實現二分搜索(二分查找)算法的簡單示例
  • Ruby實現的3種快速排序算法
  • Ruby實現的合并排序算法
  • Ruby實現的圖片濾鏡算法代碼

標簽:三沙 遼寧 荊門 普洱 張家界 公主嶺 永州 梧州

巨人網絡通訊聲明:本文標題《Ruby實現的最優二叉查找樹算法》,本文關鍵詞  Ruby,實現,的,最優,二叉,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Ruby實現的最優二叉查找樹算法》相關的同類信息!
  • 本頁收集關于Ruby實現的最優二叉查找樹算法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 国产精品久久久久久吹潮小仓菜 | 草莓精品免费AV在线播放| 嗯啊老板| 日本特一级毛片免费看| 精品国产丝袜自在线拍国| 前后夹击3p好爽| 大乳丰满人妻中文字幕日本冈江凛| 偷窥娇妻偷人叫床刺激小说| 中文无码亚洲精品字幕| 强制侵犯亲女在线观看| 一级寡妇乱色毛片全场免费| 欧美同志gaypronvideos| 欧美三级小说| 亚洲AV一区二区天天摸日日| 亚洲精品国产一区| 播放久久国产乱子伦精品| 韩国护士xxxxhd| 人獸videossex丰满极品| 亚洲女人老师毛茸茸HD| 1024国产你懂的日韩| 免费在线影院| 91吃瓜事件黑料吃瓜网曝门| 在线观看免费A片免费视频| 我是幸存者| 隔壁老王av无码精品线院| 国产一级爱c片免费播放| 国产真人一级a爱做片视频| 免费a级试看片| 亚洲AV久久艾草欧美| 一级特黄特黄的大片免费| 欧洲在线观看在线视频吗| jiZZjiZZ日本护士办公室| 久久精品日产高清版的功能介绍 | 婷婷久久综合九色综合色多多蜜臀 | 国产第一页屁屁影院| 女教师在办公室被躁| 黄色小说在线阅读| 亚洲中文无码亚洲人成人二区| 色噜噜狠狠一区二区三区AV蜜芽 | 国产凌凌漆国语完整版免费观看| 国产精品久久久久久久久久久久午衣片|