数据结构与算法--JS篇

本文详细介绍了JavaScript中数据结构的实现,包括数组、栈、队列、优先级队列、链表、集合、字典(Map)、哈希表等,并深入探讨了哈希表的冲突解决策略,如链地址法和开放地址法。此外,还讲解了二叉树、平衡树(如AVL树和红黑树)以及图论的基础概念和操作。内容覆盖了数据结构的创建、插入、删除等操作,是JS开发者学习数据结构的好资料。
摘要由CSDN通过智能技术生成

数据结构

数组结构

  • 是一种线性结构
  • 可以在数组的任意位置插入和删除数组

栈结构

  • 一种受限的线性表,只允许在表的一端进行插入删除操作,这一端被称为栈顶,另一端称为栈底
  • 后进先出(LIFO Last In First Out)
  • 栈的封装

    1.基于数组
    function Stack() {
        this.items = []
    }
    Stack.prototype.push = function (element) {
        this.items.push(element)
    }
    Stack.prototype.pop = function () {
        return this.items.pop()
    }
    Stack.prototype.peek = function () {
        return this.items[this.items.length - 1]
    }
    Stack.prototype.isEmpty = function () {
        return this.items.length === 0
    }
    Stack.prototype.size = function () {
        return this.items.length
    }
    Stack.prototype.toString = function () {
        let str = ''
        for (let i = 0;i < this.items.length;i++){
            str += this.items[i]
        }
        return str
    }

队列结构

  • 一种受限的线性表,只允许在表的前端进行删除操作,在表的后端进行插入操作
  • 先进先出(FIFO First In First Out)
  • 队列的封装

    1.基于数组
    function Queue() {
        this.items = []
    }
    Queue.prototype.enqueue = function (element) {
        this.items.push(element)
    }
    Queue.prototype.dequeue = function () {
        return this.items.shift()
    }
    Queue.prototype.front = function () {
        return this.items[0]
    }
    Queue.prototype.isEmpty = function () {
        return this.items.length === 0
    }
    Queue.prototype.size = function () {
        return this.items.length
    }
    Queue.prototype.toString = function () {
        let str = ''
        for (let i = 0;i < this.items.length;i++){
            str += this.items[i]
        }
        return str
    }

面试题:几个朋友一起玩一个游戏,围成一个圈,开始数数,数到规定数字的人自动淘汰,最后剩下的这个人会获得胜利,请问最后剩下的是原来在哪一个位置上的人

    function passGame(nameList,num) { //nameList:玩游戏的人,num:规定的数字
        const queue = new Queue()
        //将nameList的每个人依次放入队列中
        for (let i = 0;i < nameList.length;i++){
            queue.enqueue(nameList[i])
        }
        //直到队列中的人数为1才停止
        while (queue.size() > 1){
            //开始数数,数到num之前的人都安全,依次从前端出来到队列的后端排队
            for (let i = 0;i < num - 1;i++){
                queue.enqueue(queue.dequeue())
            }
            //数到数字num,删除
            queue.dequeue()
        }
        const winner = queue.front()
        return nameList.indexOf(winner)
    }

优先级队列

在插入一个元素的时候会考虑该数据的优先级,与队列中其他数据的优先级进行比较,比较完成后,可以得出该数据在队列中的正确位置
封装优先级队列的插入方法,其它方法与普通队列一样

    function PriorityQueue() {
        this.items = []
        //内部再声明一个构造函数来表示队列元素的值和优先级
        function QueueElement(element,priority) {
            this.element = element
            this.priority = priority
        }
        //插入方法
        PriorityQueue.prototype.enqueue = function (element,priority) {
            //创建queueElement实例
            const queueElement = new QueueElement(element,priority)
            //如果是首次插入不需要比较优先级直接插入
            if (this.items.length === 0){
                this.items.push(queueElement)
            }else {
                //设置元素是否插入得标志
                let flag = false
                //插入元素跟队列中元素比较优先级
                for (let i = 0;i<this.items.length;i++){
                    if (queueElement.priority<this.items[i].priority){
                        this.items.splice(i,0,queueElement)
                        flag = true
                        break
                    }
                }
                //循环结束,如果元素还没找到正确位置,就放到末尾
                if (!flag){
                    this.items.push(queueElement)
                }
            }
        }
    }

链表


链表和数组一样,可以用于存储一系列的元素,链表中的每一个元素由一个存储元素本身的节点和一个指向下一个元素的引用组成。

链表和数组的区别

1.数组的创建需要申请一段连续的内存空间,并且大小是固定的,当当前数组容量不能满足需求时,需要进行扩容(申请一个更大的数组,然后将原数组中的元素复制过去);而链表中的元素在内存中不必是连续的空间,不用在创建时就确定大小,并且大小可以无限的延伸下去。
2.在数组的开头或中间位置插入删除元素的成本很高,因为需要进行大量元素的位移;链表在插入和删除数据时,时间复杂度可以到O(1),相对数组,效率高很多。
3.数组可以通过下标直接访问元素,而链表访问任何一个位置的元素时,都需要从头开始,一个一个访问。

封装单向链表

//封装单向链表
    function LinkedList(){
        this.head = null
        this.length = 0
        //元素节点构造函数
        function Node(data) {
            this.data = data
            this.next = null
        }
        //append方法:向链表的末尾插入元素
        LinkedList.prototype.append = function (data) {
            //1.创建一个元素节点实例
            const node = new Node(data)
            //2.判断是不是首次插入
            if (this.length === 0){
                //2.1首次插入,将head指针指向该元素节点
                this.head = node
            }else {
                //2.2不是首次插入,需要找到最后一个元素节点,将最后一个元素节点的指针指向该元素节点
                let current = this.head
                while (current.next){
                    current = current.next
                }
                //2.3找到最后一个元素节点,执行操作
                current.next = node
            }
            //3.插入元素后,更新length
            this.length++
        }
        //toString方法:将链表中的数据以字符串的方式返回
        LinkedList.prototype.toString = function () {
            let str = ''
            let current = this.head
            while (current){
                str += current.data + ' '
                current = current.next
            }
            return str
        }
        //insert方法:在指定位置插入数据
        LinkedList.prototype.insert = function (position,data) {
            //1.判断position是否越界
            if (position<0 || position>this.length){
                return false
            }
            //2.创建元素节点
            const node = new Node(data)
            //3.判断是不是插入到头部
            if (position === 0){
                //3.1插入到头部,将node元素的指针指向原来的head指针指向的元素节点,再把head指向node
                node.next = this.head
                this.head = node
            }else{
                //3.2其他情况,找到对应的位置,将node的指针指向当前位置的元素,前一个位置元素指针指向node
                let index = 0
                let current = this.head
                let previous = null
                while (index < position){
                    previous = current
                    current = current.next
                    index++
                }
                //3.3找到当前位置元素节点,将node指针指向它
                node.next = current
                //3.4找到前一个元素节点,将指针指向node
                previous.next = node
            }
            //4.length+1
            this.length++
        }
        //get方法:获取指定位置的元素
        LinkedList.prototype.get = function (position) {
            //1.position越界判断
            if (position<0 || position>=this.length) return null
            //2.取对应位置元素
            let index = 0;
            let current = this.head
            while (index++ < position){
                current = current.next
            }
            return current.data
        }
        //indexOf方法:返回指定元素所在链表中的位置,如果没有该元素则返回-1
        LinkedList.prototype.indexOf = function (data) {
            //1.循环链表,找到该元素
            let index = 0
            let current = this.head
            while (index < this.length && current.data !== data){
                current = current.next
                index++
            }
            return current?index:-1
        }
        //update方法:修改指定位置的元素
        LinkedList.prototype.update = function (position,newData) {
            //1.越界判断
            if (position<0 || position >= this.length) return false
            //2.查找元素节点,替换data
            let index = 0
            let current = this.head
            while (index++ < position){
                current = current.next
            }
            current.data = newData
        }
        //removeAt方法:移除链表中指定位置的元素,并返回该元素
        LinkedList.prototype.removeAt = function (position) {
            //1.越界判断
            if (position<0 || position >= this.length) return false
            let current = this.head
            //2.判断是否删除第一个元素
            if (position === 0){
                //2.1删除第一个元素,将head指向其下一个元素
                this.head = current.next
            }else {
                //2.2找到要删除的元素,将其前一个元素的指针指向其后一个元素
                let index = 0
                let previous = null
                while (index < position){
                    previous = current
                    current = current.next
                    index ++
                }
                previous.next = current.next
            }
            //3.长度减一
            this.length--
            return current.data
        }
        //remove方法:移除链表中指定的元素
        LinkedList.prototype.remove = function (data) {
            const position = this.indexOf(data)
            return this.removeAt(position)
        }
        //isEmpty方法
        LinkedList.prototype.isEmpty = function () {
            return this.length === 0
        }
        //size方法
        LinkedList.prototype.size = function () {
            return this.length
        }
    }

