TypeError: Невозможно прочитать свойство 'data' из undefined, но оно определено - PullRequest
0 голосов
/ 22 сентября 2018

Я работаю над алгоритмом бинарного дерева поиска и по какой-то причине получаю ошибку типа.Это всегда происходит, когда в дерево вставляется второе значение.В частности, когда значение текущего узла сравнивается со значением входящих данных

Вот код:

class Node {
    constructor(data, left = null, right = null) {
        this.data = data;
        this.leftNode = left;
        this.rightNode = right;
    }
}

class BST {
    constructor() {
        this.root = null;
    }
    insert(data) {
        const dataNode = new Node(data);
        if (this.root === null) {
            this.root = dataNode;
        } else {
            let currentNode = this.root;
            let parentNode;
            while (true) {
                parentNode = currentNode;
                if (data < currentNode.data) {
                    currentNode = parentNode.left;
                    if (parentNode.left === null) {
                        parentNode.left = dataNode
                        break;
                    }
                } else {
                    currentNode = parentNode.right
                    if (parentNode.right === null) {
                        parentNode.right = dataNode
                        break;
                    }
                }
            }
        }
    }
}
const bst = new BST();

bst.insert(10);
bst.insert(5);
bst.insert(6);
bst.insert(8);
bst.insert(12);
bst.insert(7);
bst.insert(7);

Вот ошибка:

Uncaught TypeError: Cannot read property 'data' of undefined
    at BST.insert (<anonymous>:22:32)
    at <anonymous>:42:5

Ответы [ 3 ]

0 голосов
/ 22 сентября 2018

У вас была опечатка.Фиксированный фрагмент ниже:

class Node {
  constructor(data, left = null, right = null) {
    this.data = data;
    this.left = left;
    this.right = right;
  }
}

class BST {
  constructor() {
    this.root= null;
  }
 insert(data) {
    const dataNode = new Node(data);
    if (this.root === null) {
      this.root = dataNode;
    } else {
      let currentNode = this.root;
      let parentNode;
      while (true) {
        parentNode = currentNode;
        if (data < currentNode.data) {
          currentNode = parentNode.left;
          if (parentNode.left === null) {
            parentNode.left = dataNode;
            break;
          }
        } else {
          currentNode=parentNode.right
          if (parentNode.right === null) {
            parentNode.right = dataNode;
            break;
          }
        }
      }
    }
  }
}

const bst = new BST();

bst.insert(10);
bst.insert(5);
bst.insert(6);
bst.insert(8);
bst.insert(12);
bst.insert(7);
bst.insert(7);
0 голосов
/ 22 сентября 2018

Похоже, что вы присвоили parent.left для currentNode, прежде чем присваивать какое-либо значение parentNode.left. Это также произошло для правого.

0 голосов
/ 22 сентября 2018

вы делаете parentNode.left, то есть undefined, в то время как вы должны сделать parentNode.leftNode

...