当前位置: 首页 > news >正文

构建二叉树进行数值数组的去重及优化

构建二叉树进行数值数组的去重及优化

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

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 < arr.length; i++) {
    let unique = true
    for (let j = 0; j < newArr.length; j++) {
        if (newArr[j] === arr[i]) {
            unique = false
            break
        }
    }
    if (unique) {
        newArr.push(arr[i])
    }
}
console.log(newArr)

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

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

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

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

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

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 < current.value) {
                if (current.left) {
                    current = current.left
                } else {
                    current.left = node
                    this.arr.push(value)
                    break
                }
            }
            if (value === current.value) {
                break
            }
        }
        return this.arr
    }
}

let binaryTree = new BinaryTree()
for (let i = 0; i < arr.length; i++) {
    binaryTree.insert(arr[i])
}
console.log(binaryTree.arr)

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

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

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 < this.min) {
            this.arr.push(value)
            this.min = value
            this.findMin().left = node
            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 < current.value) {
                if (current.left) {
                    current = current.left
                } else {
                    current.left = node
                    this.arr.push(value)
                    break
                }
            }
            if (value === current.value) {
                break
            }
        }
        return this.arr
    }

    findMax() {
        let current = this.root
        while (current.right) {
            current = current.right
        }
        return current
    }

    findMin() {
        let current = this.root
        while (current.left) {
            current = current.left
        }
        return current
    }
}

let binaryTree = new BinaryTree()
for (let i = 0; i < arr.length; i++) {
    binaryTree.insert(arr[i])
}
console.log(binaryTree.arr)

优化思路二,构建红黑树

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

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

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 < cur.value) {
                if (cur.left) {
                    cur = cur.left
                } else {
                    cur.left = node
                    this.arr.push(value)
                    node.parent = cur
                    inserted = true
                    break
                }
            }

            if (value === cur.value) {
                break
            }
        }
        // 调整树的结构
        if(inserted){
            this.fixTree(node)
        }
        return this
    }

    fixTree(node) {
        if (!node.parent) {
            node.color = 'black'
            this.root = node
            return
        }
        if (node.parent.color === 'black') {
            return
        }
        let son = node
        let father = node.parent
        let grandFather = father.parent
        let directionFtoG = father === grandFather.left ? 'left' : 'right'
        let uncle = grandFather[directionFtoG === 'left' ? 'right' : 'left']
        let directionStoF = son === father.left ? 'left' : 'right'
        if (!uncle || uncle.color === 'black') {
            if (directionFtoG === directionStoF) {
                if (grandFather.parent) {
                    grandFather.parent[grandFather.parent.left === grandFather ? 'left' : 'right'] = father
                    father.parent = grandFather.parent
                } else {
                    this.root = father
                    father.parent = null
                }
                father.color = 'black'
                grandFather.color = 'red'

                father[father.left === son ? 'right' : 'left'] && (father[father.left === son ? 'right' : 'left'].parent = grandFather)
                grandFather[grandFather.left === father ? 'left' : 'right'] = father[father.left === son ? 'right' : 'left']

                father[father.left === son ? 'right' : 'left'] = grandFather
                grandFather.parent = father
                return
            } else {
                grandFather[directionFtoG] = son
                son.parent = grandFather

                son[directionFtoG] && (son[directionFtoG].parent = father)
                father[directionStoF] = son[directionFtoG]

                father.parent = son
                son[directionFtoG] = father
                this.fixTree(father)
            }
        } else {
            father.color = 'black'
            uncle.color = 'black'
            grandFather.color = 'red'
            this.fixTree(grandFather)
        }
    }
}

let redBlackTree = new RedBlackTree()
for (let i = 0; i < arr.length; i++) {
    redBlackTree.insert(arr[i])
}
console.log(redBlackTree.arr)

其他去重方法

通过 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)))

相关文章:

  • MySQL 存储引擎
  • 组件化方案调研
  • Android ListView 长按列表弹出菜单
  • 此地址使用了一个通常用于网络浏览以外的端口。出于安全原因,Firefox 取消了该请求...
  • Android平台游戏开发引擎使用指引
  • android获得ImageView图片的等级
  • 自动作文评分
  • 运维生产环境常用脚本
  • 数据结构与算法 - 字符串
  • c语言——‘\0’ ,‘0’, “0” ,0之间的区别
  • Emgucv使用记录-------切忌点一
  • jQuery 效果 - animate() 方法
  • SFB 项目经验-22-如何查看存储的管理IP地址
  • 使用文件映射和信号量来进程间通信
  • python 类定义 继承
  • 《Java编程思想》读书笔记-对象导论
  • 【140天】尚学堂高淇Java300集视频精华笔记(86-87)
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • Django 博客开发教程 16 - 统计文章阅读量
  • HashMap剖析之内部结构
  • Java 最常见的 200+ 面试题:面试必备
  • session共享问题解决方案
  • Vue.js-Day01
  • vue学习系列(二)vue-cli
  • Web标准制定过程
  • WePY 在小程序性能调优上做出的探究
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 坑!为什么View.startAnimation不起作用?
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 前端面试总结(at, md)
  • 实战|智能家居行业移动应用性能分析
  • 数据科学 第 3 章 11 字符串处理
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • #{}和${}的区别?
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #图像处理
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (NSDate) 时间 (time )比较
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (Python第六天)文件处理
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (译)计算距离、方位和更多经纬度之间的点
  • .net php 通信,flash与asp/php/asp.net通信的方法
  • .net使用excel的cells对象没有value方法——学习.net的Excel工作表问题
  • @converter 只能用mysql吗_python-MySQLConverter对象没有mysql-connector属性’...
  • @WebServiceClient注解,wsdlLocation 可配置
  • [ MSF使用实例 ] 利用永恒之蓝(MS17-010)漏洞导致windows靶机蓝屏并获取靶机权限
  • [ solr入门 ] - 利用solrJ进行检索
  • []C/C++读取串口接收到的数据程序
  • [20171101]rman to destination.txt
  • [ACTF2020 新生赛]Upload 1
  • [bzoj4010][HNOI2015]菜肴制作_贪心_拓扑排序