二叉树先序遍历

175 阅读1分钟

画gene heatmap的时候用到了一个插件 inchlib ,这个插件自带聚类的数据结构处理,画出来的样子如下:


参考链接: https://www.openscreen.cz/software/inchlib/inchlib_examples_summary_html

因这个插件满足不了以后的更多需求,决定改用d3去实现

d3需要的数据 是从inchlib给的数据进行处理 才能使用!

大概的数据格式是 : 

{

   parent: '',

   leftChild:'',

  rightChild:''

   ...

}

需要遍历这个二叉树,拿到一个array

js代码如下:

let self = this
      for (let k in this.$store.state.heatmapJson.heatmap_json_string.data.nodes) {
        let node = this.$store.state.heatmapJson.heatmap_json_string.data.nodes[k]
        if (('parent' in node) === false) {    // json没有parent属性,说明是根结点
          preOrder(node)
        }
      }
      function preOrder(node){
         if(node.distance === 0){
           self.temp.push(node.objects[0]);  // y 轴的gene名称
           for(let i = 0;i < node.features.length;i++) {
             self.value.push(node.features[i])   // 每一行的值
           }
         } else {
           preOrder(self.$store.state.heatmapJson.heatmap_json_string.data.nodes[node['left_child']]);
           preOrder(self.$store.state.heatmapJson.heatmap_json_string.data.nodes[node['right_child']]);
         }
       }