当前位置:首页 > JavaScript

js树实现

2026-01-14 14:24:59JavaScript

树的基本概念

树是一种非线性的数据结构,由节点和边组成。每个节点包含一个值和指向子节点的引用。树的顶部节点称为根节点,没有子节点的节点称为叶节点。

树的实现方式

在JavaScript中,树可以通过对象或类来实现。以下是两种常见的实现方式:

使用对象字面量

const tree = {
  value: 1,
  children: [
    {
      value: 2,
      children: []
    },
    {
      value: 3,
      children: [
        {
          value: 4,
          children: []
        }
      ]
    }
  ]
};

使用类

class TreeNode {
  constructor(value) {
    this.value = value;
    this.children = [];
  }

  addChild(childNode) {
    this.children.push(childNode);
  }
}

const root = new TreeNode(1);
const child1 = new TreeNode(2);
const child2 = new TreeNode(3);
const grandChild = new TreeNode(4);

root.addChild(child1);
root.addChild(child2);
child2.addChild(grandChild);

树的遍历方法

树的遍历是指访问树中所有节点的过程。常见的遍历方式有深度优先遍历(DFS)和广度优先遍历(BFS)。

深度优先遍历(DFS)

function dfs(node) {
  console.log(node.value);
  node.children.forEach(child => dfs(child));
}

广度优先遍历(BFS)

function bfs(root) {
  const queue = [root];
  while (queue.length > 0) {
    const node = queue.shift();
    console.log(node.value);
    node.children.forEach(child => queue.push(child));
  }
}

二叉树实现

二叉树是每个节点最多有两个子节点的树结构。

二叉树节点类

class BinaryTreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

二叉搜索树插入

class BinarySearchTree {
  constructor() {
    this.root = null;
  }

  insert(value) {
    const newNode = new BinaryTreeNode(value);
    if (this.root === null) {
      this.root = newNode;
    } else {
      this.insertNode(this.root, newNode);
    }
  }

  insertNode(node, newNode) {
    if (newNode.value < node.value) {
      if (node.left === null) {
        node.left = newNode;
      } else {
        this.insertNode(node.left, newNode);
      }
    } else {
      if (node.right === null) {
        node.right = newNode;
      } else {
        this.insertNode(node.right, newNode);
      }
    }
  }
}

树的常见操作

查找节点、删除节点、计算深度等是树的常见操作。

js树实现

查找节点

function findNode(root, value) {
  if (root === null) return null;
  if (root.value === value) return root;
  for (const child of root.children) {
    const found = findNode(child, value);
    if (found) return found;
  }
  return null;
}

计算树深度

function treeDepth(node) {
  if (node === null) return 0;
  let maxDepth = 0;
  node.children.forEach(child => {
    maxDepth = Math.max(maxDepth, treeDepth(child));
  });
  return maxDepth + 1;
}

标签: js
分享给朋友:

相关文章

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

js实现二叉树

js实现二叉树

二叉树的基本概念 二叉树是一种树形数据结构,每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的常见操作包括插入、删除、遍历等。 二叉树的节点定义 在JavaScript中,二叉树的节点可…

js实现防洪

js实现防洪

防抖(Debounce)实现 防抖的核心思想是在事件触发后延迟执行回调函数,若在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口调整等场景。 function debounce(f…

js实现视口

js实现视口

js实现视口检测的方法 使用JavaScript检测元素是否进入视口(viewport)可以通过Intersection Observer API或手动计算元素位置实现。以下是两种常见方法: Int…

js实现图片

js实现图片

图片加载与显示 在JavaScript中,可以通过Image对象动态加载图片。创建实例后设置src属性触发加载,通过onload回调处理加载完成后的操作: const img = new Ima…

js实现滑动

js实现滑动

实现滑动效果的方法 在JavaScript中实现滑动效果可以通过多种方式完成,以下是几种常见的实现方法: 使用CSS过渡和JavaScript触发 通过CSS定义过渡效果,JavaScript控制触…