求哈夫曼树WPL的代码
时间: 2023-12-25 11:26:46 浏览: 88
哈夫曼树的代码
```pyth\ss N\ def __ini__(self, v, weigh):\ self.v = v\ self.weigh = weigh\ self.lef = N\ self.righ = N\n\f huffm_tr(arr):\ nodes = [N(v, weigh) for v, weigh i arr]\ whi (nodes) > 1\ nodes.sor(key=lamb x x.weigh)\ lef = nodes.pop()\ righ = nodes.pop()\ par = N(N, lef.weigh + righ.weigh)\ par.lef = lef\ par.righ = righ\ nodes.app(par)\ retur nodes[]\n\f wp(r, depth=):\ if r\ retur \ if r.lef r.righ\ retur r.weigh * depth\ retur wp(r.lef, depth+1) + wp(r.righ, depth+1)\n\rr = [('', 5), ('b', 4), ('', 3), ('', 2), ('', 1)]\r = huffm_tr(arr)\pri(wp(r))\```\
阅读全文