双向链表

一个元素节点既有指向其前一个元素节点的指针,也有指向其后一个元素节点的指针

封装双向链表

    //封装双向链表
    function DoublyLinkedList() {
        this.head = null
        this.tail = null
        this.length = 0
        function Node(data) {
            this.data = data
            this.prev = null
            this.next = null
        }
        //append方法:向链表的末尾插入元素
        DoublyLinkedList.prototype.append = function (data) {
            //1.创建元素节点
            const node = new Node(data)
            //2.判断是否是首次插入
            if (this.length === 0){
                this.head = node
                this.tail = node
            }else {
                node.prev = this.tail
                this.tail.next = node
                this.tail = node
            }
            //3.length+1
            this.length++
        }
        //toString方法:从前往后遍历转为字符串
        DoublyLinkedList.prototype.toString = function(){
            return this.backwardString()
        }
        //backwardString方法:从前往后遍历转为字符串
        DoublyLinkedList.prototype.backwardString = function () {
            let resultStr = ''
            let current = this.head
            while (current){
                resultStr += current.data + ' '
                current = current.next
            }
            return resultStr
        }
        //forwardString方法:从后往前遍历转为字符串
        DoublyLinkedList.prototype.forwardString = function () {
            let resultStr = ''
            let current = this.tail
            while (current){
                resultStr += current.data + ' '
                current = current.prev
            }
            return resultStr
        }
        //insert方法:在指定位置插入数据
        DoublyLinkedList.prototype.insert = function (position,data) {
            //1.position越界判断
            if (position < 0 || position > this.length) return false
            //2.创建元素节点
            const node = new Node(data)
            //3.判断是不是首次插入
            if (this.length === 0){
                //3.1首次插入,将head和tail指向该元素节点
                this.head = node
                this.tail = node
            }else{
                //3.2判断是否插入到头部
                if (position === 0){
                    //插入到头部,将插入元素next指向原头部元素,原头部元素prev指向插入元素,head指向插入元素
                    node.next = this.head
                    this.head.prev = node
                    this.head = node
                //3.3判断是否插入到尾部
                }else if (position === this.length){
                    //插入到尾部,将插入元素prev指向原尾部元素,原尾部元素next指向插入元素,tail指向插入元素
                    node.prev = this.tail
                    this.tail.next = node
                    this.tail = node
                }else {
                    //3.4其他情况,将当前位置元素的前一个元素的next指向插入元素,插入元素的prev指向前一个元素,
                    //插入元素的next指向当前位置元素,当前元素的prev指向插入元素
                    let index = 0
                    let current = this.head
                    while (index++ < position){
                        current = current.next
                    }
                    current.prev.next = node
                    node.prev = current.prev
                    node.next = current
                    current.prev = node
                }
            }
            //4.length+1
            this.length++
        }
        //get方法:获取指定位置的元素
        DoublyLinkedList.prototype.get = function (position) {
            //1.越界判断
            if (position < 0 || position >= this.length) return null
            let index = 0
            let current = this.head
            //如果position <= this.length / 2 从前往后遍历
            if (position <= this.length / 2){
                //2.找到指定位置元素并返回
                while (index++ < position){
                    current = current.next
                }
            }else {
                //如果position > this.length / 2 从后往前遍历
                index = this.length - 1
                current = this.tail
                while (index-- > position){
                    current = current.prev
                }
            }
            return current.data
        }
        //indexOf方法:返回指定元素所在链表中的位置,如果没有该元素则返回-1
        DoublyLinkedList.prototype.indexOf = function (data) {
            let index = 0
            let current = this.head
            while (index < this.length && current.data !== data){
                current = current.next
                index++
            }
            return current?index:-1
        }
        //update方法:修改指定位置的元素
        DoublyLinkedList.prototype.update = function (position,newData) {
            //1.越界判断
            if (position < 0 || position >= this.length) return false
            //2.找到指定位置元素并修改
            let index = 0
            let current = this.head
            while (index++ < position){
                current = current.next
            }
            current.data = newData
        }
        //removeAt方法:移除链表中指定位置的元素,并返回该元素
        DoublyLinkedList.prototype.removeAt = function (position) {
            //1.越界判断
            if (position < 0 || position >= this.length) return null
            let current = this.head
            // 2.判断是否只有一个元素
            if (this.length === 1) {
                this.head = null
                this.tail = null
                //3.判断是否删除头部元素
            }else if (position === 0){
                //将head指向后一个元素,后一个元素的prev指向null
                current.next.prev = null
                this.head = current.next
            //4.判断是否删除尾部元素
            }else if (position === this.length - 1){
                //将tail指向前一个元素,前一个元素的next指向null
                current = this.tail
                current.prev.next = null
                this.tail = current.prev
            }else { //5.其他情况
                //5.1.找到该位置元素
                let index = 0
                while (index++ < position){
                    current = current.next
                }
                //5.2.将该位置元素的前一个元素的next指向该位置元素的后一个元素,后一个元素的prev指向前一个元素
                current.prev.next = current.next
                current.next.prev = current.prev
            }
            //6.length-1
            this.length--
            //7.返回该元素
            return current.data
        }
        //remove方法:移除链表中指定的元素
        DoublyLinkedList.prototype.remove = function (data) {
            const position = this.indexOf(data)
            return this.removeAt(position)
        }
        //isEmpty方法
        DoublyLinkedList.prototype.isEmpty = function () {
            return this.length === 0
        }
        //size方法
        DoublyLinkedList.prototype.size = function () {
            return this.length
        }
        //getHead方法:获取链表的第一个元素
        DoublyLinkedList.prototype.getHead = function () {
            return this.head.data
        }
        //getTail方法:获取链表的最后一个元素
        DoublyLinkedList.prototype.getTail = function () {
            return this.tail.data
        }
    }

