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
3 Answers
Reset to default 6you 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
版权声明:本文标题:javascript - TypeError: Cannot read property 'data' of undefined - but it is defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741595796a2387401.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论