<!-- index.html -->
<html>
<head>
  <meta charset="utf-8">
</head>
<body>
  <script type="text/javascript">


      var list = [1, 2, 3, 4, 5, 6, 7, 8];

      function make_sort_tree(arr) {
          var len = arr.length,
              mid = parseInt(len / 2),
              node = {
                  value: arr[mid]
              };
        if(mid > 0) { node['leftChild'] = make_sort_tree(arr.slice(0, mid)); }
        if(len > 2) { node['rightChild'] = make_sort_tree(arr.slice(mid + 1, len)); };
          return node;
      }

      function serach_tree(node) {
          if(node) {
              var ret = [node['value']];
              var leftList = serach_tree(node['leftChild']);
              var rightList = serach_tree(node['rightChild']);
              return leftList.concat(ret).concat(rightList);
          } else {
              return [];
          }
      }

      var root = make_sort_tree(list);
      console.log(root);
      var ret = serach_tree(root);
      console.log(ret);


  </script>
</body>
</html>

炑木
1.4k 声望9 粉丝

« 上一篇
邮箱自动补全
下一篇 »
LazyMan