Overloading Java function (insert into binary tree)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite
1












I have a binary tree below and it seems to make sense to overload the insert() method. If insert() is called without a Node specified, it will just work with head, otherwise if it receives a Node, it will do the insert in the subtree starting with that Node. This second variant is then called recursively, see below.



The repetition that happens in the code below seems wrong to me (not DRY). Is there a better way to do this or is this a limitation in Java?



public class probBinaryTree 
public Node head;

public probBinaryTree()
this.head = null;


private class Node
int data;
Node left;
Node right;
int size;

public Node(int d)
this.data = d;
this.left = null;
this.right = null;
this.size = 1;



public Node insert(int d)
if (head == null)
head = new Node(d);
return head;

head.size++;
if (head.left == null) return insert(head.left, d);
if (head.right == null) return insert(head.right, d);
if (head.left.size <= head.right.size) return insert(head.left, d);
if (head.left.size > head.right.size) return insert(head.right, d);
return null;


private Node insert(Node n, int d)
if (n == null)
n = new Node(d);
return n;

n.size++;
if (n.left == null) return insert(n.left, d);
if (n.right == null) return insert(n.right, d);
if (n.left.size <= n.right.size) return insert(n.left, d);
if (n.left.size > n.right.size) return insert(n.right, d);
return null;








share|improve this question



























    up vote
    1
    down vote

    favorite
    1












    I have a binary tree below and it seems to make sense to overload the insert() method. If insert() is called without a Node specified, it will just work with head, otherwise if it receives a Node, it will do the insert in the subtree starting with that Node. This second variant is then called recursively, see below.



    The repetition that happens in the code below seems wrong to me (not DRY). Is there a better way to do this or is this a limitation in Java?



    public class probBinaryTree 
    public Node head;

    public probBinaryTree()
    this.head = null;


    private class Node
    int data;
    Node left;
    Node right;
    int size;

    public Node(int d)
    this.data = d;
    this.left = null;
    this.right = null;
    this.size = 1;



    public Node insert(int d)
    if (head == null)
    head = new Node(d);
    return head;

    head.size++;
    if (head.left == null) return insert(head.left, d);
    if (head.right == null) return insert(head.right, d);
    if (head.left.size <= head.right.size) return insert(head.left, d);
    if (head.left.size > head.right.size) return insert(head.right, d);
    return null;


    private Node insert(Node n, int d)
    if (n == null)
    n = new Node(d);
    return n;

    n.size++;
    if (n.left == null) return insert(n.left, d);
    if (n.right == null) return insert(n.right, d);
    if (n.left.size <= n.right.size) return insert(n.left, d);
    if (n.left.size > n.right.size) return insert(n.right, d);
    return null;








    share|improve this question























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I have a binary tree below and it seems to make sense to overload the insert() method. If insert() is called without a Node specified, it will just work with head, otherwise if it receives a Node, it will do the insert in the subtree starting with that Node. This second variant is then called recursively, see below.



      The repetition that happens in the code below seems wrong to me (not DRY). Is there a better way to do this or is this a limitation in Java?



      public class probBinaryTree 
      public Node head;

      public probBinaryTree()
      this.head = null;


      private class Node
      int data;
      Node left;
      Node right;
      int size;

      public Node(int d)
      this.data = d;
      this.left = null;
      this.right = null;
      this.size = 1;



      public Node insert(int d)
      if (head == null)
      head = new Node(d);
      return head;

      head.size++;
      if (head.left == null) return insert(head.left, d);
      if (head.right == null) return insert(head.right, d);
      if (head.left.size <= head.right.size) return insert(head.left, d);
      if (head.left.size > head.right.size) return insert(head.right, d);
      return null;


      private Node insert(Node n, int d)
      if (n == null)
      n = new Node(d);
      return n;

      n.size++;
      if (n.left == null) return insert(n.left, d);
      if (n.right == null) return insert(n.right, d);
      if (n.left.size <= n.right.size) return insert(n.left, d);
      if (n.left.size > n.right.size) return insert(n.right, d);
      return null;








      share|improve this question













      I have a binary tree below and it seems to make sense to overload the insert() method. If insert() is called without a Node specified, it will just work with head, otherwise if it receives a Node, it will do the insert in the subtree starting with that Node. This second variant is then called recursively, see below.



      The repetition that happens in the code below seems wrong to me (not DRY). Is there a better way to do this or is this a limitation in Java?



      public class probBinaryTree 
      public Node head;

      public probBinaryTree()
      this.head = null;


      private class Node
      int data;
      Node left;
      Node right;
      int size;

      public Node(int d)
      this.data = d;
      this.left = null;
      this.right = null;
      this.size = 1;



      public Node insert(int d)
      if (head == null)
      head = new Node(d);
      return head;

      head.size++;
      if (head.left == null) return insert(head.left, d);
      if (head.right == null) return insert(head.right, d);
      if (head.left.size <= head.right.size) return insert(head.left, d);
      if (head.left.size > head.right.size) return insert(head.right, d);
      return null;


      private Node insert(Node n, int d)
      if (n == null)
      n = new Node(d);
      return n;

      n.size++;
      if (n.left == null) return insert(n.left, d);
      if (n.right == null) return insert(n.right, d);
      if (n.left.size <= n.right.size) return insert(n.left, d);
      if (n.left.size > n.right.size) return insert(n.right, d);
      return null;










      share|improve this question












      share|improve this question




      share|improve this question








      edited May 21 at 9:05
























      asked May 21 at 8:12









      Andras

      1184




      1184




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Just have one insert method call the other. In your code, that would look like:



          public final class BinaryTree 

          public Node head;

          public BinaryTree()
          this.head = null;


          private class Node
          private final int data;
          private final Node left;
          private final Node right;
          private int size;

          public Node(final int d)
          this.data = d;
          this.left = null;
          this.right = null;
          this.size = 1;



          public Node insert(final int d)
          if (this.head == null)
          this.head = new Node(d);
          return this.head;


          return this.insert(this.head, d);


          private Node insert(final Node n, final int d)
          if (n == null)
          return new Node(d);


          n.size++;
          if (n.left == null)
          return this.insert(n.left, d);

          if (n.right == null)
          return this.insert(n.right, d);

          if (n.left.size <= n.right.size)
          return this.insert(n.left, d);

          if (n.left.size > n.right.size)
          return this.insert(n.right, d);

          return null;







          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            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: "196"
            ;
            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194850%2foverloading-java-function-insert-into-binary-tree%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            Just have one insert method call the other. In your code, that would look like:



            public final class BinaryTree 

            public Node head;

            public BinaryTree()
            this.head = null;


            private class Node
            private final int data;
            private final Node left;
            private final Node right;
            private int size;

            public Node(final int d)
            this.data = d;
            this.left = null;
            this.right = null;
            this.size = 1;



            public Node insert(final int d)
            if (this.head == null)
            this.head = new Node(d);
            return this.head;


            return this.insert(this.head, d);


            private Node insert(final Node n, final int d)
            if (n == null)
            return new Node(d);


            n.size++;
            if (n.left == null)
            return this.insert(n.left, d);

            if (n.right == null)
            return this.insert(n.right, d);

            if (n.left.size <= n.right.size)
            return this.insert(n.left, d);

            if (n.left.size > n.right.size)
            return this.insert(n.right, d);

            return null;







            share|improve this answer

























              up vote
              1
              down vote













              Just have one insert method call the other. In your code, that would look like:



              public final class BinaryTree 

              public Node head;

              public BinaryTree()
              this.head = null;


              private class Node
              private final int data;
              private final Node left;
              private final Node right;
              private int size;

              public Node(final int d)
              this.data = d;
              this.left = null;
              this.right = null;
              this.size = 1;



              public Node insert(final int d)
              if (this.head == null)
              this.head = new Node(d);
              return this.head;


              return this.insert(this.head, d);


              private Node insert(final Node n, final int d)
              if (n == null)
              return new Node(d);


              n.size++;
              if (n.left == null)
              return this.insert(n.left, d);

              if (n.right == null)
              return this.insert(n.right, d);

              if (n.left.size <= n.right.size)
              return this.insert(n.left, d);

              if (n.left.size > n.right.size)
              return this.insert(n.right, d);

              return null;







              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                Just have one insert method call the other. In your code, that would look like:



                public final class BinaryTree 

                public Node head;

                public BinaryTree()
                this.head = null;


                private class Node
                private final int data;
                private final Node left;
                private final Node right;
                private int size;

                public Node(final int d)
                this.data = d;
                this.left = null;
                this.right = null;
                this.size = 1;



                public Node insert(final int d)
                if (this.head == null)
                this.head = new Node(d);
                return this.head;


                return this.insert(this.head, d);


                private Node insert(final Node n, final int d)
                if (n == null)
                return new Node(d);


                n.size++;
                if (n.left == null)
                return this.insert(n.left, d);

                if (n.right == null)
                return this.insert(n.right, d);

                if (n.left.size <= n.right.size)
                return this.insert(n.left, d);

                if (n.left.size > n.right.size)
                return this.insert(n.right, d);

                return null;







                share|improve this answer













                Just have one insert method call the other. In your code, that would look like:



                public final class BinaryTree 

                public Node head;

                public BinaryTree()
                this.head = null;


                private class Node
                private final int data;
                private final Node left;
                private final Node right;
                private int size;

                public Node(final int d)
                this.data = d;
                this.left = null;
                this.right = null;
                this.size = 1;



                public Node insert(final int d)
                if (this.head == null)
                this.head = new Node(d);
                return this.head;


                return this.insert(this.head, d);


                private Node insert(final Node n, final int d)
                if (n == null)
                return new Node(d);


                n.size++;
                if (n.left == null)
                return this.insert(n.left, d);

                if (n.right == null)
                return this.insert(n.right, d);

                if (n.left.size <= n.right.size)
                return this.insert(n.left, d);

                if (n.left.size > n.right.size)
                return this.insert(n.right, d);

                return null;








                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered May 21 at 11:15









                Eric Stein

                3,703512




                3,703512






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194850%2foverloading-java-function-insert-into-binary-tree%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation