c++functionreturn-typered-black-treered-black-tree-insertion

What does private in the scope of the return value of a function definition/implementation mean (c++)?


So I'm looking over some code I found in relation to a project I'm working on for school and I found a function implementation that has private before the return value and I was hoping someone could explain its purpose and use to me. I haven't been able to find anything about it online, possibly because I'm not entirely sure how to pose the question without being redirected to info about private in class definitions or basic function definitions.

private Node insert(Node h, Key key, Value val)
{
   if(h == null)
      return new Node(key, val, RED);

   if(isRed(h.left) && isRed(h.right))
      colorFlip(h);

   int cmp = key.compateTo(h.key);
   if(cmp == 0) h.val = val;
   else if(cmp < 0)
      h.left = insert(h.left, key, val);
   else
      h.right = insert(h.right, key, val);

   if(isRed(h.right))
      h = rotateLeft(h);

   if(isRed(h.left) && isRed(h.left.left))
      h = rotateRight(h);

   return h;
}

This is in regards to left-leaning-red-black-trees. Thanks in advance.


Solution

  • I just googled your code and found it in https://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf Page 5

    This is java and the code is part of a class implementation. So private just declares this method as private, meaning that this method can only be called from within the class.

    See What is the difference between public, protected, package-private and private in Java?

    I'm not sure how your document looks like but this document clearly states that the implementation is provided in Java.

    private class Node
    {
      private Key key;
      private Value val;
      private Node left, right;
      private boolean color;
      Node(Key key, Value val)
      {
       this.key = key;
       this.val = val;
       this.color = RED;
      }
     }
     public Value search(Key key)
     {
       Node x = root;
       while (x != null)
       {
        int cmp = key.compareTo(x.key);
        if (cmp == 0) return x.val;
        else if (cmp < 0) x = x.left;
        else if (cmp > 0) x = x.right;
       }
       return null;
     }
     public void insert(Key key, Value value)
     {
       root = insert(root, key, value);
       root.color = BLACK;
     }
     private Node insert(Node h, Key key, Value value)
     {
       if (h == null) return new Node(key, value);
       if (isRed(h.left) && isRed(h.right)) colorFlip(h);
       int cmp = key.compareTo(h.key);
       if (cmp == 0) h.val = value;
       else if (cmp < 0) h.left = insert(h.left, key, value);
       else h.right = insert(h.right, key, value);
       if (isRed(h.right) && !isRed(h.left)) h = rotateLeft(h);
       if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h);
       return h;
     }
    }