dtpoius74857 2014-04-30 02:56
浏览 75
已采纳

将二进制树编码为Json

I've stored a bunch of data in my database to draw a binary tree in a html canvas


Idx / Name

1 Apple

2 Bee

3 Cafe

4 Diamond

8 East

9 Game

16 Hobby


Here, idx represents the location of items in a binary tree. So the data above looks something like this in a tree


               1.Apple
               /     \
            2.Bee    3.Cafe
             /
      4.Diamond
       /      \
  8.East    9.Game
     /
16.Hobby

Now, I need to encode that database rows into a json format:

{
    id: "1",
    name: "Apple",
    data: {},
    children: [{
                   id: "2",
                   name: "Bee",
                   data: {},
                   children: [{
                       id: "4",
                       name: "Diamond",
                       data: {},
                       children: [{
                         // East/Game/Hobby comes here in the same manner...
                       }]
                   }]
               },
               {
                   id: "3",
                   name: "Cafe",
                   data: {},
                   children: [] // has no children
               }]
}

What I've tried was creating an array of arrays and going through all the values descending order by grab a vale and put it into its parent array and remove it from the array. So, my pseudo code was something like this...

nodeArray = [1,2,3,4,8,9,16];  <-each node is an object with needed data contained.
treeArray = [........]  <- arrays with key=>each index / value=>empty
while(nodeArray size is larger than 1) // 1 = the top most value 
{
    grab the last node from nodeArray
    parent_idx = (int)(last one id / 2)
    push the last node into the treeArray[parent_idx]
    pop the used index
}

Then, I will have treeArray something like this

treeArray = [
  1:[2,3]
  2:[4]
  4:[8,9]
  8:[16]
]

...this is not the array-converted binary tree I was looking for.

So, I need to go through treeArray in desc order and relocate them... Yup. I know I'm getting messed up here :( It's getting more complicated and harder to understand.

Would be there more elegant and simpler way to do it? :(

  • 写回答

1条回答 默认 最新

  • doumi6685 2014-04-30 04:53
    关注

    I ended up using javascript and loop through each node and calling the following function

    var objlist = {};
    function buildTree(id, parent_id, data)
    {
       if(id in objlist) alert("It already exists!");
       objlist[id] = { id: id, data: data, children: [] };
       if (parent_id in objlist)
       {
          objlist[parent_id].children.push(objlist[id]);
       }
    }
    

    where parent_id is id/2.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程