集合

集合通常由一组无序的,不能重复的元素组成
(ES6中已经包含了Set,可以直接使用)
封装集合

    //封装集合
    function Set(){
        this.items = {}
        //add方法:向集合中添加新的元素
        Set.prototype.add = function (value) {
            if (this.has(value)) return false
            this.items[value] = value
            return true
        }
        //has方法:如果集合中有该元素,返回true,否则返回false
        Set.prototype.has = function (value) {
            return this.items.hasOwnProperty(value)
        }
        //remove方法:从集合中删除一个元素
        Set.prototype.remove = function (value) {
            if (!this.has(value)) return false
            delete this.items[value]
            return true
        }
        //clear方法:移除集合中的所有元素
        Set.prototype.clear = function () {
            this.items = {}
        }
        //size方法:返回集合中所有元素的个数
        Set.prototype.size = function () {
            return Object.keys(this.items).length
        }
        //values方法:返回包含所有元素的数组
        Set.prototype.values = function () {
            return Object.values(this.items)
        }
        //集合间的操作
        //并集
        Set.prototype.union = function (anotherSet) {
            //1.创建一个新的集合
            const unionSet = new Set()
            //2.将当前集合中的所有数据添加到unionSet中
            let values = this.values()
            for (let i = 0;i < values.length;i++){
                unionSet.add(values[i])
            }
            //3.将anotherSet中不重复的元素添加到unionSet中
            values = anotherSet.values()
            for (let i = 0;i < values.length;i++){
                unionSet.add(values[i])
            }
            //4.返回unionSet
            return unionSet
        }
        //交集
        Set.prototype.intersection = function (anotherSet) {
            //1.创建交集
            const intersection = new Set()
            //2.遍历集合中的元素是否同时存在于anotherSet中,如果存在,则添加
            const values = this.values()
            for (let i = 0;i < values.length;i++){
                if (anotherSet.has(values[i])){
                    intersection.add(values[i])
                }
            }
            //3.返回交集
            return intersection
        }
        //差集 A-B
        Set.prototype.subtraction = function (anotherSet) {
            //1.创建差集
            const subtraction = new Set()
            //2.遍历A中元素,如果不存在于B中,则添加到新集合
            const values = this.values()
            for (let i = 0;i < values.length;i++){
                if (!anotherSet.has(values[i])){
                    subtraction.add(values[i])
                }
            }
            //3.返回交集
            return subtraction
        }
        //判断集合A是否是集合B的子集
        Set.prototype.isSubSet = function (anotherSet) {
            //循环A集合中所有元素,只要有一个不再集合B中就返回false,否则返回true
            const values = this.values()
            for (let i = 0;i < values.length;i++){
                if (!anotherSet.has(values[i])) return false
            }
            return true
        }
    }

字典(Map)

字典是由一一对应的键(key)值(value)对组成,key是不允许重复的,value允许重复,并且key是无序的

哈希表

