2016-09-28 7 views
-2

さまざまな方法(挿入、削除、印刷)でバイナリ検索ツリーを作成できるプログラムを作成しました。 、など)。これは2つの別々のファイルに分割されています:Tree()(とそのメソッド)とtest();関数をエクスポートするTree.js; main.jsは関数をインポートしてtest()を実行します。バイナリ検索ツリー(Node.js) - SyntaxError:予期しない識別子

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); 
} 
} 

はその後

var treeLib = require("./Tree"); 
treeLib.test(); 

しかし、私はmain.jsを実行しようとすると、私は以下のエラーメッセージが表示されます

 
    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) 

を私は構文エラーを見つけるために検索してきましたこれまでのところそれを見つけることができなかった。あなたは、オブジェクト初期化子内の変数を宣言することはできません

module.exports = { 
    //Tree object with constructor 
    var Tree = function(){ 
// ^----- here 

+2

スタックスニペット(使用した '<>'ツールバーボタン)は**ブラウザで実行可能な**のコードであり、ブラウザでは実行できないコードではなく、例外リストではありません。 –

答えて

2

コードは、2行目に、基本的な構文エラーがあります。

+0

これは仕事をしました。親切にありがとう! –

関連する問題