当前位置:首页 > JavaScript

js树实现

2026-01-14 14:24:59JavaScript

树的基本概念

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

树的实现方式

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

js树实现

使用对象字面量

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)。

js树实现

深度优先遍历(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);
      }
    }
  }
}

树的常见操作

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

查找节点

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 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.defin…

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 //…

js实现轮播

js实现轮播

实现基础轮播效果 使用HTML结构创建轮播容器和图片元素: <div class="carousel"> <div class="carousel-inner">…

js实现分页

js实现分页

实现分页的基本思路 分页功能通常需要处理数据分割、页码生成和用户交互。核心逻辑包括计算总页数、根据当前页截取数据、渲染页码按钮等。 前端分页实现(静态数据) 假设已有全部数据,仅需前端分页展示:…

vue实现js休眠

vue实现js休眠

Vue 中实现 JavaScript 休眠 在 Vue 中实现 JavaScript 休眠通常需要使用异步方式,以避免阻塞主线程。以下是几种常见方法: 使用 setTimeout 和 Pro…

js实现vue路由

js实现vue路由

Vue路由基础实现 使用Vue Router实现前端路由需要先安装并配置路由库。Vue Router是Vue.js官方的路由管理器。 安装Vue Router: npm install vue…