通过哈希函数将字符串转为下标值,通过下标值查找操作数据的效率就会变得很高
哈希表通常是基于数组进行实现的,但是相对数组又有很多优势

  • 它可以提供非常快速的插入-删除-查找操作
  • 哈希表的速度比树还要快,基本可以瞬间查找到想要的元素
  • 哈希表相对于树来说编码要容易很多
    哈希表相对于数组的一些不足
  • 哈希表中的数据是没有顺序的,所以不能以一种固定的方式(比如从小到大)来遍历其中的元素
  • 通常情况下,哈希表中的key是不允许重复的
    哈希化:将大数字转化成数组范围内下标
    哈希函数:将字符串转成大数字,大数字再进行哈希化的代码实现
    哈希表:最终将数据插入到的这个数组,对整个结构的封装
    经过哈希化后的下标值可能会发生重复,这种情况称为冲突
    解决冲突的方法:

链地址法(拉链法)

每个数组单元中存储的不再是单个数据,而是一个数组或链表,一旦发现重复,将重复的元素插入到数组或链表的首端或者末端。当查询时,先根据哈希化后的下标值找到相应的位置,再取出该位置的链表,依次查询需要查找的数据。

开放地址法

寻找空白的位置来放置冲突的数据项
寻找空白位置的三种方法

  • 线性探测
    线性的查找空白单元
    插入:
    在插入时,如果发现该位置已有数据,就从index+1开始依次向后查找空的位置来存放该数据
    查询:
    查询经过哈希化得到的index位置的数据是否是要找的数据,如果是就返回,如果不是,从index+1开始依次向后查找,当查询到空位置时就停止,说明没有该数据
    删除:
    跟插入查询比较类似,但是删除一个数据项时,不能把该位置元素内容设为null(否则,查询操作时可能会出现未查找到该元素就停止的错误)可以将它进行特殊处理(比如说设为-1)当我们看到-1时就知道查询操作要继续,可以在该位置插入数据
    线性探测的问题:聚集
    聚集就是连续填充单元,会影响哈希表的性能,因为在插入重复数据的时候,会发现连续的单元都不能用来存放,需要探测很长的距离。
  • 二次探测
    对线性探测的优化,主要优化的是探测的步长,比如从下标值x开始,x+12,x+22…,这样就可以一次性探测比较长的距离,避免了聚集带来的影响
    二次探测的问题:
    比如我们连续插入的是22-132-82-92-112,他们依次累加的时候步长是相同的,会造成步长不一的一种聚集,还是会影响效率(但这种可能性比连续的数字小)
  • 再哈希法
    为了解决线性探测和二次探测带来的问题
    把大数字用另外一个哈希函数再做一次哈希化,用这次哈希化的结果作为步长
    第二次哈希化需要具备如下特点:
    1.和第一个哈希函数不同
    2.不能输出为0,否则每次探测都是原地踏步,算法进入死循环
    计算机专家已经设计出一种工作很好的哈希函数

链地址法封装哈希表

    function HashTable() {
        this.storage = [] //存放数据
        this.count = 0  //数据个数
        this.limit = 7 //哈希表容量:最好为质数,这样数据能更均匀分布
        //哈希函数
        HashTable.prototype.hashFunc = function (str) {
            //1.定义hashCode
            let hashCode = 0
            //2.霍拉算法,将字符串转为较大的数字hashCode
            //str.charCodeAt(i):将字符转为unicode编码
            for (let i = 0;i < str.length;i++){
                hashCode = 37 * hashCode + str.charCodeAt(i)
            }
            //3.将hashCode转为数组范围内下标值返回
            return hashCode % this.limit
        }
        //put方法:插入&修改数据
        HashTable.prototype.put = function (key,value) {
            //1.根据key获取索引值
            const index = this.hashFunc(key)
            //2.判断该位置是否为空,如果是需要在该位置创建数组容器
            let bucket = this.storage[index]
            if (bucket === null){
                bucket = []
                this.storage[index] = bucket
            }
            //3.判断是插入操作还是修改操作
            for (let i = 0;i < bucket.length;i++){
                if (bucket[i][0] === key){
                    //3.1修改操作
                    bucket[i][1] = value
                    return
                }
            }
            //3.2插入操作
            //3.2.1创建一个数组存放插入数据
            const tuple = [key,value]
            //3.2.2将数据存放到单元数组容器中
            bucket.push(tuple)
            //3.2.3 数据个数加一
            this.count++
            //判断是否需要扩容,当loadFactor>0.75时就需要扩容
            if (this.count > this.limit * 0.75){
                const newSize = this.limit * 2
                this.resize(this.getPrime(newSize))
            }
        }
        //get方法:获取数据
        HashTable.prototype.get = function (key) {
            //1.根据key获取索引值
            const index = this.hashFunc(key)
            //2.判断当前索引位置是否有值,如果没有返回null,如果有遍历查找
            const bucket = this.storage[index]
            if (bucket === null){
                return null
            }
            for (let i = 0;i < bucket.length;i++){
                const tuple = bucket[i]
                if (tuple[0] === key){
                    return tuple[1]
                }
            }
            //3.如果遍历完数组容器还没找到,就返回null
            return null
        }
        //remove方法:删除数据
        HashTable.prototype.remove = function (key) {
            //1.根据key获取对应的下标值
            const index = this.hashFunc(key)
            //2.根据index获取该位置的数组容器
            const bucket = this.storage[index]
            //3.如果bucket不存在,返回null
            if (bucket === null) return null
            //4.如果存在,线性查找并删除
            for (let i = 0;i < bucket.length;i++){
                const tuple = bucket[i]
                if (tuple[0] === key){
                    bucket.splice(i,1)
                    this.count--
                    //判断是否需要缩容,当loadFactor < 0.25时就需要缩容
                    if (this.count < this.limit * 0.25){
                        const newSize = Math.floor(this.limit / 2)
                        this.resize(this.getPrime(newSize))
                    }
                    return tuple[1]
                }
            }
            //5.如果还是没有找到,就返回null
            return null
        }
        //其他方法
        //判断哈希表是否为空
        HashTable.prototype.isEmpty = function () {
            return this.count === 0
        }
        //获取哈希表中元素的个数
        HashTable.prototype.size = function () {
            return this.count
        }
        //哈希表修改容量的方法
        HashTable.prototype.resize = function (newLimit) {
            //1.定义一个变量指向原哈希表数据
            const oldStorage = this.storage
            //2.重置当前哈希表属性
            this.storage = []
            this.count = 0
            this.limit = newLimit
            //3.遍历原哈希表中的数据,存放到新的哈希表中
            for (let i = 0;i < oldStorage.length;i++){
                const bucket = oldStorage[i]
                //如果bucket为空,跳出本次循环
                if (bucket === null) continue
                //如果存在bucket,遍历bucket中的数据
                for (let j = 0;j < bucket.length;j++){
                    const tuple = bucket[i]
                    this.put(tuple[0],tuple[1])
                }
            }
        }
        //判断一个数是否是质数
        HashTable.prototype.isPrime = function (num) {
            /**质数:在大于1的自然数中,只能被1和自己整除的数
             * 一个数如果能被因式分解,那么分解得到的两个数,一定是一个大于等于这个数开平方,一个小于等于这个数开平方
             * 所以我们判断一个数是否是质数时,只需要判断从2到这个数的平方根是否能整除这个数就行了
             * */
            const sqrt = Math.floor(Math.sqrt(num))
            for (let i = 2;i <= sqrt;i++){
                if (num % i === 0){
                    return false
                }
            }
            return true
        }
        //获取质数的方法
        HashTable.prototype.getPrime = function (num) {
            while (!this.isPrime(num)){
                num++
            }
            return num
        }
    }

树是非线性的,可以表示一对多的关系
树的常用术语

二叉树

  • 完美二叉树
  • 完全二叉树
  • 二叉搜索树

    封装二叉搜索树
    //封装二叉搜索树
    function BinarySearchTree() {
        //定义指向根节点的索引
        this.root = null
        //节点构造函数
        function Node(key,value) {
            this.key = key
            this.value = value
            this.left = null
            this.right = null
        }
        //insert方法:插入数据
        BinarySearchTree.prototype.insert = function (key,value) {
            //1.创建新节点
            const newNode = new Node(key,value)
            //2.判断是否存在根节点
            if (this.root === null){
                this.root = newNode
            }else {
                this.insertNode(this.root,newNode)
            }
        }
        //内部使用的比较节点并插入的递归方法
        BinarySearchTree.prototype.insertNode = function (node,newNode) {
            if (newNode.key < node.key){ //向左查找
                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)
                }
            }
        }
        //树的遍历方法
        //1.先序遍历
        BinarySearchTree.prototype.preOrderTraversal = function (handler) {
            this.preOrderTraversalNode(this.root,handler)
        }
        //内部调用的先序遍历的递归方法
        BinarySearchTree.prototype.preOrderTraversalNode = function (node,handler) {
            //判断节点是否存在
            if (node !== null){
                //处理节点
                handler(node.value)
                //遍历左子节点
                this.preOrderTraversalNode(node.left,handler)
                //遍历右子节点
                this.preOrderTraversalNode(node.right,handler)
            }
        }
        //2.中序遍历
        BinarySearchTree.prototype.midOrderTraversal = function (handler) {
            this.midOrderTraversalNode(this.root,handler)
        }
        //中序遍历的递归方法
        BinarySearchTree.prototype.midOrderTraversalNode = function (node,handler) {
            //1.判断该节点是否存在
            if (node !== null){
                //中序遍历左子树
                this.midOrderTraversalNode(node.left,handler)
                //处理该节点
                handler(node.value)
                //中序遍历右子树
                this.midOrderTraversalNode(node.right,handler)
            }
        }
        //3.后序遍历
        BinarySearchTree.prototype.postOrderTraversal = function (handler) {
            this.postOrderTraversalNode(this.root,handler)
        }
        //后序遍历的递归方法
        BinarySearchTree.prototype.postOrderTraversalNode = function (node,handler) {
            if (node !== null){
                //后序遍历左子树
                this.postOrderTraversalNode(node.left,handler)
                //后序遍历右子树
                this.postOrderTraversalNode(node.right,handler)
                //处理节点
                handler(node.value)
            }
        }
        //min方法:查找最小值
        BinarySearchTree.prototype.min = function() {
            let node = this.root
            while(node.left !== null){
                node = node.left
            }
            return node.value
        }
        //max方法:查找最大值
        BinarySearchTree.prototype.max = function() {
            let node = this.root
            while(node.right !== null){
                node = node.right
            }
            return node.value
        }
        //search方法:查找元素节点,如果存在返回value,如果不存在返回false
        BinarySearchTree.prototype.search = function (key) {
            let node = this.root
            while (node && node.key !== key){
                if (node.key < key){
                    node = node.right
                }else {
                    node = node.left
                }
            }
            return node?node.value:false
        }
        //remove方法:删除元素节点
        BinarySearchTree.prototype.remove = function (key) {
            //1.通过key查找到该节点
            let current = this.root
            let parentNode = null //定义父节点
            let isLeftChild = true //定义该节点是否是左子节点
            while (current){
                if (current.key < key){
                    parentNode = current
                    current = current.right
                    isLeftChild = false
                }else if (current.key > key){
                    parentNode = current
                    current = current.left
                    isLeftChild = true
                }else {
                    //2.找到该节点
                    //2.1如果该节点是叶子节点,直接删除,将其父节点指向null
                    if (!current.left && !current.right){
                        //2.1.1判断是否删除的是根节点
                        if (!parentNode) this.root = null
                        else {
                            //判断该节点是否是左子节点
                            if (isLeftChild) parentNode.left = null
                            else parentNode.right = null
                        }
                        return current.value
                    }
                    //2.2如果该节点有两个子节点
                    else if (current.left && current.right){
                        /**
                         * 如果删除的节点有两个子节点,可以找其前驱节点或者后继节点来替换该节点的位置,这里我找的后继节点
                         * 封装一个找后继节点的内部调用方法successor
                         */
                        //2.2.1找到该节点的后继节点
                        let successor = this.successor(current)
                        //2.2.2用后继节点替换当前节点
                        successor.left = current.left
                        successor.right = current.right
                        //2.2.3判断是否删除根节点
                        if (!parentNode) this.root = successor
                        else {
                            //2.2.4不是根节点
                            if (isLeftChild) parentNode.left = successor
                            else parentNode.right = successor
                        }
                        return current.value
                    }
                    //2.3如果该节点只有一个子节点,将其父节点直接指向其子节点
                    else {
                        //2.3.1判断该节点是否是根节点
                        if (!parentNode){
                            if (current.left) this.root = current.left
                            else this.root = current.right
                        }else { //不是根节点的情况
                            if (isLeftChild){
                                //如果删除的是是左子节点,将其父节点的左子节点指向其子节点
                                if (current.left) parentNode.left = current.left
                                else parentNode.left = current.right
                            }else {
                                //如果删除的是右子节点,将其父节点的右子节点指向其子节点
                                if (current.left) parentNode.right = current.left
                                else parentNode.right = current.right
                            }
                        }
                        return current.value
                    }
                }
            }
            //3.没有找到,返回null
            return null
        }
        //successor方法:供内部调用的查找后继节点的方法
        BinarySearchTree.prototype.successor = function (node) {
            let successor = node.right
            let parentNode = node
            while (successor.left){
                parentNode = successor
                successor = successor.left
            }
            //判断successor是否直接是该节点的右子节点
            if (successor === node.right){
                parentNode.right = null
            }else {
                //如果后继节点有右子树,将后继节点的父节点指向其右子树
                if (successor.right) {
                    parentNode.left = successor.right
                }else {
                    parentNode.left = null
                }
            }
            return successor
        }
    }

