I've created a program (run through node.js) that should be able to create a binary search tree with a variety of methods (insert, remove, print, etc.). It's broken up into two separate files: Tree.js which exports the functions Tree() (and its methods) and test(); and main.js which then imports the functions and runs test().
module.exports = {
//Tree object with constructor
var Tree = function(){
this.data = undefined;
this.leftNode = undefined;
this.rightNode = undefined;
}
//Check if tree/root is empty/undefined
Tree.prototype.isEmpty = function(){
if (this.data === undefined){
return true;
}
else {return false;}
}
//Check if tree/root has no children
Tree.prototype.isLeaf = function(){
//check to make sure node is not undefined
if (this.data !== undefined){
//check to make sure both children are undefined
if (((this.leftNode === undefined) || (this.leftNode.data === undefined))
&& ((this.rightNode === undefined) || (this.rightNode.data === undefined))){
return true;
}
}
else {return false;}
}
//Insert new value as node in tree
Tree.prototype.insert = function(input){
//Node contains no data, assign value to current node
if (this.data === undefined){
this.data = input;
}
//Node contains data
else {
var nextNode = new Tree(); //new node to be added
nextNode.data = input; //assign value to new node
//Value is smaller than data in node
if (input < this.data){
//Assign value to left child if empty
if (this.leftNode === undefined){
this.leftNode = nextNode;
}
//Recursively repeat insertion procedure with left child if value already assigned
else {
this.leftNode.insert(input);
}
}
//Value is greater or equal to data in node
else {
//Assign value to right child if empty
if (this.rightNode === undefined){
this.rightNode = nextNode;
}
//Recursively repeat insertion procedure with left child if value already assigned
else {
this.rightNode.insert(input);
}
}
}
}
Tree.prototype.remove = function(input){
//Node contains input value, wipe node
if (this.data === input){
this.data = undefined;
this.leftNode = undefined;
this.rightNode = undefined;
}
else if (this.isLeaf() == true){
return;
}
//Node contains data
else {
//Value is smaller than data in node
if (input < this.data){
//If left child contains input, wipe left child
if (this.leftNode.data == input){
this.leftNode.data = undefined;
this.leftNode.leftNode = undefined;
this.leftNode.rightNode = undefined;
}
//Recursively repeat removal procedure with left child if value does not match
else {
this.leftNode.remove(input);
}
}
//Value is greater or equal to data in node
else {
//If right child contains input, wipe right child
if (this.rightNode.data == input){
this.rightNode.data = undefined;
this.rightNode.leftNode = undefined;
this.rightNode.rightNode = undefined;
}
//Recursively repeat removal procedure with right child if value does not match
else {
this.rightNode.remove(input);
}
}
}
}
//Find out if value is contained in tree
Tree.prototype.contains = function(input){
//Input is contained in this node
if (this.data == input){
return true;
}
//Input has reached end of tree without finding input
else if (this.isLeaf() == true){
return false;
}
//Input is smaller than data contained in node
else if (input < this.data){
return this.leftChild.contains(input); //Continue searching in left child
}
//Input is greater than data contained in node
else{
return this.rightChild.contains(input);//continue searching in right child
}
}
//return largest value in tree
Tree.prototype.findLargest = function(){
refNode = this.root; //reference node
//loop until reference has no right child
while (refNode.rightChild !== undefined){
refNode = refNode.leftChild;
}
//Rightmost leaf contains Largest value
return refNode.data;
}
//return smallest value in tree
Tree.prototype.findSmallest = function(){
refNode = this.root; //reference node
//loop until reference has no left child
while (refNode.leftChild !== undefined){
refNode = refNode.leftChild;
}
//Leftmost leaf contains smallest value
return refNode.data;
}
//Create copy of tree
Tree.prototype.copy = function(){
//copy each attribute of old tree to new tree
var nextNode = new Tree();
nextNode.data = this.data;
nextNode.leftNode = this.leftNode;
nextNode.rightNode;
//return tree copy
return nextNode;
}
//Output all data values found in tree as an ascending string
Tree.prototype.toString = function(){
var string = '';
if (this.leftNode !== undefined){
string = string + this.leftNode.toString() + ', ';
}
if (this.rightNode !== undefined){
string = string + this.rightNode.toString() + ', ';
}
if (this.data !== undefined){
string = string + this.data;
}
else {
return '';
}
return string;
}
//Apply function to all data values found in tree
Tree.prototype.treeMap = function(funk){
//Apply function to data in current node
if (this.data !== undefined){
this.data = funk(this.data);
//If left child is not undefined, apply method to left child
if (this.leftNode !== undefined){
this.leftNode.treeMap(funk);
}
//If right child is not undefined, apply method to right child
if (this.rightNode !== undefined){
this.rightNode.treeMap(funk);
}
}
}
//Test Tree
var test = function() {
console.log("Creating empty tree/node.\n")
var spruce = new Tree();
console.log("Is this node empty? " + this.isEmpty() + "\n");
spruce.insert(8);
console.log("Inserted value 8. Is node still empty? " +this.isEmpty() + "\n");
console.log("Is this node a leaf? " + this.isLeaf() + "\n");
spruce.insert(3);
spruce.insert(1);
spruce.insert(6);
spruce.insert(4);
spruce.insert(7);
spruce.insert(10);
spruce.insert(14);
spruce.insert(13);
console.log("Contents of tree: " + this.toString() + "\n");
console.log("Inserted several more values. Is this node still a leaf?" + this.isLeaf() + "\n");
console.log("Inserted value 14. Does tree contain 14? " + this.contains(14) + "\n");
spruce.remove(14);
console.log("Let's remove value 14. Does tree still contain 14?" + this.contains(14) + "\n");
console.log("Contents of tree: " + this.toString() + "\n");
console.log("Finally, let's multiply all values within the tree by three! \n")
var triple = function(x){return x * 3;}
spruce.treeMap(triple);
console.log("Contents of tree: " + this.toString() + "\n");
console.log("Now clearing all contents of tree. \n")
spruce.remove(8);
}
}
Then
var treeLib = require("./Tree");
treeLib.test();
However, when I attempt to run main.js, I receive the below error message:
SyntaxError: Unexpected identifier at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:373:25) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object. (/Users/Anubis/Documents/COMP2406/Assignment 1/main.js:1:77) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10)
I've been searching to find the syntax error in question, but have so far been unable to locate it.
The code has a basic syntax error on the second line:
module.exports = {
//Tree object with constructor
var Tree = function(){
// ^----- here
You can't declare variables within an object initializer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.