Files
freeCodeCamp/curriculum/challenges/espanol/10-coding-interview-prep/data-structures/check-if-binary-search-tree.md
2022-09-15 11:30:53 -07:00

3.6 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5cc0c1b32479e176caf3b422 Verificar si un árbol es un Árbol Binario de Búsqueda 1 301624 check-if-tree-is-binary-search-tree

--description--

Como ya sabes lo que es un árbol binario de búsqueda, este desafío establecerá como es que puedes decir que un árbol es un árbol binario de búsqueda o no lo es.

La principal distinción de un árbol de binario de búsqueda es que los nodos están ordenados de forma organizada. Los nodos tienen como máximo 2 nodos hijos (ubicados a la derecha y/o a la izquierda) basados en si el valor de los nodos hijos es mayor o igual que (derecha) o menor que (izquierda) el nodo padre.

--instructions--

En este desafío, crearás una utilidad para tu árbol. Escribe un método en JavaScript isBinarySearchTree el cual tomará un árbol como entrada y devolverá un valor booleano si el árbol es un árbol binario de búsqueda o no. Usa recursividad cuando sea posible.

--hints--

Tu árbol binario de búsqueda debe devolver true cuando se compruebe con isBinarySearchTree().

assert(
  (function () {
    var test = false;
    if (typeof BinarySearchTree !== 'undefined') {
      test = new BinarySearchTree();
    } else {
      return false;
    }
    test.push(1);
    test.push(5);
    test.push(3);
    test.push(2);
    test.push(4);
    return isBinarySearchTree(test) == true;
  })()
);

isBinarySearchTree() debe devolver false cuando compruebe que un árbol no es un árbol binario de búsqueda.

assert(
  (function () {
    var test = false;
    if (typeof BinarySearchTree !== 'undefined') {
      test = new BinarySearchTree();
    } else {
      return false;
    }
    test.push(1);
    test.root.left = new Node(1);
    return isBinarySearchTree(test) == false;
  })()
);

--seed--

--after-user-code--

BinarySearchTree.prototype.push = function(val) {
  var root = this.root;

  if (!root) {
    this.root = new Node(val);
    return;
  }

  var currentNode = root;
  var newNode = new Node(val);

  while (currentNode) {
    if (val < currentNode.value) {
      if (!currentNode.left) {
        currentNode.left = newNode;
        break;
      } else {
        currentNode = currentNode.left;
      }
    } else {
      if (!currentNode.right) {
        currentNode.right = newNode;
        break;
      } else {
        currentNode = currentNode.right;
      }
    }
  }
};

--seed-contents--

var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
  this.value = value;
  this.left = null;
  this.right = null;
}
function BinarySearchTree() {
  this.root = null;
}
function isBinarySearchTree(tree) {
  // Only change code below this line

  // Only change code above this line
}

--solutions--

var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
  this.value = value;
  this.left = null;
  this.right = null;
}
function BinarySearchTree() {
  this.root = null;
}
function isBinarySearchTree(tree) {
  if (tree.root == null) {
    return null;
  } else {
    let isBST = true;
    function checkTree(node) {
      if (node.left != null) {
        const left = node.left;
        if (left.value >= node.value) {
          isBST = false;
        } else {
          checkTree(left);
        }
      }
      if (node.right != null) {
        const right = node.right;
        if (right.value < node.value) {
          isBST = false;
        } else {
          checkTree(right);
        }
      }
    }
    checkTree(tree.root);
    return isBST;
  }
};