NEWS INFORMATION

新闻资讯

Ruby实现的最优二叉查找树算法

作者:admin 发布时间:2019-12-07




















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{array.new}
root = array.new{array.new}






def optimalbst
  w = array.new{array.new}
  for i in
  e[i][i - 1] = q[i - 1]
  w[i][i - 1] = q[i - 1]
  end
  for l in
  for i in
  j = i + l -1
  e[i][j] = 1 / 0.0
  w[i][j] = w[i][j - 1] + p[j] + q[j]
  for r in
  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
  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
  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
  end
 
end























optimalbst
printbst
puts " the expected cost is #{e[1][a.size-1]}."



详解如何用模块化的方式写vuejs
.net带事件的对象BinaryFormatter 序列化失败
扫一扫,关注我们

友情链接 :
 
地址:江苏省南京市玄武区玄武湖
电话:
邮箱:9490489@qq.com
Q Q:9490489