-1

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.

  • 8
    That's not C++, probably Java – Mat Nov 22 '19 at 08:31
  • 4
    You claim this is from a C++ source ? If you "found" it, add the link to your question, because that looks more like C# than C++. – WhozCraig Nov 22 '19 at 08:32
  • Unfortunately I can't add the link, or at least it would do no good, the source is from a class sight from my university which only allows for student access. The class is a data structure class in c++ which is why I assumed that was what this is. The above code was used in talking about the insertion method for a left leaning red black tree but it was supplemental to the professors in class lecture material. He often posts code snippets with things the class isn't familiar w/, maybe he grabbed this from something that wasn't c++. – hapless_automaton Nov 22 '19 at 08:40
  • 2
    I would assume that this is just an inadvertent mistake in the lecture material. If you usually write e.g. Java or C#, you might slip in the wrong syntax in C++ code, which is presumably what happened here. But yes, this is not valid C++. – Max Langhof Nov 22 '19 at 08:42
  • That might explain why I wasn't able to find anything in my search efforts. There are a number of times in which we are presented with things we aren't familiar with like enum, auto, void* so I thought this might a have been one of them. He is also busy with a lot of other stuff, the following isn't the source site but its his for his stuff at UNR https://www.autonomousrobotslab.com/ He's pretty busy so the mistake makes prefect sense. – hapless_automaton Nov 22 '19 at 08:52

1 Answers1

1

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;
 }
}
Piglet
  • 27,501
  • 3
  • 20
  • 43
  • I got this from some supplemental info about red black trees provided by my professor on my colleges site for my class, a data structure class using c++, which is why I had assumed it was c++. My professor is active in his own research and efforts at the college and was likely not paying attention or forgot to mention it was a not a c++ reference. – hapless_automaton Nov 22 '19 at 08:59