JS 简单实现二叉树

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    //定义节点
    //
    class Node {
      constructor(key) {
        this.key = key; // 节点值
        this.left = null; // 左指针
        this.right = null; // 右指针
      }
    }

    class NodeTree {
      constructor() {
        this.root = null;
        this.list = []; //排序数组
      }
      //二叉树结构
      insertNodeParents(data) {
        const newNode = new Node(data)
        if (this.root == null) this.root = newNode
        else this.insertNode(this.root, newNode)
      }
      //二叉树结构
      insertNode(root, newNode) {
        if (root.key >= newNode.key) {
          if (root.left == null) root.left = newNode
          else this.insertNode(root.left, newNode)
        } else {
          if (root.right == null) root.right = newNode
          else this.insertNode(root.right, newNode)
        }
      }
      //二叉树中序遍历
      sortingNodes() {
        this.sortingNode(this.root)
      }
      sortingNode(root) {
        if (root) {
          this.sortingNode(root.right)
          this.list.push(root.key)
          this.sortingNode(root.left)
        }
      }
      //二叉树前序遍历
      sortingNodeBefore() {
        this.sortingNodeBefores(this.root)
      }
      sortingNodeBefores(root) {
        if (root) {
          console.log('left: ', root.key);
          this.sortingNodeBefores(root.left)
          this.sortingNodeBefores(root.right)
        }
      }
      //二叉树后序遍历
      sortingNodeAfter() {
        this.sortingNodeAfters(this.root)
      }
      sortingNodeAfters(root) {
        if (root) {
          this.sortingNodeAfters(root.left)
          this.sortingNodeAfters(root.right)
          console.log('left: ', root.key);
        }
      }
      //查找最大值
      getMax(root = this.root) {
        if (root.right == null) {
          return root.key
        }
        return this.getMax(root.right)
      }
      //查找最小值
      getMin(root = this.root) {
        if (root.left == null) {
          return root.key
        }
        return this.getMin(root.left)
      }
      //查找某一个值
      getSpecified(value) {
        return this.getSpecifieds(this.root, value)
      }
      getSpecifieds(root, value) {
        if (root == null) return false
        if (root.key > value) {
          return this.getSpecifieds(root.left, value)
        } else if (root.key < value) {
          return this.getSpecifieds(root.right, value)
        }
        return true
      }
    }
    const xcq = new NodeTree()
    const list = [19, 8, 5, 15, 12, 24, 45]
    list.forEach(i => xcq.insertNodeParents(i))
    xcq.sortingNodes(node => node.key)
    let max = xcq.getMax()
    let getMin = xcq.getMin()
    xcq.sortingNodeBefore()
    xcq.sortingNodeAfter()
    let isHas = xcq.getSpecified(11229)
    console.log('isHas: ', isHas);
    console.log('getMin: ', getMin);
    console.log('max: ', max);
    console.log(xcq)
  </script>
</body>

</html>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值