day22-24 二叉树

放一个二叉树的例子

<html>
  <body>
    <script>
      var tree = {
        id: 0,
        name: "root",
        left: {
          id: 1,
          name: "Simon",
          left: {
            id: 3,
            name: "Carl",
            left: {
              id: 7,
              name: "Lee",
              left: {
                id: 11,
                name: "Fate",
              },
            },
            right: {
              id: 8,
              name: "Annie",
              left: {
                id: 12,
                name: "Saber",
              },
            },
          },
          right: {
            id: 4,
            name: "Tony",
            left: {
              id: 9,
              name: "Candy",
            },
          },
        },
        right: {
          id: 2,
          name: "right",
          left: {
            id: 5,
            name: "Carl",
          },
          right: {
            id: 6,
            name: "Carl",
            right: {
              id: 10,
              name: "Kai",
            },
          },
        },
      };

      // 假设id和name均不会重复,根据输入name找到对应的id
      function findIdByName(name) {
        var id;
        function node(tree) {
          if (tree) {
            if (tree.name == name) {
              id = tree.id;
            }
            node(tree.left);
            node(tree.right);
          }
        }
        node(tree);
        return id;
      }
      console.log(findIdByName("Annie"));

      // 假设id和name均不会重复,根据输入id找到对应的name
      function findNameById(id) {
        var name;
        function node(tree) {
          if (tree) {
            if (tree.id == id) {
              name = tree.name;
            }
            node(tree.left);
            node(tree.right);
          }
        }
        node(tree);
        return name;
      }
      console.log(findNameById("8"));

      // 把这个对象中所有的名字以“前序遍历”的方式全部输出到console中
      var dlr = [];
      function getListWithDLR() {
        function node(tree) {
          if (tree) {
            dlr.push(tree.name);
            node(tree.left);
            node(tree.right);
          }
        }
        node(tree);
        console.log(dlr);
      }
      getListWithDLR();

      // 把这个对象中所有的名字以“中序遍历”的方式全部输出到console中
      var ldr = [];
      function getListWithLDR() {
        function node(tree) {
          if (tree) {
            node(tree.left);
            ldr.push(tree.name);
            node(tree.right);
          }
        }
        node(tree);
        console.log(ldr);
      }
      getListWithLDR();

      // 把这个对象中所有的名字以“后序遍历”的方式全部输出到console中
      var lrd = [];
      function getListWithLRD() {
        function node(tree) {
          if (tree) {
            node(tree.left);
            node(tree.right);
            lrd.push(tree.name);
          }
        }
        node(tree);
        console.log(lrd);
      }
      getListWithLRD();
    </script>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值