构造次优查找树
来源:互联网 发布:qq三国js高配灵魂搭配 编辑:程序博客网 时间:2024/05/18 03:10
似乎有些错误,但是错在哪了呢?
#include <iostream>#include <cmath>using namespace std;const int NUM = 9;int value[NUM] = {1,2,3,4,5,6,7,8,9};float weight[NUM] = {1,1,2,5,3,4,4,3,5};float sum_weight[NUM];void init_sum_weight(){ int sum = 0; for(int i = 0; i <= NUM; i++){ sum += weight[i]; sum_weight[i] = sum; }}struct Tree{ float weight; int value; Tree* left; Tree* right; ~Tree(){delete left; delete right;}}(*tree);void mid(Tree (*tree)){ if(!tree)return; cout<<tree->value<<"("; if(tree->left) cout<<tree->left->value; cout<<","; if(tree->right) cout<<tree->right->value; cout<<")"<<" "; mid(tree->left); mid(tree->right);}void pre(Tree (*tree)){ if(!tree)return; pre(tree->left); cout<<tree->value<<" "; pre(tree->right);}//Δpi = sum[h] - sum[i] - sum[i - 1];void constructTree(Tree *(*tree), int h, int l){ int min, dw, hit = l; if(l != 0){ min = abs(sum_weight[h] - sum_weight[l] - sum_weight[l-1]);//Δpl dw = abs(sum_weight[h] + sum_weight[l-1]); }else{ min = abs(sum_weight[h] - sum_weight[l]); dw = abs(sum_weight[h]); } for(int i = l+1; i <=h; i++){ int new_min = abs(sum_weight[h] - sum_weight[i] - sum_weight[i-1]);//Δpi if(new_min < min){ min = new_min; hit = i; } } (*tree) = new Tree; (*tree)->weight = weight[hit]; (*tree)->value = value[hit]; cout<<"value="<<value[hit]<<endl; if(hit == l){ (*tree)->left = NULL;//左 }else{ constructTree(&(*tree)->left, hit-1, l); } if(hit == h){ (*tree)->right = NULL; }else{ constructTree(&(*tree)->right, h, hit+1); }}int main(){ Tree (*tree); init_sum_weight(); constructTree(&tree, NUM-1, 0); cout<<"mid:"; mid(tree); cout<<endl; cout<<"pre:"; pre(tree); cout<<endl; delete tree;}
错在哪里呢?也许没错。。
- 构造次优查找树
- 构造次优查找树
- 构造次优查找树的算法
- 数据结构笔记-构造次优查找树
- 数据结构——次优查找树的构造
- 次优查找树
- 次优查找树
- 次优查找树
- 次优查找树
- 数据结构_查找_静态查找数表_构造次优查找树
- 【数据结构-查找】次优排序树
- 数据结构_查找_次优查找树
- 次优查找树的查找原理
- 【数据结构】算法9.3-9.4 静态树表-构造次优查找树
- C++代码,数据结构-次优查找树
- 第九章(2).次优查找树
- 次优查找树(Nearly Optimal Search Tree)
- 次优查找树的原理是什么?
- 【Perl读书笔记】基本数组,初始化
- 浅谈设备、驱动的加载和匹配
- C#学习之类
- 解决mysql“Access denied for user 'root'@'localhost'”
- C# 多线程通讯
- 构造次优查找树
- 提高 Linux 上 socket 性能
- 自定义UIButton--iPhone按钮控件点击效果写法
- 使用 Adobe FlexPMD 审核你的代码品质
- drupal 中文命名的图片上传显示出错
- 关于CTreeCtrl::DeleteAllItems()
- javascript 将object转换为字符串JSON.stringify
- 嵌入式Linux文件系统详细介绍
- 《代码整齐之道》读书笔记