functionHashTable(){this.table =newArray(137);this.simpleHash = simpleHash;this.showDistro = showDistro;this.put = put;}functionput(data){var key =this.simpleHash(data);this.table[key]= data;}functionsimpleHash(data){var total =0;for(let i =0; i < data.length; i++){
total += data.charCodeAt(i);}return total %this.table.length;}functionshowDistro(){var n =0;for(var i =0; i <this.table.length;++i){if(this.table[i]!= undefined){
console.log(i +": "+this.table[i]);}}}
someNames =["David","Jennifer","Donnie","Raymond","Cynthia","Mike","Clayton","Danny","Jonathan"];var hTable =newHashTable();for(var i =0; i < someNames.length;++i){
hTable.put(someNames[i]);}
hTable.showDistro();
集合
functionSet(){this.dataStore =[];this.add = add;this.remove = remove;this.size = size;this.union = union;this.intersect = intersect;this.subset = subset;this.difference = difference;this.show = show;this.contains = contains;}functionadd(data){if(this.dataStore.indexOf(data)==-1){this.dataStore.push(data);returntrue;}elsereturnfalse;}functionremove(data){var index =this.dataStore.indexOf(data);if(index !=-1){this.dataStore.splice(index,1);returntrue;}elsereturnfalse;}functionsize(){returnthis.dataStore.size;}functionshow(){returnthis.dataStore;}functioncontains(data){if(this.dataStore.indexOf(data)!=-1){returntrue;}elsereturnfalse;}functionunion(set){var tempSet =newSet();for(var i =0; i <this.dataStore.length;++i){
tempSet.add(this.dataStore[i]);}for(var i =0; i <set.dataStore.length;++i){if(!tempSet.contains(set.dataStore[i])){
tempSet.dataStore.push(set.dataStore[i]);}}return tempSet;}functionintersect(set){var tempSet =newSet();for(var i =0; i <this.dataStore.length;++i){if(set.contains(this.dataStore[i])){
tempSet.add(this.dataStore[i]);}}return tempSet;}functionsubset(set){if(this.size()>set.size()){returnfalse;}else{for(var member inthis.dataStore){if(!set.contains(member)){returnfalse;}}}returntrue;}functiondifference(set){var tempSet =newSet();for(var i =0; i <this.dataStore.length;++i){if(!set.contains(this.dataStore[i])){
tempSet.add(this.dataStore[i]);}}return tempSet;}var it =newSet();
it.add("Cynthia");
it.add("Clayton");
it.add("Jennifer");
it.add("Danny");
it.add("Jonathan");
it.add("Terrill");
it.add("Raymond");
it.add("Mike");var dmp =newSet();
dmp.add("Cynthia");
dmp.add("Raymond");
dmp.add("Jonathan");if(dmp.subset(it)){
console.log("DMP is a subset of IT.");}else{
console.log("DMP is not a subset of IT.");}
二叉树
functionNode(data){this.data = data;this.right =null;this.left =null;this.show = show;}functionshow(){returnthis.data;}functionBST(){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;}functioninsert(data){var newNode =newNode(data);if(this.root ==null)this.root = newNode;else{var current =this.root;while(true){if(data > current.data){if(current.right ==null){
current.right = newNode;break;}else
current = current.right;}else{if(current.left ==null){
current.left = newNode;break;}else
current = current.left;}}}}functioninOrder(node){if(node !=null){inOrder(node.left);
console.log(node.data);inOrder(node.right);}}functionpreOrder(node){if(node){
console.log(node.data);preOrder(node.left);preOrder(node.right);}}functionpostOrder(node){if(node){postOrder(node.left);postOrder(node.right);
console.log(node.data);}}functiongetMin(){var current =this.root;while(current.left !=null)
current = current.left;return current.data;}functiongetMax(){var current =this.root;while(current.right !=null)
current = current.right;return current.data;}functionfind(data){var current =this.root;while(current){if(current.data == data)return current;elseif(current.data < data)
current = current.right;else
current = current.left;}returnnull;}functionremove(data){
root =removeNode(this.root, data);}functionremoveNode(node, data){if(node ==null){returnnull;}if(data == node.data){// 没有子节点的节点if(node.left ==null&& node.right ==null){returnnull;}// 没有左子节点的节点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;}elseif(data < node.data){
node.left =removeNode(node.left, data);return node;}else{
node.right =removeNode(node.right, data);return node;}}var nums =newBST();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(22);inOrder(nums.root);
console.log("\n");var value =parseInt(23);var found = nums.find(value);if(found !=null){
console.log("Found "+ value +" in the BST.");}else{
console.log(value +" was not found in the BST.");}