Data Structure : remove live code & console.log

This commit is contained in:
Eric Lavault
2021-10-10 16:11:06 +02:00
parent 30779682b9
commit a3d44ad3e1
8 changed files with 32 additions and 153 deletions

View File

@ -32,13 +32,13 @@ const Node = (function Node () {
}
// Visit a node
Node.prototype.visit = function () {
Node.prototype.visit = function (output = value => console.log(value)) {
// Recursively go left
if (this.left !== null) {
this.left.visit()
}
// Print out value
console.log(this.value)
output(this.value)
// Recursively go right
if (this.right !== null) {
this.right.visit()
@ -115,7 +115,7 @@ const Tree = (function () {
// Inorder traversal
Tree.prototype.traverse = function () {
if (!this.root) {
console.log('No nodes are there in the tree till now')
// No nodes are there in the tree till now
return
}
this.root.visit()
@ -124,11 +124,11 @@ const Tree = (function () {
// Start by searching the root
Tree.prototype.search = function (val) {
const found = this.root.search(val)
if (found === null) {
console.log(val + ' not found')
} else {
console.log('Found:' + found.value)
if (found !== null) {
return found.value
}
// not found
return null
}
// Add a new value to the tree
@ -151,16 +151,4 @@ const Tree = (function () {
return Tree
}())
// Implementation of BST
const bst = new Tree()
bst.addValue(6)
bst.addValue(3)
bst.addValue(9)
bst.addValue(2)
bst.addValue(8)
bst.addValue(4)
bst.traverse()
bst.search(8)
bst.removeValue(3)
bst.removeValue(8)
bst.traverse()
export { Tree }

View File

@ -118,20 +118,4 @@ Trie.prototype.findOccurences = function (word) {
return node.count
};
// To test
(function demo () {
const x = new Trie()
x.insert('sheldon')
x.insert('hello')
x.insert('anyword')
x.insert('sheldoncooper')
console.log(x.findOccurences('sheldon'))
x.remove('anything')
x.insert('sheldon')
console.log(x.findOccurences('sheldon'))
console.log(x.findAllWords('sheldon'))
x.insert('anything')
x.remove('sheldoncooper')
console.log(x.contains('sheldoncooper'))
console.log(x.findAllWords('sheldon'))
})()
export { Trie }