1. 为 BST 类增加一个新方法,该方法返回 BST 中节点的个数。
function Node(data, left, right) {
this.data = data;
this.left = left;
this.right = right;
this.show = show;
}
function show() {
return this.data;
}
function BST() {
this.root = null;
this.insert = insert;
this.inOrder = inOrder;
this.preOrder = preOrder;
this.postOrder = postOrder;
this.getMin = getMin;
this.getMax = getMax;
this.find = find;
this.remove = remove;
this.size = size;
this.edgeCount = edgeCount;
}
function insert(data) {
var n = new Node(data, null, null);
if (this.root === null) {
this.root = n;
} else {
var current = this.root;
var parent;
while (true) {
parent = current;
if (data < current.data) {
current = current.left;
if (current === null) {
parent.left = n;
break;
}
} else {
current = current.right;
if (current === null) {
parent.right = n;
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() {
var current = this.root;
while (!(current.left === null)) {
current = current.left;
}
return current.data;
}
function getMax() {
var current = this.root;
while (!(current.right === null)) {
current = current.right;
}
return current.data;
}
function find(data) {
var current = this.root;
while (current !== null) {
if (current.data === data) {
return current;
} else if (current.data > data) {
current = current.left;
} else {
current = current.right;
}
}
return null;
}
function remove(data) {
this.root = removeNode(this.root, data);
}
function removeNode(node, data) {
if (node === null) {
return null;
}
if (data === node.data) {
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.data = tempNode.data;
node.right = removeNode(node.right, tempNode.data);
return node;
} else if (data < node.data) {
node.left = removeNode(node.left, data);
return node;
} else {
node.right = removeNode(node.right, data);
return node;
}
}
function getSmallest(node) {
while (node.left != null) {
node = node.left;
}
return node;
}
function size(node) {
if (node === null) {
return 0;
}
return 1 + size(node.left) + size(node.right);
}
2. 为 BST 类增加一个新方法,该方法返回 BST 中边的个数。
function Node(data, left, right) {
this.data = data;
this.left = left;
this.right = right;
this.show = show;
}
function show() {
return this.data;
}
function BST() {
this.root = null;
this.insert = insert;
this.inOrder = inOrder;
this.preOrder = preOrder;
this.postOrder = postOrder;
this.getMin = getMin;
this.getMax = getMax;
this.find = find;
this.remove = remove;
this.size = size;
this.edgeCount = edgeCount;
}
function insert(data) {
var n = new Node(data, null, null);
if (this.root === null) {
this.root = n;
} else {
var current = this.root;
var parent;
while (true) {
parent = current;
if (data < current.data) {
current = current.left;
if (current === null) {
parent.left = n;
break;
}
} else {
current = current.right;
if (current === null) {
parent.right = n;
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() {
var current = this.root;
while (!(current.left === null)) {
current = current.left;
}
return current.data;
}
function getMax() {
var current = this.root;
while (!(current.right === null)) {
current = current.right;
}
return current.data;
}
function find(data) {
var current = this.root;
while (current !== null) {
if (current.data === data) {
return current;
} else if (current.data > data) {
current = current.left;
} else {
current = current.right;
}
}
return null;
}
function remove(data) {
this.root = removeNode(this.root, data);
}
function removeNode(node, data) {
if (node === null) {
return null;
}
if (data === node.data) {
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.data = tempNode.data;
node.right = removeNode(node.right, tempNode.data);
return node;
} else if (data < node.data) {
node.left = removeNode(node.left, data);
return node;
} else {
node.right = removeNode(node.right, data);
return node;
}
}
function getSmallest(node) {
while (node.left != null) {
node = node.left;
}
return node;
}
function size(node) {
if (node === null) {
return 0;
}
return 1 + size(node.left) + size(node.right);
}
function edgeCount(node) {
if (node === null) {
return 0;
}
return size(node) - 1;
}
3. 为 BST 类增加一个新方法 max(),该方法返回 BST 中的最大值。
同getMax()
4. 为 BST 类增加一个新方法 min(),该方法返回 BST 中的最小值。
同getMin()
5. 写一段程序,读入一个较大的文本文件,并将其中的单词保存到 BST 中,显示每个单词 在文本中出现的次数。
function Node(data, left, right) {
this.data = data;
this.left = left;
this.right = right;
this.show = show;
}
function show() {
return this.data.word;
}
function BST() {
this.root = null;
this.insert = insert;
this.inOrder = inOrder;
this.preOrder = preOrder;
this.postOrder = postOrder;
this.getMin = getMin;
this.getMax = getMax;
this.find = find;
this.remove = remove;
this.size = size;
this.edgeCount = edgeCount;
}
function insert(data) {
var n = new Node(data, null, null);
if (this.root === null) {
this.root = n;
} else {
var current = this.root;
var parent;
while (true) {
parent = current;
if (data.word < current.data.word) {
current = current.left;
if (current === null) {
parent.left = n;
break;
}
} else {
current = current.right;
if (current === null) {
parent.right = n;
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() {
var current = this.root;
while (!(current.left === null)) {
current = current.left;
}
return current.data;
}
function getMax() {
var current = this.root;
while (!(current.right === null)) {
current = current.right;
}
return current.data;
}
function find(data) {
var current = this.root;
while (current !== null) {
if (current.data.word === data.word) {
return current;
} else if (current.data.word > data.word) {
current = current.left;
} else {
current = current.right;
}
}
return null;
}
function remove(data) {
this.root = removeNode(this.root, data);
}
function removeNode(node, data) {
if (node === null) {
return null;
}
if (data.word === node.data.word) {
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.data = tempNode.data;
node.right = removeNode(node.right, tempNode.data);
return node;
} else if (data.word < node.data.word) {
node.left = removeNode(node.left, data);
return node;
} else {
node.right = removeNode(node.right, data);
return node;
}
}
function getSmallest(node) {
while (node.left != null) {
node = node.left;
}
return node;
}
function size(node) {
if (node === null) {
return 0;
}
return 1 + size(node.left) + size(node.right);
}
function edgeCount(node) {
if (node === null) {
return 0;
}
return size(node) - 1;
}
function updateWordCount(word) {
var node = bst.find({word: word, count: 0});
if (node === null) {
bst.insert({word: word, count: 1});
} else {
node.data.count++;
}
}
function displayWordCounts(node) {
if (node !== null) {
displayWordCounts(node.left);
console.log(node.data.word + ": " + node.data.count);
displayWordCounts(node.right);
}
}
var inputString = "This is a test string with some words and some repeated words and some unique words";
var words = inputString.split(/\s+/);
var bst = new BST();
words.forEach(function(word) {
updateWordCount(word);
});
displayWordCounts(bst.root);