function Node(value, left, right) {
this.value = value;
this.left = left;
this.right = right;
this.show = show;
}
function show() {
return this.value;
}
function BST() {
this.root = null;
this.insert = insert;
this.preOrder = preOrder;
this.inOrder = inOrder;
this.postOrder = postOrder;
this.find = find;
this.findParent = findParent;
this.getMin = getMin;
this.getMax = getMax;
this.remove = remove;
this.removeNode = removeNode;
}
function insert(value) {
let newNode = new Node(value, null, null);
if (this.root == null) {
this.root = newNode;
} else {
let currentNode = this.root;
while (true) {
let parentNode = currentNode;
if (value < currentNode.value) {
currentNode = currentNode.left;
if (currentNode == null) {
parentNode.left = newNode;
break;
}
} else {
currentNode = currentNode.right;
if (currentNode == null) {
parentNode.right = newNode;
break;
}
}
}
}
}
function inOrder(node) {
if (node != null) {
inOrder(node.left);
console.log(node.show() + ' ');
inOrder(node.right);
}
}
function preOrder(node) {
if (node != null) {
console.log(node.show() + ' ');
preOrder(node.left);
preOrder(node.right);
}
}
function postOrder(node) {
if (node != null) {
postOrder(node.left);
postOrder(node.right);
console.log(node.show() + ' ');
}
}
function getMin(node) {
let currentNode = node;
while (true) {
if (currentNode.left != null) {
currentNode = currentNode.left;
} else {
return currentNode.value;
}
}
}
function getMax(node) {
let currentNode = node;
while (true) {
if (currentNode.right != null) {
currentNode = currentNode.right;
} else {
return currentNode.value;
}
}
}
function find(value) {
let currentNode = this.root;
if (value == currentNode.value) {
return currentNode;
}
while (true) {
if (value < currentNode.value) {
currentNode = currentNode.left;
if (value == currentNode.value) {
return currentNode;
} else if (currentNode.left == null && currentNode.right == null) {
break;
}
} else if (value > currentNode.value) {
currentNode = currentNode.right;
if (value == currentNode.value) {
return currentNode;
} else if (currentNode.left == null && currentNode.right == null) {
break;
}
}
}
return null;
}
function findParent(value) {
let currentNode = this.root;
if (value == currentNode.value) {
return null;
}
while (true) {
let parentNode = currentNode;
if (value < currentNode.value) {
currentNode = currentNode.left;
if (value == currentNode.value) {
return parentNode;
} else if (currentNode.left == null && currentNode.right == null) {
break;
}
} else if (value > currentNode.value) {
currentNode = currentNode.right;
if (value == currentNode.value) {
return parentNode;
} else if (currentNode.left == null && currentNode.right == null) {
break;
}
}
}
return null;
}
function remove( value ) {
removeNode( this.root , value);
}
function getSmallest(node) {
if (node.left == null) {
return node;
}
else {
return getSmallest(node.left);
}
}
function removeNode( node , value ) {
if( node == null ) {
return null;
}
if(value == node.value) {
if(node.left == null && node.right == null) {
return null;
}
if(node.left == null) {
return node.right;
}
if(node.right == null) {
return node.left;
}
var tempNode = getSmallest(node.right);
node.value = tempNode.value;
node.right = removeNode(node.right,tempNode.value);
return node;
}else if(value < node.value) {
node.left = removeNode( node.left,value);
return node;
}else {
node.right = removeNode( node.right,value);
return node;
}
}
var bst = new BST();
bst.insert(50);
bst.insert(30);
bst.insert(60);
bst.insert(10);
bst.insert(40);
bst.insert(35);
bst.insert(32);
bst.insert(37);
bst.insert(45);
bst.inOrder(bst.root);
bst.remove(30)
console.log('-------------')
bst.inOrder(bst.root);
JS中的設計模式與數據結構之二叉排序樹
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.