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 es im Detail anhand von Beispielcodes vor Brauchen Sie es? Lassen Sie uns gemeinsam mit dem Herausgeber unten lernen.
Gemeinsame zweischichtige Schleife zur Implementierung der Array-Deduplizierung
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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)
|
Nach dem Login kopieren
Konstruieren 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. Jeder Knoten im Baum ist 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 ist größer als der aktuelle. Wenn der Knoten groß 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 feststellen Sie müssen lediglich feststellen, ob das Element im linken Teilbaum des Knotens aufgetreten ist.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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)
|
Nach dem Login kopieren
Optimierungsidee eins, das Maximum und das Minimum aufzeichnen Werte
Datensatz Wenn die Maximal- und Minimalwerte der eingefügten Elemente größer als das größte Element oder kleiner als das kleinste Element sind, fügen Sie direkt
< ein 🎜>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 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)
|
Nach dem Login kopieren
Optimierungsidee zwei, Konstruieren Sie einen rot-schwarzen Baum
Konstruieren Sie einen rot-schwarzen Baum und gleichen Sie die Höhe des Baums aus
Für den Teil über den rot-schwarzen Baum sehen Sie sich bitte die Einfügung des rot-schwarzen Baums an
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 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)
|
Nach dem Login kopieren
Andere Deduplizierungsmethoden
Deduplizierung durch Set-Objekt
Duplikate mit der Methode
+ sort()
entfernenreduce()
Benachbarte Elemente vergleichen, nachdem die Sortierung gleich ist. Wenn sie unterschiedlich sind, werden sie dem zurückgegebenen Array hinzugefügt
Es ist zu beachten, dass beim Sortieren der Standardwert
0 zurückgibt, während in Reduce() ein kongruenter Vergleich durchgeführt wird compare(2, '2')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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)
|
Nach dem Login kopieren
Deduplizierung über
+ includes()
map()
1 2 3 | 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))
|
Nach dem Login kopieren
über
+ includes()
Methodendeduplizierungreduce()
1 2 3 4 5 | 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
}, [])
|
Nach dem Login kopieren
Deduplizierung durch das Schlüssel-Wert-Paar des Objekts + JSON-Objektmethode
1 2 3 4 5 6 7 8 | 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)))
|
Nach dem Login kopieren
Verwandte Empfehlungen:
Analyse des Deduplizierungsproblems des zweidimensionalen PHP-Array-Arrays
Zusammenfassung der JS-Array-Deduplizierungsmethode
Mehrere Möglichkeiten zur gemeinsamen Nutzung Duplikate aus JavaScript-Arrays entfernen
Das obige ist der detaillierte Inhalt vonDeduplizierung und Optimierung numerischer Arrays mithilfe des js-Binärbaums. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!