新闻中心

J*aScript数据结构_链表与树形结构实现

2025-11-21
浏览次数:
返回列表
链表和树可通过对象与引用实现;链表用于高效插入删除,树适用于查找与层级结构,J*aScript中二者均需手动构建节点与操作方法。

javascript数据结构_链表与树形结构实现

链表和树是J*aScript中常见的数据结构,尤其在处理动态数据和层级关系时非常有用。虽然J*aScript没有内置的链表或树类型,但我们可以用对象和引用轻松实现它们。下面分别介绍链表和树形结构的基本实现方式。

链表的实现

链表由一系列节点组成,每个节点包含数据和指向下一个节点的指针。最常见的链表是单向链表。

定义节点:

每个节点是一个对象,包含两个属性:data(存储数据)和next(指向下一个节点)。

class ListNode {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

链表类的基本操作:

实现一个链表类,支持插入、删除、查找等操作。

class LinkedList {
  constructor() {
    this.head = null;
  }

  // 在链表末尾添加节点
  append(data) {
    const newNode = new ListNode(data);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  // 在指定位置插入节点
  insertAt(index, data) {
    if (index === 0) {
      const newNode = new ListNode(data);
      newNode.next = this.head;
      this.head = newNode;
      return;
    }
    let current = this.head;
    for (let i = 0; i < index - 1 && current; i++) {
      current = current.next;
    }
    if (!current) throw new Error('Index out of bounds');
    const newNode = new ListNode(data);
    newNode.next = current.next;
    current.next = newNode;
  }

  // 删除指定值的第一个节点
  remove(data) {
    if (!this.head) return;
    if (this.head.data === data) {
      this.head = this.head.next;
      return;
    }
    let current = this.head;
    while (current.next && current.next.data !== data) {
      current = current.next;
    }
    if (current.next) {
      current.next = current.next.next;
    }
  }

  // 查找是否包含某个值
  contains(data) {
    let current = this.head;
    while (current) {
      if (current.data === data) return true;
      current = current.next;
    }
    return false;
  }

  // 转为数组便于查看
  toArray() {
    const result = [];
    let current = this.head;
    while (current) {
      result.push(current.data);
      current = current.next;
    }
    return result;
  }
}

使用示例:

const list = new LinkedList();
list.append(1);
list.append(2);
list.append(3);
console.log(list.toArray()); // [1, 2, 3]
list.insertAt(1, 1.5);
console.log(list.toArray()); // [1, 1.5, 2, 3]
list.remove(1.5);
console.log(list.toArray()); // [1, 2, 3]

树形结构的实现

树是一种分层数据结构,最常见的是二叉树,每个节点最多有两个子节点:左子节点和右子节点。

E购-新零售系统 E购-新零售系统

“米烁云货宝”,是一款基于云计算的Saas模式新零售系统。以互联网为基础,通过大数据、人工智能等先进技术,对商品的生产、流通、销售、服务等环节转型升级改造,进而重塑业态结构与生态圈。并对线上交易运营服务、线下体验购买及现代物流进行深度融合,所形成的零售新模式。

E购-新零售系统 0 查看详情 E购-新零售系统

定义树节点:

class TreeNode {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

二叉搜索树(BST)实现:

二叉搜索树满足:左子树所有节点值小于根节点,右子树所有节点值大于根节点。

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

  // 插入节点
  insert(data) {
    const newNode = new TreeNode(data);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    this._insertNode(this.root, newNode);
  }

  _insertNode(node, newNode) {
    if (newNode.data < node.data) {
      if (!node.left) {
        node.left = newNode;
      } else {
        this._insertNode(node.left, newNode);
      }
    } else {
      if (!node.right) {
        node.right = newNode;
      } else {
        this._insertNode(node.right, newNode);
      }
    }
  }

  // 查找节点
  search(data) {
    return this._searchNode(this.root, data);
  }

  _searchNode(node, data) {
    if (!node) return null;
    if (data === node.data) return node;
    return data < node.data 
      ? this._searchNode(node.left, data) 
      : this._searchNode(node.right, data);
  }

  // 中序遍历(左-根-右),输出有序序列
  inorderTr*ersal(node = this.root, result = []) {
    if (node) {
      this.inorderTr*ersal(node.left, result);
      result.push(node.data);
      this.inorderTr*ersal(node.right, result);
    }
    return result;
  }

  // 先序遍历(根-左-右)
  preorderTr*ersal(node = this.root, result = []) {
    if (node) {
      result.push(node.data);
      this.preorderTr*ersal(node.left, result);
      this.preorderTr*ersal(node.right, result);
    }
    return result;
  }

  // 后序遍历(左-右-根)
  postorderTr*ersal(node = this.root, result = []) {
    if (node) {
      this.postorderTr*ersal(node.left, result);
      this.postorderTr*ersal(node.right, result);
      result.push(node.data);
    }
    return result;
  }
}

使用示例:

const bst = new BinarySearchTree();
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(3);
bst.insert(7);

console.log(bst.inorderTr*ersal());   // [3, 5, 7, 10, 15]
console.log(bst.preorderTr*ersal());  // [10, 5, 3, 7, 15]
console.log(bst.postorderTr*ersal()); // [3, 7, 5, 15, 10]

console.log(bst.search(7) !== null);   // true
console.log(bst.search(9) !== null);   // false

链表适合频繁插入删除的场景,而树适合快速查找、排序和层级表达。J*aScript通过对象引用来构建这些结构非常自然。理解它们的原理有助于写出更高效的代码。

基本上就这些。掌握基本实现后,可以进一步扩展功能,比如双向链表、平衡树、删除节点等复杂操作。

以上就是J*aScript数据结构_链表与树形结构实现的详细内容,更多请关注其它相关文章!


# 服务端  # 深圳营销推广网站模板  # 网站视频 推广  # 南平seo公司推荐30火星  # seo外包公司找哪家  # 山东新站seo优化推广  # 南京品牌推广招标网站  # sem中关键词规划网站推广  # 东莞网站建设开发与制作  # 网站推广产品赚钱吗  # 美容医疗视频推广营销  # 的是  # 点对点  # 数据结构  # 最常见  # 带来了  # 如何实现  # 遍历  # 子树  # 链表  # ai  # app  # node  # java  # javascript 


相关栏目: 【 科技资讯46185 】 【 网络学院92790


相关推荐: 2025年云电脑操作系统体验 | 无需本地硬件,随时随地使用高性能PC  漫蛙Manwa2官网入口地址分享 漫蛙漫画PC版永久访问通道  AO3镜像入口大全 AO3网页版内容访问全集  黑猫投诉统一入口官网 消费者权益保护投诉平台  蛙漫安全无毒 官方认证的绿色入口  《北京人工智能产业白皮书(2025)》发布:全年核心产值预计突破 4500 亿元  QQ邮箱官方邮箱登录入口 QQ邮箱网页版快速访问  Django通过AJAX异步上传图片并保存至模型的完整指南  抖音隐秘迷城小游戏入口_ 抖音冒险解谜小游戏秒玩  Pygame教程:解决用户输入与游戏状态更新不同步问题  正确连接J*aScript到HTML实现可点击图片与自定义事件处理  抖音网页版企业服务中心登录入口_抖音网页版企业登录平台  如何为你的Composer包编写自动化测试_集成PHPUnit到Composer的scripts工作流  C++如何打印当前代码行号与文件名_C++预定义宏FILE与LINE的使用  利用5118提升短视频内容效果_5118短视频关键词优化方法  Centos/Linux 系统下安装 composer 的完整步骤  俄罗斯浏览器官网直达链接 俄罗斯浏览器最新在线入口导航  excel怎么制作工资条 excel快速生成工资条的方法  Basecamp怎样用留言钉固定重点_Basecamp用留言钉固定重点【重点标记】  J*aScript异步迭代器_j*ascript异步遍历  Yandex免登录网页版地址 Yandex搜索引擎官方访问入口  C#中解析不规范的HTML为XML 常见的坑与解决办法  印象笔记如何设离线包出差查阅_印象笔记设离线包出差查阅【离线阅读】  QQ邮箱稳定登录入口_QQ邮箱官方网站网页版使用  2306选座时如何选靠窗位置_12306选座靠窗座位查看方法解析  德邦快递查询平台 德邦快递物流信息查询入口  不会效仿卡普空!《铁拳》制作人澄清:不采取赛事付费|直播|  “在文档元素之后找到了标记”是什么错误? 检查并修复XML中多个根元素的3个方法  c++ 命名空间怎么用 c++ namespace使用指南  离线运行Go语言之旅:本地部署与GOPATH配置指南  NRF24L01数据传输深度解析:解决大载荷接收异常与分包策略  React Hooks最佳实践:动态组件状态管理的组件化方案  支付宝如何管理隐私设置_支付宝隐私保护的配置技巧  如何在Promise链中优雅地中断后续then执行  处理嵌套交互式控件:前端可访问性指南  qq音乐在线播放入口_qq音乐电脑版登录链接  qq游戏网页版直接玩_qq游戏免下载快速入口  jQuery Mask 插件中实现电话号码固定前导零的教程  J*a递归快速排序中静态变量导致数据累积的陷阱与解决方案  Tabulator表格中精确实现日期时间排序的指南  电脑屏幕颜色不舒服怎么办_Windows夜间模式与色彩校准教程【护眼技巧】  最新韩小圈网页版登录入口_官网在线观看官方链接  谷歌浏览器最新官方入口链接 谷歌浏览器网页版官网导航  J*aScriptWebpack优化_J*aScript构建工具实战  优化 Jest 模拟:强制未实现函数抛出错误以提升测试效率  抖音小游戏合成大西瓜免费秒玩入口链接 抖音小游戏热门合集秒玩网站  QQ邮箱在线使用入口 QQ邮箱个人账号网页版登录  c++如何使用Meson构建系统_c++比CMake更快的构建工具  知乎APP怎么管理已购盐选内容_知乎APP盐选内容购买记录与查看方法  Angular Material 垂直步进器:实现底部到顶部排序的教程 

搜索