js二叉树进行数值数组的去重与优化

本文主要给大家介绍了关于js构建二叉树进行数值数组的去重与优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

常见两层循环实现数组去重

let arr = [11, 12, 13, 9, 8, 7, 0, 1, 2, 2, 5, 7, 11, 11, 7, 6, 4, 5, 2, 2]let newArr = []for (let i = 0; i 

构建二叉树实现去重(仅适用于数值类型的数组)

将先前遍历过的元素,构建成二叉树,树中每个结点都满足:左子结点的值

这样优化了判断元素是否之前出现过的过程

若元素比当前结点大,只需要判断元素是否在结点的右子树中出现过即可

若元素比当前结点小,只需要判断元素是否在结点的左子树中出现过即可


let arr = [0, 1, 2, 2, 5, 7, 11, 7, 6, 4,5, 2, 2]class Node { constructor(value) {  this.value = value  this.left = null  this.right = null }}class BinaryTree { constructor() {  this.root = null  this.arr = [] } insert(value) {  let node = new Node(value)  if (!this.root) {   this.root = node   this.arr.push(value)   return this.arr  }  let current = this.root  while (true) {   if (value > current.value) {    if (current.right) {     current = current.right    } else {     current.right = node     this.arr.push(value)     break    }   }   if (value 

优化思路一,记录最大最小值

记录已经插入元素的最大最小值,若比最大元素大,或最小元素小,则直接插入


let arr = [11, 12, 13, 9, 8, 7, 0, 1, 2, 2, 5, 7, 11, 11, 7, 6, 4, 5, 2, 2]class Node { constructor(value) {  this.value = value  this.left = null  this.right = null }}class BinaryTree { constructor() {  this.root = null  this.arr = []  this.max = null  this.min = null } insert(value) {  let node = new Node(value)  if (!this.root) {   this.root = node   this.arr.push(value)   this.max = value   this.min = value   return this.arr  }  if (value > this.max) {   this.arr.push(value)   this.max = value   this.findMax().right = node   return this.arr  }  if (value  current.value) {    if (current.right) {     current = current.right    } else {     current.right = node     this.arr.push(value)     break    }   }   if (value 

优化思路二,构建红黑树

构建红黑树,平衡树的高度

有关红黑树的部分,请见红黑树的插入


let arr = [11, 12, 13, 9, 8, 7, 0, 1, 2, 2, 5, 7, 11, 11, 7, 6, 4, 5, 2, 2]console.log(Array.from(new Set(arr)))class Node { constructor(value) {  this.value = value  this.left = null  this.right = null  this.parent = null  this.color = 'red' }}class RedBlackTree { constructor() {  this.root = null  this.arr = [] } insert(value) {  let node = new Node(value)  if (!this.root) {   node.color = 'black'   this.root = node   this.arr.push(value)   return this  }  let cur = this.root  let inserted = false  while (true) {   if (value > cur.value) {    if (cur.right) {     cur = cur.right    } else {     cur.right = node     this.arr.push(value)     node.parent = cur     inserted = true     break    }   }   if (value 

其他去重方法

通过 Set 对象去重


[...new Set(arr)]

登录后复制

通过 sort() + reduce() 方法去重

排序后比较相邻元素是否相同,若不同则添加至返回的数组中

值得注意的是,排序的时候,默认 compare(2, '2') 返回 0;而 reduce() 时,进行全等比较

let arr = [0, 1, 2, '2', 2, 5, 7, 11, 7, 5, 2, '2', 2]let newArr = []arr.sort((a, b) => { let res = a - b if (res !== 0) {  return res } else {  if (a === b) {   return 0  } else {   if (typeof a === 'number') {    return -1   } else {    return 1   }  } }}).reduce((pre, cur) => { if (pre !== cur) {  newArr.push(cur)  return cur } return pre}, null)

登录后复制

通过 includes() + map() 方法去重

let arr = [0, 1, 2, '2', 2, 5, 7, 11, 7, 5, 2, '2', 2]let newArr = []arr.map(a => !newArr.includes(a) && newArr.push(a))

登录后复制

通过 includes() + reduce() 方法去重

let arr = [0, 1, 2, '2', 2, 5, 7, 11, 7, 5, 2, '2', 2]let newArr = arr.reduce((pre, cur) => {  !pre.includes(cur) && pre.push(cur)  return pre}, [])

登录后复制

通过对象的键值对 + JSON 对象方法去重

let arr = [0, 1, 2, '2', 2, 5, 7, 11, 7, 5, 2, '2', 2]let obj = {}arr.map(a => {  if(!obj[JSON.stringify(a)]){    obj[JSON.stringify(a)] = 1  }})console.log(Object.keys(obj).map(a => JSON.parse(a)))

登录后复制

相关推荐:

数组 PHP二维数组的去重问题解析

JS数组去重方法总结

JavaScript数组去重的几种方法分享

以上就是js二叉树进行数值数组的去重与优化的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2779020.html

(0)
上一篇 2025年3月8日 15:41:53
下一篇 2025年2月25日 00:29:46

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 红黑树的插入及Javascript实现方法详解

    本文主要和大家分享红黑树的插入及javascript实现方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,希望能帮助到大家。 红黑树的性质 一棵满足以下性质的二叉搜索树是一棵红黑树 每个结点或是黑色或是红…

    2025年3月8日 编程技术
    200
  • js和canvas实现滑动拼图验证码功能

    本文主要和大家分享js和canvas实现滑动拼图验证码功能,现在经常会遇到滑动拼图验证,希望能帮助到大家。   上图为网易云盾的滑动拼图验证码,其应该有一个专门的图片库,裁剪的位置是固定的。我的想法是,随机生成图片,随机生成位置,再用can…

    2025年3月8日 编程技术
    200
  • JS实现从非数组对象转数组的方法

    本文主要和大家介绍了关于js从非数组对象转数组的一些方法,分别是array.prototype.slice.call(obj)、array.from(obj)、[…obj]和object.values(obj)等方法的详细实现方法,需要的朋…

    编程技术 2025年3月8日
    200
  • 详解JS的解析顺序及作用域

    本文主要为大家分享一篇详解JS的解析顺序和作用域,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。 一、javascript的解析顺序 我们大家所理解的代码的执行顺序都是从上到下的,但是实际上确不是这样的。我们看一下下面的代…

    编程技术 2025年3月8日
    200
  • jQuery+JSONP跨域请求如何实现

    这次给大家带来jQuery+JSONP跨域请求如何实现,实现jQuery+JSONP跨域请求的注意事项有哪些,下面就是实战案例,一起来看一下。   JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流…

    2025年3月8日
    200
  • JS的深浅拷贝使用详解

    这次给大家带来JS的深浅拷贝使用详解,使用JS深浅拷贝的注意事项有哪些,下面就是实战案例,一起来看一下。 前言 说到深浅拷贝,必须先提到的是JavaScript的数据类型,之前的一篇文章JavaScript基础心法——数据类型说的很清楚了,…

    编程技术 2025年3月8日
    200
  • JS的数据类型使用详解

    这次给大家带来JS的数据类型使用详解,使用JS的数据类型的注意事项有哪些,下面就是实战案例,一起来看一下。 由于自己是野生程序员,在刚开始学习程序设计的时候没有在意内存这些基础知识,导致后来在提到“什么什么是存在栈中的,栈中只是存了一个引用…

    2025年3月8日
    200
  • js如何直接获取网页中图片地址

    这次给大家带来js如何直接获取网页中图片地址,js直接获取网页中图片地址的注意事项有哪些,下面就是实战案例,一起来看一下。 第一种方法:js通过正则实现 /** * 获取html代码中图片地址 * @param htmlstr * @ret…

    编程技术 2025年3月8日
    200
  • node.js怎样通过axios实现网络请求

    这次给大家带来node.js怎样通过axios实现网络请求,node.js通过axios实现网络请求的注意事项有哪些,下面就是实战案例,一起来看一下。 1、使用Npm 下载axios npm install –save axio…

    编程技术 2025年3月8日
    200
  • AngularJS中@HostBinding()和@HostListener()有什么区别

    这次给大家带来AngularJS中@HostBinding()和@HostListener()有什么区别,AngularJS中使用@HostBinding()和@HostListener()的注意事项有哪些,下面就是实战案例,一起来看一下。…

    2025年3月8日
    200

发表回复

登录后才能评论