Find a value in a treeIs Java “pass-by-reference” or “pass-by-value”?How do I sort a list of dictionaries by a value of the dictionary?Sort a Map<Key, Value> by valuesHow to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?Java tree data-structure?implement a tree class from ground in javaFind child in non-binary tree (recursively)Flatten Binary Tree --> Singly Linked List (Ruby)How can I walk through this recursive binary tree problem?
Telemetry for feature health
If the only attacker is removed from combat, is a creature still counted as having attacked this turn?
Is there a RAID 0 Equivalent for RAM?
How to make a list of partial sums using forEach
Is there anyway, I can have two passwords for my wi-fi
Grepping string, but include all non-blank lines following each grep match
Proving an identity involving cross products and coplanar vectors
Would this string work as string?
Storage of electrolytic capacitors - how long?
How to leave product feedback on macOS?
Check if object is null and return null
Typing CO_2 easily
In One Punch Man, is King actually weak?
Would a primitive species be able to learn English from reading books alone?
Animation: customize bounce interpolation
Why is the principal energy of an electron lower for excited electrons in a higher energy state?
What in this world is she trying to say?
When and why was runway 07/25 at Kai Tak removed?
I'm just a whisper. Who am I?
Confusion over Hunter with Crossbow Expert and Giant Killer
Can I cause damage to electrical appliances by unplugging them when they are turned on?
Giving feedback to someone without sounding prejudiced
Identifying "long and narrow" polygons in with PostGIS
Limit max CPU usage SQL SERVER with WSRM
Find a value in a tree
Is Java “pass-by-reference” or “pass-by-value”?How do I sort a list of dictionaries by a value of the dictionary?Sort a Map<Key, Value> by valuesHow to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?Java tree data-structure?implement a tree class from ground in javaFind child in non-binary tree (recursively)Flatten Binary Tree --> Singly Linked List (Ruby)How can I walk through this recursive binary tree problem?
I need to create a method that checks if a given integer is present in the tree or not and respectively return true or false.
The tree is not a binary search tree therefore the values of the node are not always smaller on the left.
My constructor is as below:
public class TreeNode {
TreeNode left;
int payload;
TreeNode right;
public TreeNode(int x)
payload = x;
The below method work perfectly:
public boolean find(int x,TreeNode root)
if (root.payload == x)
return true;
if (root.left != null && find(x, root.left))
return true;
if (root.right != null && find(x, root.right))
return true;
return false;
However I realised i need to follow the guide and do it as follow:
public Boolean find(int x)
How can I change my code to implement this version ?
java data-structures tree
add a comment |
I need to create a method that checks if a given integer is present in the tree or not and respectively return true or false.
The tree is not a binary search tree therefore the values of the node are not always smaller on the left.
My constructor is as below:
public class TreeNode {
TreeNode left;
int payload;
TreeNode right;
public TreeNode(int x)
payload = x;
The below method work perfectly:
public boolean find(int x,TreeNode root)
if (root.payload == x)
return true;
if (root.left != null && find(x, root.left))
return true;
if (root.right != null && find(x, root.right))
return true;
return false;
However I realised i need to follow the guide and do it as follow:
public Boolean find(int x)
How can I change my code to implement this version ?
java data-structures tree
add a comment |
I need to create a method that checks if a given integer is present in the tree or not and respectively return true or false.
The tree is not a binary search tree therefore the values of the node are not always smaller on the left.
My constructor is as below:
public class TreeNode {
TreeNode left;
int payload;
TreeNode right;
public TreeNode(int x)
payload = x;
The below method work perfectly:
public boolean find(int x,TreeNode root)
if (root.payload == x)
return true;
if (root.left != null && find(x, root.left))
return true;
if (root.right != null && find(x, root.right))
return true;
return false;
However I realised i need to follow the guide and do it as follow:
public Boolean find(int x)
How can I change my code to implement this version ?
java data-structures tree
I need to create a method that checks if a given integer is present in the tree or not and respectively return true or false.
The tree is not a binary search tree therefore the values of the node are not always smaller on the left.
My constructor is as below:
public class TreeNode {
TreeNode left;
int payload;
TreeNode right;
public TreeNode(int x)
payload = x;
The below method work perfectly:
public boolean find(int x,TreeNode root)
if (root.payload == x)
return true;
if (root.left != null && find(x, root.left))
return true;
if (root.right != null && find(x, root.right))
return true;
return false;
However I realised i need to follow the guide and do it as follow:
public Boolean find(int x)
How can I change my code to implement this version ?
java data-structures tree
java data-structures tree
edited Mar 9 at 20:00
Andronicus
5,24421732
5,24421732
asked Mar 7 at 21:48
randomguyrandomguy
146
146
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Make this an instance method by replacing passing a tree to invoke method on with this:
public Boolean find(int x)
if (this.payload == x)
return true;
if (this.left != null && this.left.find(x))
return true;
if (this.right != null && this.right.find(x))
return true;
return false;
It gives an error: remove argument to match find(int).
– randomguy
Mar 7 at 21:54
As the method takes only 1 argument it says to remove one or add one and match (int, TreeNode)
– randomguy
Mar 7 at 21:55
of course it does, not it should do it
– Andronicus
Mar 7 at 21:55
it works, thanks
– randomguy
Mar 7 at 21:59
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%2f55053332%2ffind-a-value-in-a-tree%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
Make this an instance method by replacing passing a tree to invoke method on with this:
public Boolean find(int x)
if (this.payload == x)
return true;
if (this.left != null && this.left.find(x))
return true;
if (this.right != null && this.right.find(x))
return true;
return false;
It gives an error: remove argument to match find(int).
– randomguy
Mar 7 at 21:54
As the method takes only 1 argument it says to remove one or add one and match (int, TreeNode)
– randomguy
Mar 7 at 21:55
of course it does, not it should do it
– Andronicus
Mar 7 at 21:55
it works, thanks
– randomguy
Mar 7 at 21:59
add a comment |
Make this an instance method by replacing passing a tree to invoke method on with this:
public Boolean find(int x)
if (this.payload == x)
return true;
if (this.left != null && this.left.find(x))
return true;
if (this.right != null && this.right.find(x))
return true;
return false;
It gives an error: remove argument to match find(int).
– randomguy
Mar 7 at 21:54
As the method takes only 1 argument it says to remove one or add one and match (int, TreeNode)
– randomguy
Mar 7 at 21:55
of course it does, not it should do it
– Andronicus
Mar 7 at 21:55
it works, thanks
– randomguy
Mar 7 at 21:59
add a comment |
Make this an instance method by replacing passing a tree to invoke method on with this:
public Boolean find(int x)
if (this.payload == x)
return true;
if (this.left != null && this.left.find(x))
return true;
if (this.right != null && this.right.find(x))
return true;
return false;
Make this an instance method by replacing passing a tree to invoke method on with this:
public Boolean find(int x)
if (this.payload == x)
return true;
if (this.left != null && this.left.find(x))
return true;
if (this.right != null && this.right.find(x))
return true;
return false;
edited Mar 7 at 21:55
answered Mar 7 at 21:50
AndronicusAndronicus
5,24421732
5,24421732
It gives an error: remove argument to match find(int).
– randomguy
Mar 7 at 21:54
As the method takes only 1 argument it says to remove one or add one and match (int, TreeNode)
– randomguy
Mar 7 at 21:55
of course it does, not it should do it
– Andronicus
Mar 7 at 21:55
it works, thanks
– randomguy
Mar 7 at 21:59
add a comment |
It gives an error: remove argument to match find(int).
– randomguy
Mar 7 at 21:54
As the method takes only 1 argument it says to remove one or add one and match (int, TreeNode)
– randomguy
Mar 7 at 21:55
of course it does, not it should do it
– Andronicus
Mar 7 at 21:55
it works, thanks
– randomguy
Mar 7 at 21:59
It gives an error: remove argument to match find(int).
– randomguy
Mar 7 at 21:54
It gives an error: remove argument to match find(int).
– randomguy
Mar 7 at 21:54
As the method takes only 1 argument it says to remove one or add one and match (int, TreeNode)
– randomguy
Mar 7 at 21:55
As the method takes only 1 argument it says to remove one or add one and match (int, TreeNode)
– randomguy
Mar 7 at 21:55
of course it does, not it should do it
– Andronicus
Mar 7 at 21:55
of course it does, not it should do it
– Andronicus
Mar 7 at 21:55
it works, thanks
– randomguy
Mar 7 at 21:59
it works, thanks
– randomguy
Mar 7 at 21:59
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%2f55053332%2ffind-a-value-in-a-tree%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