树(Tree,UVA 548)

题目描述:

题目思路:

1.使用数组建树 //递归

2.理解后序遍历和中序遍历,建立左右子树

3.dfs深度搜索找出权重最小的路径

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

const int maxv = 10000 + 10;
int in_order[maxv], post_order[maxv], lch[maxv], rch[maxv];
int i;

bool readlist(int* t){ //读入输入 
    string line;
    if(!getline(cin,line)) return false ;
    stringstream ss(line);
    i = 0;
    int x;
    while(ss >> x) t[i++] = x;    
    return i > 0 ;
}

int buildtree(int L1, int R1, int L2, int R2){//将读入的两行建树 
    if(L1 > R1) return 0; // 空树
    int root = post_order[R2] ;//后序遍历最后一个
    int p = L1;
      while(in_order[p] != root) p++; 
      int cnt = p-L1; // 左子树的结点个数
      lch[root] = buildtree(L1, p-1, L2, L2+cnt-1);
      rch[root] = buildtree(p+1, R1, L2+cnt, R2-1);
      return root ;
}

int best, best_sum; // 目前为止的最优解和对应的权和

void dfs(int u, int sum){//深度搜索 
    sum += u;
    if(!lch[u] && !rch[u]) { // 叶子
    if(sum < best_sum || (sum == best_sum && u < best)) 
        { best = u; best_sum = sum; }
      }
      if(lch[u]) dfs(lch[u], sum);
      if(rch[u]) dfs(rch[u], sum); 
}

int main(int argc, char *argv[])
{
    while(readlist(in_order)) {
        readlist(post_order);
        buildtree(0, i-1, 0, i-1);
        best_sum = 1000000000;
        dfs(post_order[i-1], 0);
        cout << best << "\n";
      }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/secoding/p/9532540.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要在 ECharts 中实现Tree)的点击节点功能,您可以按照以下步骤进行操作: 1. 首先,确保您已经引入了 ECharts 库,并在页面中创建了一个具有指定 ID 的容器,用于渲染图表。 2. 创建一个包含节点数据的 JSON 对象。每个节点应包含 `name` (节点名称)和 `children`(子节点)属性。 3. 使用 ECharts 的 `option` 对象配置项,设置图的基本配置,例如 `series` 类型为 `'tree'`,并指定 `data` 属性为您的节点数据。 4. 在 `series` 配置项中,使用 `expandAndCollapse` 属性来启用节点的展开和折叠功能。 5. 在 `series` 配置项中,使用 `label` 属性来设置节点的文本样式。 6. 在 `series` 配置项中,使用 `emphasis` 属性来设置节点的高亮样式。 7. 使用 ECharts 的 `on` 方法,监听 `'click'` 事件,并在回调函数中处理节点的点击事件。 下面是一个示例代码,演示了如何实现节点的点击功能: ```javascript // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('chart-container')); // 节点数据 var treeData = { name: 'Root', children: [ { name: 'Node 1', children: [ { name: 'Leaf 1-1' }, { name: 'Leaf 1-2' } ] }, { name: 'Node 2', children: [ { name: 'Leaf 2-1' }, { name: 'Leaf 2-2' } ] } ] }; // 配置项 var option = { series: [ { type: 'tree', data: [treeData], expandAndCollapse: true, label: { show: true, position: 'top', formatter: '{b}' }, emphasis: { focus: 'descendant' } } ] }; // 渲染图表 myChart.setOption(option); // 监听节点点击事件 myChart.on('click', function(params) { console.log('点击了节点:', params); // 根据需要执行相应的操作 }); ``` 请注意,这只是一个简单的示例,您可以根据实际需求进行更复杂的配置和处理。希望对您有所帮助!如果有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值