BinarySearchTree's insert function in javascriptHow do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?var functionName = function() vs function functionName() Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?
Prove that NP is closed under karp reduction?
Fencing style for blades that can attack from a distance
Theorems that impeded progress
Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?
What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)
Can a Warlock become Neutral Good?
How to write a macro that is braces sensitive?
What are the differences between the usage of 'it' and 'they'?
Can I make popcorn with any corn?
tikz: show 0 at the axis origin
Why, historically, did Gödel think CH was false?
In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?
What defenses are there against being summoned by the Gate spell?
Finding angle with pure Geometry.
What do you call a Matrix-like slowdown and camera movement effect?
Is a tag line useful on a cover?
Smoothness of finite-dimensional functional calculus
Which models of the Boeing 737 are still in production?
How is it possible to have an ability score that is less than 3?
Maximum likelihood parameters deviate from posterior distributions
What does it mean to describe someone as a butt steak?
What are these boxed doors outside store fronts in New York?
Watching something be written to a file live with tail
Dragon forelimb placement
BinarySearchTree's insert function in javascript
How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?var functionName = function() vs function functionName() Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I was building a BinarySearchTree by using javascript
but the insert function in my code deoesn't run what i want
my ideal result is
"value":9,"left":"value":4,"left":"value":1,"left":null,"right":null,"right":"value":6,"left":null,"right":null,"right":"value":20,"left":"value":15,"left":null,"right":null,"right":"value":170,"left":null,"right":null
but my result is
'"value":9,"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":null'
I think the problem is in my insert function
Here is my JS:
class Node
constructor(value)
this.left = null;
this.right = null;
this.value = value;
class BinarySearchTree
constructor()
this.root = null;
insert(value)
var root=this.root
if(!root)
this.root=new Node(value)
else
var currentNode=root;
var newNode=new Node(value)
while(currentNode)
if(value<currentNode.value)
if(!currentNode.left)
currentNode.left=new Node;
break;
else
currentNode=currentNode.left;
else
if(!currentNode.right)
currentNode.right=new Node;
break;
else
currentNode=currentNode.right
lookup(value)
var root=this.root
var searchNode = new Node(value)
if(!this.root)
return null;
if(searchNode===root)
return root
else
if(searchNode.value>root)
return root.right.value
else
return root.left.value
const tree = new BinarySearchTree();
tree.insert(9)
tree.insert(4)
tree.insert(6)
tree.insert(20)
tree.insert(170)
tree.insert(15)
tree.insert(1)
JSON.stringify(traverse(tree.root))
// 9
// 4 20
//1 6 15 170
function traverse(node)
const tree = value: node.value ;
tree.left = node.left === null ? null : traverse(node.left);
tree.right = node.right === null ? null : traverse(node.right);
return tree;
javascript binary-search-tree
add a comment |
I was building a BinarySearchTree by using javascript
but the insert function in my code deoesn't run what i want
my ideal result is
"value":9,"left":"value":4,"left":"value":1,"left":null,"right":null,"right":"value":6,"left":null,"right":null,"right":"value":20,"left":"value":15,"left":null,"right":null,"right":"value":170,"left":null,"right":null
but my result is
'"value":9,"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":null'
I think the problem is in my insert function
Here is my JS:
class Node
constructor(value)
this.left = null;
this.right = null;
this.value = value;
class BinarySearchTree
constructor()
this.root = null;
insert(value)
var root=this.root
if(!root)
this.root=new Node(value)
else
var currentNode=root;
var newNode=new Node(value)
while(currentNode)
if(value<currentNode.value)
if(!currentNode.left)
currentNode.left=new Node;
break;
else
currentNode=currentNode.left;
else
if(!currentNode.right)
currentNode.right=new Node;
break;
else
currentNode=currentNode.right
lookup(value)
var root=this.root
var searchNode = new Node(value)
if(!this.root)
return null;
if(searchNode===root)
return root
else
if(searchNode.value>root)
return root.right.value
else
return root.left.value
const tree = new BinarySearchTree();
tree.insert(9)
tree.insert(4)
tree.insert(6)
tree.insert(20)
tree.insert(170)
tree.insert(15)
tree.insert(1)
JSON.stringify(traverse(tree.root))
// 9
// 4 20
//1 6 15 170
function traverse(node)
const tree = value: node.value ;
tree.left = node.left === null ? null : traverse(node.left);
tree.right = node.right === null ? null : traverse(node.right);
return tree;
javascript binary-search-tree
Just filter out all null items...
– Jack Bashford
Mar 8 at 4:11
newNodenotnew Node
– zer00ne
Mar 8 at 4:11
add a comment |
I was building a BinarySearchTree by using javascript
but the insert function in my code deoesn't run what i want
my ideal result is
"value":9,"left":"value":4,"left":"value":1,"left":null,"right":null,"right":"value":6,"left":null,"right":null,"right":"value":20,"left":"value":15,"left":null,"right":null,"right":"value":170,"left":null,"right":null
but my result is
'"value":9,"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":null'
I think the problem is in my insert function
Here is my JS:
class Node
constructor(value)
this.left = null;
this.right = null;
this.value = value;
class BinarySearchTree
constructor()
this.root = null;
insert(value)
var root=this.root
if(!root)
this.root=new Node(value)
else
var currentNode=root;
var newNode=new Node(value)
while(currentNode)
if(value<currentNode.value)
if(!currentNode.left)
currentNode.left=new Node;
break;
else
currentNode=currentNode.left;
else
if(!currentNode.right)
currentNode.right=new Node;
break;
else
currentNode=currentNode.right
lookup(value)
var root=this.root
var searchNode = new Node(value)
if(!this.root)
return null;
if(searchNode===root)
return root
else
if(searchNode.value>root)
return root.right.value
else
return root.left.value
const tree = new BinarySearchTree();
tree.insert(9)
tree.insert(4)
tree.insert(6)
tree.insert(20)
tree.insert(170)
tree.insert(15)
tree.insert(1)
JSON.stringify(traverse(tree.root))
// 9
// 4 20
//1 6 15 170
function traverse(node)
const tree = value: node.value ;
tree.left = node.left === null ? null : traverse(node.left);
tree.right = node.right === null ? null : traverse(node.right);
return tree;
javascript binary-search-tree
I was building a BinarySearchTree by using javascript
but the insert function in my code deoesn't run what i want
my ideal result is
"value":9,"left":"value":4,"left":"value":1,"left":null,"right":null,"right":"value":6,"left":null,"right":null,"right":"value":20,"left":"value":15,"left":null,"right":null,"right":"value":170,"left":null,"right":null
but my result is
'"value":9,"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":"left":null,"right":null'
I think the problem is in my insert function
Here is my JS:
class Node
constructor(value)
this.left = null;
this.right = null;
this.value = value;
class BinarySearchTree
constructor()
this.root = null;
insert(value)
var root=this.root
if(!root)
this.root=new Node(value)
else
var currentNode=root;
var newNode=new Node(value)
while(currentNode)
if(value<currentNode.value)
if(!currentNode.left)
currentNode.left=new Node;
break;
else
currentNode=currentNode.left;
else
if(!currentNode.right)
currentNode.right=new Node;
break;
else
currentNode=currentNode.right
lookup(value)
var root=this.root
var searchNode = new Node(value)
if(!this.root)
return null;
if(searchNode===root)
return root
else
if(searchNode.value>root)
return root.right.value
else
return root.left.value
const tree = new BinarySearchTree();
tree.insert(9)
tree.insert(4)
tree.insert(6)
tree.insert(20)
tree.insert(170)
tree.insert(15)
tree.insert(1)
JSON.stringify(traverse(tree.root))
// 9
// 4 20
//1 6 15 170
function traverse(node)
const tree = value: node.value ;
tree.left = node.left === null ? null : traverse(node.left);
tree.right = node.right === null ? null : traverse(node.right);
return tree;
class Node
constructor(value)
this.left = null;
this.right = null;
this.value = value;
class BinarySearchTree
constructor()
this.root = null;
insert(value)
var root=this.root
if(!root)
this.root=new Node(value)
else
var currentNode=root;
var newNode=new Node(value)
while(currentNode)
if(value<currentNode.value)
if(!currentNode.left)
currentNode.left=new Node;
break;
else
currentNode=currentNode.left;
else
if(!currentNode.right)
currentNode.right=new Node;
break;
else
currentNode=currentNode.right
lookup(value)
var root=this.root
var searchNode = new Node(value)
if(!this.root)
return null;
if(searchNode===root)
return root
else
if(searchNode.value>root)
return root.right.value
else
return root.left.value
const tree = new BinarySearchTree();
tree.insert(9)
tree.insert(4)
tree.insert(6)
tree.insert(20)
tree.insert(170)
tree.insert(15)
tree.insert(1)
JSON.stringify(traverse(tree.root))
// 9
// 4 20
//1 6 15 170
function traverse(node)
const tree = value: node.value ;
tree.left = node.left === null ? null : traverse(node.left);
tree.right = node.right === null ? null : traverse(node.right);
return tree;
class Node
constructor(value)
this.left = null;
this.right = null;
this.value = value;
class BinarySearchTree
constructor()
this.root = null;
insert(value)
var root=this.root
if(!root)
this.root=new Node(value)
else
var currentNode=root;
var newNode=new Node(value)
while(currentNode)
if(value<currentNode.value)
if(!currentNode.left)
currentNode.left=new Node;
break;
else
currentNode=currentNode.left;
else
if(!currentNode.right)
currentNode.right=new Node;
break;
else
currentNode=currentNode.right
lookup(value)
var root=this.root
var searchNode = new Node(value)
if(!this.root)
return null;
if(searchNode===root)
return root
else
if(searchNode.value>root)
return root.right.value
else
return root.left.value
const tree = new BinarySearchTree();
tree.insert(9)
tree.insert(4)
tree.insert(6)
tree.insert(20)
tree.insert(170)
tree.insert(15)
tree.insert(1)
JSON.stringify(traverse(tree.root))
// 9
// 4 20
//1 6 15 170
function traverse(node)
const tree = value: node.value ;
tree.left = node.left === null ? null : traverse(node.left);
tree.right = node.right === null ? null : traverse(node.right);
return tree;
javascript binary-search-tree
javascript binary-search-tree
edited Mar 8 at 5:49
Jacky
asked Mar 8 at 4:07
JackyJacky
30510
30510
Just filter out all null items...
– Jack Bashford
Mar 8 at 4:11
newNodenotnew Node
– zer00ne
Mar 8 at 4:11
add a comment |
Just filter out all null items...
– Jack Bashford
Mar 8 at 4:11
newNodenotnew Node
– zer00ne
Mar 8 at 4:11
Just filter out all null items...
– Jack Bashford
Mar 8 at 4:11
Just filter out all null items...
– Jack Bashford
Mar 8 at 4:11
newNode not new Node– zer00ne
Mar 8 at 4:11
newNode not new Node– zer00ne
Mar 8 at 4:11
add a comment |
1 Answer
1
active
oldest
votes
Your Node class was instantiated as newNode so change all new Node into newNode *except for the first instance of new Node of course.
var newNode=new Node(value) // Leave this one alone and fix the rest
var searchNode = new Node(value) // Leave this one alone as well
var insertNode = function(node, newNode)
if (newNode.key < node.key)
if (node.left === null)
node.left = newNode;
else
insertNode(node.left, newNode);
else
if (node.right === null)
node.right = newNode;
else
insertNode(node.right, newNode);
;
No problem, that BST looked familiar and then I remembered it was from an ebook I had read 📙
– zer00ne
Mar 8 at 10:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55056585%2fbinarysearchtrees-insert-function-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your Node class was instantiated as newNode so change all new Node into newNode *except for the first instance of new Node of course.
var newNode=new Node(value) // Leave this one alone and fix the rest
var searchNode = new Node(value) // Leave this one alone as well
var insertNode = function(node, newNode)
if (newNode.key < node.key)
if (node.left === null)
node.left = newNode;
else
insertNode(node.left, newNode);
else
if (node.right === null)
node.right = newNode;
else
insertNode(node.right, newNode);
;
No problem, that BST looked familiar and then I remembered it was from an ebook I had read 📙
– zer00ne
Mar 8 at 10:56
add a comment |
Your Node class was instantiated as newNode so change all new Node into newNode *except for the first instance of new Node of course.
var newNode=new Node(value) // Leave this one alone and fix the rest
var searchNode = new Node(value) // Leave this one alone as well
var insertNode = function(node, newNode)
if (newNode.key < node.key)
if (node.left === null)
node.left = newNode;
else
insertNode(node.left, newNode);
else
if (node.right === null)
node.right = newNode;
else
insertNode(node.right, newNode);
;
No problem, that BST looked familiar and then I remembered it was from an ebook I had read 📙
– zer00ne
Mar 8 at 10:56
add a comment |
Your Node class was instantiated as newNode so change all new Node into newNode *except for the first instance of new Node of course.
var newNode=new Node(value) // Leave this one alone and fix the rest
var searchNode = new Node(value) // Leave this one alone as well
var insertNode = function(node, newNode)
if (newNode.key < node.key)
if (node.left === null)
node.left = newNode;
else
insertNode(node.left, newNode);
else
if (node.right === null)
node.right = newNode;
else
insertNode(node.right, newNode);
;
Your Node class was instantiated as newNode so change all new Node into newNode *except for the first instance of new Node of course.
var newNode=new Node(value) // Leave this one alone and fix the rest
var searchNode = new Node(value) // Leave this one alone as well
var insertNode = function(node, newNode)
if (newNode.key < node.key)
if (node.left === null)
node.left = newNode;
else
insertNode(node.left, newNode);
else
if (node.right === null)
node.right = newNode;
else
insertNode(node.right, newNode);
;
var insertNode = function(node, newNode)
if (newNode.key < node.key)
if (node.left === null)
node.left = newNode;
else
insertNode(node.left, newNode);
else
if (node.right === null)
node.right = newNode;
else
insertNode(node.right, newNode);
;var insertNode = function(node, newNode)
if (newNode.key < node.key)
if (node.left === null)
node.left = newNode;
else
insertNode(node.left, newNode);
else
if (node.right === null)
node.right = newNode;
else
insertNode(node.right, newNode);
;answered Mar 8 at 4:16
zer00nezer00ne
25.4k32547
25.4k32547
No problem, that BST looked familiar and then I remembered it was from an ebook I had read 📙
– zer00ne
Mar 8 at 10:56
add a comment |
No problem, that BST looked familiar and then I remembered it was from an ebook I had read 📙
– zer00ne
Mar 8 at 10:56
No problem, that BST looked familiar and then I remembered it was from an ebook I had read 📙
– zer00ne
Mar 8 at 10:56
No problem, that BST looked familiar and then I remembered it was from an ebook I had read 📙
– zer00ne
Mar 8 at 10:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55056585%2fbinarysearchtrees-insert-function-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Just filter out all null items...
– Jack Bashford
Mar 8 at 4:11
newNodenotnew Node– zer00ne
Mar 8 at 4:11