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

          Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

          Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

          How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page