Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Intentando crear un 'trie' en Javascript. No puedo entender por qué mi método no está agregando a los niños correctamente

Aquí está la función 'constructor' así como el método 'recuadro' que creé...

 function Trie(value) { this.value = value; this.endOfWord = false; this.children = {}; } Trie.prototype.insert = function(string) { let node = new Trie(null); for(let character of string) { if (node.children[character] === undefined) { node.children[character] = new Trie(character); } node = node.children[character]; } node.endOfWord = true; };

Aquí está el caso de prueba que creé...

 let trie = new Trie; trie.insert('hello'); console.log(trie)

la salida para el registro de la consola es...

 Trie { value: undefined, endOfWord: false, children: {} }

según mi entrada en la función 'insertar' que esperaba ...

 Trie { value: 'h', endOfWord: false, children: {value: 'e', endOfWord: false, children: {value: 'l', endOfWord: false, children: {value: 'l', endOfWord: false, children: {value: 'o', endOfWord: false, children: {}}}}} }

¿Alguna pista o consejo sobre por qué esto no está agregando a los niños correctamente?

¡Gracias por tu tiempo!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe usar los elementos secundarios en la instancia en la que se encuentra, no en la nueva instancia que creó.

Entonces, cuando dejas let node = new Trie(null); está creando una nueva instancia. Está agregando los niños a eso, no la instancia actual que ya creó.

 function Trie(value) { this.value = value; this.endOfWord = false; this.children = {}; } Trie.prototype.insert = function(string) { let node = this; for(let character of string) { if (node.children[character] === undefined) { node.children[character] = new Trie(character); } node = node.children[character]; } node.endOfWord = true; }; let trie = new Trie; trie.insert('hello'); console.log(trie)

about 4 years ago · Juan Pablo Isaza Report

0

Simplemente está creando un nuevo nodo Trie y asignándole todas las propiedades. No lo estás usando en ningún lado.

Está considerando el node dentro de la función de insert como el objeto que lo está llamando y debe asignar todas las propiedades como hijos:

 let node = this;

Puede usar this directamente, pero para simplificar, acabo de asignar el objeto actual al node

 function Trie(value) { this.value = value; this.endOfWord = false; this.children = {}; } Trie.prototype.insert = function(string) { const node = this; string.split("").forEach((character, index) => { if (node.children[character] === undefined) { node.children[character] = new Trie(character); if (index === string.length - 1) node.children[character].endOfWord = true; } }); }; let trie = new Trie(); trie.insert("hello"); console.log(trie);
 /* This is not a part of answer. It is just to give the output full height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!