patternjavascriptMinor
LinkedList and binary search tree in JavaScript
Viewed 0 times
searchlinkedlistjavascriptbinaryandtree
Problem
I recently decided to make a
Linked List:
```
var Node = function(value) {
this.value = value;
this.next=null;
return this;
};
var LinkedList = function(node){
this.head = node;
return this;
};
LinkedList.prototype.insertEnd = function(newNode, currentNode) {
var currentNode = currentNode || this.head;
if(currentNode.next !== null) {
return this.insertEnd(newNode, currentNode.next);
} else {
currentNode.next = newNode;
}
};
LinkedList.prototype.insertBeginning = function(newNode) {
newNode.next = this.head;
this.head = newNode;
};
LinkedList.prototype.search = function(searchValue, currentNode) {
var currentNode = currentNode || this.head;
if(currentNode.value == searchValue) {
console.log("true");
return true;
} else if(currentNode.next !== null) {
return this.search(searchValue, currentNode.next);
}
console.log("not found");
return false;
};
LinkedList.prototype.remove = function(deleteValue, currentNode, parentNode) {
currentNode = currentNode || this.head;
if(currentNode.value === deleteValue) {
if(currentNode.next !== null) {
parentNode.next = currentNode.next;
} else {
parentNode.next = null;
}
} else if(currentNode.next !== null) {
return this.remove(deleteValue, currentNode.next, currentNode);
}
};
LinkedList.prototype.size = function(currentNode, size) {
var currentNode = currentNode || this.head;
var size = size || 1;
if(currentNode.next !== null) {
return this.size(currentNode.next, size+1);
} else {
console.log(size);
return size;
}
};
(function(){
// LinkedList Example
var linkedList = new LinkedList(new Node("oldHead"));
linkedList.insertEnd(new Node(2));
LinkedList and binary search tree using JavaScript. I wanted to reach out to a wider audience and see how I did, and where could I improve.Linked List:
```
var Node = function(value) {
this.value = value;
this.next=null;
return this;
};
var LinkedList = function(node){
this.head = node;
return this;
};
LinkedList.prototype.insertEnd = function(newNode, currentNode) {
var currentNode = currentNode || this.head;
if(currentNode.next !== null) {
return this.insertEnd(newNode, currentNode.next);
} else {
currentNode.next = newNode;
}
};
LinkedList.prototype.insertBeginning = function(newNode) {
newNode.next = this.head;
this.head = newNode;
};
LinkedList.prototype.search = function(searchValue, currentNode) {
var currentNode = currentNode || this.head;
if(currentNode.value == searchValue) {
console.log("true");
return true;
} else if(currentNode.next !== null) {
return this.search(searchValue, currentNode.next);
}
console.log("not found");
return false;
};
LinkedList.prototype.remove = function(deleteValue, currentNode, parentNode) {
currentNode = currentNode || this.head;
if(currentNode.value === deleteValue) {
if(currentNode.next !== null) {
parentNode.next = currentNode.next;
} else {
parentNode.next = null;
}
} else if(currentNode.next !== null) {
return this.remove(deleteValue, currentNode.next, currentNode);
}
};
LinkedList.prototype.size = function(currentNode, size) {
var currentNode = currentNode || this.head;
var size = size || 1;
if(currentNode.next !== null) {
return this.size(currentNode.next, size+1);
} else {
console.log(size);
return size;
}
};
(function(){
// LinkedList Example
var linkedList = new LinkedList(new Node("oldHead"));
linkedList.insertEnd(new Node(2));
Solution
Most of your code looks like textbook code.
Your
Your traversal functions should not hard-code
Your
size function shouldn't have to take a second argument.LinkedList.prototype.size = function(currentNode /* optional */) {
// var currentNode, shadowing the currentNode param, is weird
var node = currentNode || this.head;
// I prefer to put the base case first
if (node.next == null) {
return 1;
} else {
return 1 + this.size(node.next);
}
};Your traversal functions should not hard-code
console.log() as the action. For flexibility, they should take a visitor function as a callback.// Fixed capitalization of "inOrderTraversal"
Node.prototype.inOrderTraversal = function(visitor) {
if (this.left !== null) {
this.left.inOrderTraversal(visitor);
}
visitor(this.value);
if(this.right !== null) {
this.right.inOrderTraversal(visitor);
}
};
// Elsewhere...
tree.inOrderTraversal(console.log);Code Snippets
LinkedList.prototype.size = function(currentNode /* optional */) {
// var currentNode, shadowing the currentNode param, is weird
var node = currentNode || this.head;
// I prefer to put the base case first
if (node.next == null) {
return 1;
} else {
return 1 + this.size(node.next);
}
};// Fixed capitalization of "inOrderTraversal"
Node.prototype.inOrderTraversal = function(visitor) {
if (this.left !== null) {
this.left.inOrderTraversal(visitor);
}
visitor(this.value);
if(this.right !== null) {
this.right.inOrderTraversal(visitor);
}
};
// Elsewhere...
tree.inOrderTraversal(console.log);Context
StackExchange Code Review Q#31513, answer score: 6
Revisions (0)
No revisions yet.