다양한 방법 (삽입, 제거, 인쇄)으로 이진 검색 트리를 만들 수 있어야하는 프로그램 (node.js를 통해 실행)을 만들었습니다. , 등). 두 개의 분리 된 파일로 나뉩니다 : Tree() (및 그 메소드) 및 test(); main.js는 함수를 임포트하고 test()를 실행합니다. 나는 구문 오류를 찾기 위해 검색했습니다이진 검색 트리 (Node.js) - SyntaxError : 예기치 않은 식별자
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)
: 나는 main.js를 실행하려고 할 때
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();
는, 나는 다음과 같은 오류 메시지가 나타납니다 문제의 위치를 찾을 수 없었습니다. 당신은 개체 이니셜 라이저 내에서 변수를 선언 할 수 없습니다
module.exports = {
//Tree object with constructor
var Tree = function(){
// ^----- here
:
Stack Snippets (사용한 '<>'툴바 버튼)는 브라우저에서 실행 가능한 코드가 아닌 ** 실행 가능한 브라우저 내부 코드 **에 사용되며 예외 목록에는 사용할 수 없습니다. –