class Node{
constructor(element){
this.element = element;
this.right = null;
this.left = null;
}
}
class BST{
constructor(){
this.head = null;
}
add(element){
var node = new Node(element);
if(this.head === null){
this.head = node;
}else{
this.insertNode(this.head,node);
}
}
insertNode(node,key){
if(key.element < node.element){
if(node.left === null){
node.left = key;
}else{
this.insertNode(node.left,key);
}
}else{
if(node.right === null){
node.right = key;
}else{
this.insertNode(node.right, key);
}
}
}
remove(element){
this.head = this.removeNode(this.head,element);
}
removeNode(node,key){
if(node === null){
return null;
}else if(key < node.element){
node.left = this.removeNode(node.left,key);
return node;
}else if(key > node.element){
node.right = this.removeNode(node.right,key);
return node;
}else{
if(node.left === null && node.right === null){
node = null;
return node;
}
if(node.left === null){
node = node.right;
return node;
}else if(node.right === null){
node = node.left;
return node;
}
var temp = this.findMinNode(node.right);
node.element = temp.element;
node.right = this.removeNode(node.right, temp.element);
return node;
}
}
findMinNode(node){
if(node.left === null){
return node;
}else{
this.findMinNode(node.left);
}
}
}
var bst = new BST();
bst.add(10);
bst.add(5);
bst.add(3);
bst.add(7);
bst.add(4);
bst.add(2);
bst.add(6);
bst.add(9);
bst.add(8);
bst.add(15);
bst.add(24);
bst.add(22);
bst.add(25);
bst.add(21);
bst.remove(3);
console.log(bst);
当我从二叉树中删除节点'5'时,它显示错误'无法读取未定义的属性'element',但是当我从二叉树中删除节点'3'时,它不显示我错误,那时代码可以正常工作,并且可以完美删除节点“ 3”。
在此处输入代码
class Node{
constructor(element){
this.element = element;
this.right = null;
this.left = null;
}
}
class BST{
constructor(){
this.head = null;
}
add(element){
var node = new Node(element);
if(this.head === null){
this.head = node;
}else{
this.insertNode(this.head,node);
}
}
insertNode(node,key){
if(key.element < node.element){
if(node.left === null){
node.left = key;
}else{
this.insertNode(node.left,key);
}
}else{
if(node.right === null){
node.right = key;
}else{
this.insertNode(node.right, key);
}
}
}
remove(element){
this.head = this.removeNode(this.head,element);
}
removeNode(node,key){
if(node === null){
return null;
}else if(key < node.element){
node.left = this.removeNode(node.left,key);
return node;
}else if(key > node.element){
node.right = this.removeNode(node.right,key);
return node;
}else{
if(node.left === null && node.right === null){
node = null;
return node;
}
if(node.left === null){
node = node.right;
return node;
}else if(node.right === null){
node = node.left;
return node;
}
var temp = this.findMinNode(node.right);
node.element = temp.element;
node.right = this.removeNode(node.right, temp.element);
return node;
}
}
findMinNode(node){
if(node.left === null){
return node;
}else{
this.findMinNode(node.left);
}
}
}
var bst = new BST();
bst.add(10);
bst.add(5);
bst.add(3);
bst.add(7);
bst.add(4);
bst.add(2);
bst.add(6);
bst.add(9);
bst.add(8);
bst.add(15);
bst.add(24);
bst.add(22);
bst.add(25);
bst.add(21);
bst.remove(5);
console.log(bst);
输出:用于从二叉树中删除节点5
node.element = temp.element;
^
TypeError: Cannot read property 'element' of undefined
输出:用于从二叉树中删除节点3。
BST {
head: Node {
element: 10,
right: Node { element: 15, right: [Node], left: null },
left: Node { element: 5, right: [Node], left: [Node] }
}
}
答案 0 :(得分:0)
问题出在findMinNode
方法中。您可能忘记了在return
语句中添加else
:
findMinNode(node) {
if (node.left === null) {
return node;
} else {
// return the value of recursive call
return this.findMinNode(node.left);
}
}