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?













0















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 ?










share|improve this question




























    0















    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 ?










    share|improve this question


























      0












      0








      0








      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 ?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 9 at 20:00









      Andronicus

      5,24421732




      5,24421732










      asked Mar 7 at 21:48









      randomguyrandomguy

      146




      146






















          1 Answer
          1






          active

          oldest

          votes


















          1














          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;







          share|improve this answer

























          • 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










          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
          );



          );













          draft saved

          draft discarded


















          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









          1














          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;







          share|improve this answer

























          • 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















          1














          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;







          share|improve this answer

























          • 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













          1












          1








          1







          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;







          share|improve this answer















          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;








          share|improve this answer














          share|improve this answer



          share|improve this answer








          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

















          • 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



















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229