admin管理员组

文章数量:1294381

I am working on a binary search tree algorithm and for some reason, I keep getting a type error. It always occurs when a second value is being inserted into the tree. Specifically when the current node's value is being pared to the ining data value

Here's the code:

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);

I am working on a binary search tree algorithm and for some reason, I keep getting a type error. It always occurs when a second value is being inserted into the tree. Specifically when the current node's value is being pared to the ining data value

Here's the code:

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);

Here's the error:

Uncaught TypeError: Cannot read property 'data' of undefined
    at BST.insert (<anonymous>:22:32)
    at <anonymous>:42:5
Share Improve this question edited Sep 22, 2018 at 18:57 VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked Sep 22, 2018 at 18:54 AlsidneioAlsidneio 331 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

you are doing parentNode.left which is undefined while you should do parentNode.leftNode

It seems you assigned parent.left to currentNode before you assign any value to parentNode.left This also happened for right one.

You had a typo. Fixed snippet below:

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);

本文标签: javascriptTypeError Cannot read property 39data39 of undefinedbut it is definedStack Overflow