树的遍历方式:

  • 先序遍历

  • 中序遍历

  • 后续遍历

    二叉搜索树的缺陷
    当插入的数据是连续的时候,树的深度会变得很深,插入删除数据项时效率会变得很低

平衡树(二叉搜索树的优化)

  • AVL树
    删除/删除操作的效率相对于红黑树较低,所以整体效率低于红黑树

  • 红黑树
    现在平衡树的应用基本都是红黑树
    红黑树除了符合二叉搜索树外的基本规则外,还有一些自己的特性:
    1.节点是红色或黑色
    2.根节点是黑色
    3.每个叶子节点都是黑色的空节点(NIL节点)
    4.每个红色节点的子节点都是黑色的(从每个叶子到根的所有路径上不可能出现两个连续的红色节点)
    5.从任一节点到其每个叶子节点的所有路径都包含相同数目的黑色节点
    前面的约束,确保了红黑树的关键特性:从根到叶子的最长路径不会超过最短路径的两倍
    插入一个新节点时,有可能树不再平衡,可以通过三种方式的变换,使树保持平衡

  • 变色
    新插入的节点默认都是红色的

  • 左旋转
    逆时针旋转红黑树的两个节点,使得父节点被自己的右孩子取代,而自己成为自己的左孩子

  • 右旋转
    顺时针旋转红黑树的两个节点,使得父节点被自己的左孩子取代,而自己成为自己的右孩子

插入操作

设要插入的节点为N,其父节点为P,其祖父节点为G,其父节点的兄弟节点为U
1.情况一:
新节点N位于树的根上,没有父节点
操作:直接将红色变换为黑色即可
2.情况二:插入节点的父节点P是黑色的
操作:不需要变换
3.情况三:P为红色,U也为红色
操作:将P和U变换为黑色,并且将G变为红色

4.情况四:P为红色,U为黑色,且N是作为左孩子
操作:P变为黑色,G变为红色,G、P进行右旋转

5.情况五:P为红色,U为黑色,且N是作为右孩子
操作:
1.P、N进行左旋转(旋转后将P作为新插入的红色节点考虑即可,继续情况四操作)
2.N变为黑色,G变为红色,G、N进行右旋转

图论

图结构是一种与树结构有些相似的数据结构
图论是数学的一个分支,在数学的概念上,树是图的一种
它以图为研究对象,研究顶点和边组成的图形的数学理论和方法
主要研究的目的是事物之间的关系,顶点代表事物,边代表两个事物间的关系

图的常用术语

  • 顶点:表示图中的一个节点
  • 边:表示顶点和顶点之间的连线
  • 相邻顶点:由一条边连接起来的顶点称为相邻顶点
  • 度:一个顶点的度是其相邻顶点的数量
  • 路径:一串连续的顶点序列
  • 简单路径:不包含重复的顶点
  • 回路:第一个顶点和最后一个顶点相同的路径称为回路
  • 无向图:所有的边都没有方向的图称为无向图
  • 有向图:图中的边是有方向的
  • 无权图:边没有携带权重
  • 带权图:边有一定的权重

图的表示

顶点可以抽象成ABCD…(或1234…),可以使用一个数组存储所有的顶点
边的表示:

  • 邻接矩阵
    邻接矩阵让每个顶点和一个整数相关联,该整数作为数组的下标值
    用一个二维数组来表示顶点的连接

    邻接矩阵的问题:
    邻接矩阵有一个比较严重的问题,就是如果图是一个稀疏图,那么矩阵中将存在大量的0,这意味着我们浪费了计算机的存储空间来表示根本不存在的边。
  • 邻接表
    邻接表由图中每个顶点以及和顶点相邻的顶点列表组成
    这个列表有很多种方式来存储:数组、链表、字典、哈希表等都可以

    邻接表的问题:
    邻接表计算出度比较简单,但如果需要计算有向图的入度,是一件非常麻烦的事情。它必须构造一个“逆邻接表”,才能有效的计算入度,但是在开发中入度用的比较少。
    (出度:指向别人的数量;入度:指向自己的数量)

封装图结构

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值