Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
Should I put `addNode()` in the `Node` class or `Tree`?

I'm trying to implement a binary search tree (BST) with javascript. I came up with 2 versions.

Here is the first version

class Node {
  constructor(value) {
    this.value = value
    this.left = null
    this.right = null
  }
}

class Tree {
  constructor(value) {
    if (value != null) {
      this.root = new Node(value)
    } else {
      return null;
    }
  }
  addNode(parent, n) {
    if (n.value === parent.value) {
      return;
    } else if (n.value < parent.value) {
      if (parent.left == null) {
        parent.left = n;
      } else {
        this.addNode(parent.left, n)
      }
    } else { // n.value > parent.value
      if (parent.right == null) {
        parent.right = n;
      } else {
        this.addNode(parent.right, n)
      }
    }
  }
  addValue(val) {
    let n = new Node(val);
    if (this.root == null) {
      this.root = n;
    } else {
      this.addNode(this.root, n);
    }
  }
}

I defined a simple class Node and the Tree class.

The Node class abstracts any node that ranges from the root to a leaf.

The Tree class handles all the business, especially the addNode method. In contrast, my 2nd version put the addNode method in Node class. Here is the code

class Node {
  constructor(value) {
    this.value = value
    this.left = null
    this.right = null
  }
  addNode(n) {
    if (n.value == this.value) {
      return;
    } else if (n.value < this.value) {
      if (this.left == null) {
        this.left = n;
      } else {
        this.left.addNode(n)
      }
    } else {
      if (this.right == null) {
        this.right = n;
      } else {
        this.right.addNode(n)
      }
    }
  }
}

class Tree {
  constructor(value) {
    if (value != null) {
      this.root = new Node(value)
    } else {
      return null;
    }
  }
  addValue(val) {
    let n = new Node(val);
    if (this.root == null) {
      this.root = n;
    } else {
      this.root.addNode(n);
    }
  }
}

Both versions works as expected. I know I should add more check, in case something like tree.addValue(); happens.

I'd just like to know which version should I go with, and why. Are there some kind of principles or consideration to make such decision?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda