Heim  >  Artikel  >  Web-Frontend  >  Detaillierte Erläuterung der Deduplizierung und Optimierung numerischer Arrays mithilfe von js zum Aufbau eines Binärbaums

Detaillierte Erläuterung der Deduplizierung und Optimierung numerischer Arrays mithilfe von js zum Aufbau eines Binärbaums

亚连
亚连Original
2018-05-28 17:33:161550Durchsuche

Dieser Artikel stellt Ihnen hauptsächlich die relevanten Informationen zur Deduplizierung und Optimierung numerischer Arrays zum Erstellen von Binärbäumen vor. Der Artikel stellt sie im Detail anhand von Beispielcodes vor benötigt Freunde, lasst uns gemeinsam lernen.

Vorwort

In diesem Artikel werden hauptsächlich die relevanten Inhalte zum Erstellen eines Binärbaums mit js zur Deduplizierung und Optimierung numerischer Arrays vorgestellt Ihre Referenz. Im Folgenden gibt es nicht viel zu sagen. Schauen wir uns die detaillierte Einführung an.

Gemeinsame zweischichtige Schleife zur Implementierung der Array-Deduplizierung

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)

Erstellen Sie einen Binärbaum, um eine Deduplizierung zu erreichen (gilt nur für Arrays vom numerischen Typ)

Konstruieren Sie die zuvor durchlaufenen Elemente in einen Binärbaum, jeden Knoten im Baum Alle sind erfüllt: der Wert des linken untergeordneten Knotens < der Wert des aktuellen untergeordneten Knotens

Dies optimiert den Prozess der Beurteilung, ob das Element zuvor aufgetreten ist

Wenn das Element größer als der aktuelle Knoten ist, müssen Sie nur feststellen, ob das Element im rechten Teilbaum des Knotens aufgetreten ist.

Wenn das Element kleiner als der aktuelle Knoten ist, müssen Sie nur bestimmen Sie müssen feststellen, ob das Element im linken Teilbaum des Knotens aufgetreten ist. Passen Sie einfach an

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)

Optimierungsidee eins, notieren Sie das Maximum und Minimalwerte

Notieren Sie die Maximal- und Minimalwerte der eingefügten Elemente. Wenn es größer als das größte Element oder kleiner als das kleinste Element ist, fügen Sie es direkt ein

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)

Optimierungsidee 2, konstruiere einen rot-schwarzen Baum

Konstruiere einen Rot-Schwarz-Baum und gleichen Sie die Höhe des Baums aus

Informationen zu Rot-Schwarz-Bäumen finden Sie in der Einfügung „Rot-Schwarz-Bäume“

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 = &#39;red&#39;
 }
}

class RedBlackTree {
 constructor() {
  this.root = null
  this.arr = []
 }

 insert(value) {
  let node = new Node(value)
  if (!this.root) {
   node.color = &#39;black&#39;
   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 = &#39;black&#39;
   this.root = node
   return
  }
  if (node.parent.color === &#39;black&#39;) {
   return
  }
  let son = node
  let father = node.parent
  let grandFather = father.parent
  let directionFtoG = father === grandFather.left ? &#39;left&#39; : &#39;right&#39;
  let uncle = grandFather[directionFtoG === &#39;left&#39; ? &#39;right&#39; : &#39;left&#39;]
  let directionStoF = son === father.left ? &#39;left&#39; : &#39;right&#39;
  if (!uncle || uncle.color === &#39;black&#39;) {
   if (directionFtoG === directionStoF) {
    if (grandFather.parent) {
     grandFather.parent[grandFather.parent.left === grandFather ? &#39;left&#39; : &#39;right&#39;] = father
     father.parent = grandFather.parent
    } else {
     this.root = father
     father.parent = null
    }
    father.color = &#39;black&#39;
    grandFather.color = &#39;red&#39;

    father[father.left === son ? &#39;right&#39; : &#39;left&#39;] && (father[father.left === son ? &#39;right&#39; : &#39;left&#39;].parent = grandFather)
    grandFather[grandFather.left === father ? &#39;left&#39; : &#39;right&#39;] = father[father.left === son ? &#39;right&#39; : &#39;left&#39;]

    father[father.left === son ? &#39;right&#39; : &#39;left&#39;] = 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 = &#39;black&#39;
   uncle.color = &#39;black&#39;
   grandFather.color = &#39;red&#39;
   this.fixTree(grandFather)
  }
 }
}

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

Andere Deduplizierungsmethoden

über Set Object Deduplication

[...new Set(arr)]

Deduplizierung durch sort() + reduce() Methode

nach dem Sortieren Vergleichen Sie, ob benachbarte Elemente gleich sind, und fügen Sie sie dem zurückgegebenen Array hinzu, wenn sie unterschiedlich sind

Es ist erwähnenswert, dass beim Sortieren der Standardwert compare(2, '2') 0 zurückgibt, während in Reduce() ein kongruenter Vergleich durchgeführt wird

let arr = [0, 1, 2, &#39;2&#39;, 2, 5, 7, 11, 7, 5, 2, &#39;2&#39;, 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 === &#39;number&#39;) {
    return -1
   } else {
    return 1
   }
  }
 }
}).reduce((pre, cur) => {
 if (pre !== cur) {
  newArr.push(cur)
  return cur
 }
 return pre
}, null)

Entfernen Duplizierung durch die includes() + map() -Methode

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

Deduplizierung durch die includes() + reduce() -Methode

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

Deduplizierung durch das Schlüssel-Wert-Paar des Objekts + JSON-Objektmethode

let arr = [0, 1, 2, &#39;2&#39;, 2, 5, 7, 11, 7, 5, 2, &#39;2&#39;, 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)))

Das Obige habe ich für alle zusammengestellt und hoffe, dass es in Zukunft für alle hilfreich sein wird.

Verwandte Artikel:

Jquery1.8-Version verwendet Ajax, um Problemanalysen und Lösungen für WeChat-Anrufe zu implementieren

Written Lightweight Ajax-Komponente 01 – Vergleich mit verschiedenen Implementierungsmethoden auf der Webform-Plattform

Analyse der Gründe und Lösungen für das Scheitern des Jquery Ajax-Anforderungsdatei-Downloadvorgangs

Das obige ist der detaillierte Inhalt vonDetaillierte Erläuterung der Deduplizierung und Optimierung numerischer Arrays mithilfe von js zum Aufbau eines Binärbaums. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn