Sorting according to the frequency of characters – heap sorting solution

Time:2020-2-22

Sorting according to the frequency of characters - heap sorting solution
Sorting according to the frequency of characters - heap sorting solution
Sorting according to the frequency of characters - heap sorting solution

class Heap {
  constructor (str) {
    let map = new Map()
    str.split('').forEach(item => {
      if (map.has(item)) {
        map.set(item, map.get(item) + 1)
      } else {
        map.set(item, 1)
      }
    })
    this.map = map
    this.data = Array.from(map.values())
  }
  sort () {
    let iArr = this.data
    let n = iArr.length
    if (n <= 1) {
      return iArr
    } else {
      //The loop is to traverse every possible node to be adjusted, and the internal recursion of maxheap is to recover the damaged heap
      for (let i = Math.floor(n / 2); i >= 0; i--) {
        Heap.maxHeapify(iArr, i, n)
      }
      for (let j = 0; j < n; j++) {
        Heap.swap(iArr, 0, n - 1 - j)
        Heap.maxHeapify(iArr, 0, n - 1 - j - 1)
      }
      return iArr
    }
  }
  toString () {
    let arr = this.sort()
    let str = []
    while (arr.length) {
      //Because of this, delete the traversed K
      let top = arr.pop()
      for (let [k, v] of this.map) {
        if (v === top) {
          str.push(k.repeat(v))
          this.map.delete(k)
          break
        }
      }
    }
    return str.join('')
  }
  static swap (arr, a, b) {
    if (a === b) {
      return ''
    }
    // exchange
    let c = arr[a]
    arr[a] = arr[b]
    arr[b] = c
  }
  //Build maximum heap
  static maxHeapify (Arr, i, size) {
    // left node
    let l = i * 2 + 1
    // right node
    let r = i * 2 + 2
    let largest = i
    //Compare the parent node and the left node l to get the maximum
    if (l <= size && Arr[l] > Arr[largest]) {
      largest = l
    }
    // right node额最大值比较
    if (r <= size && Arr[r] > Arr[largest]) {
      largest = r
    }
    if (largest !== i) {
      Heap.swap(Arr, i, largest)
      Heap.maxHeapify(Arr, largest, size)
    }
  }
}

export default Heap