0

I'm looking at the algorithm for breadth-first sorting of a binary search tree, and there is a symbol used that I cannot understand. Funny enough, Google returns zero results.

//  levelorder()
//      q = empty queue
//      q.enqueue(root)
//      while not q.empty do
//          node := q.dequeue()  //Referring to this
//          visit(node)
//          if node.left !=  null then
//                q.enqueue(node.left)
//          if node.right != null then
//                q.enqueue(node.right)

What is the operation being used here? I'm quite confused by this line.

oblitum
  • 11,380
  • 6
  • 54
  • 120
Mock
  • 359
  • 1
  • 3
  • 11
  • 3
    This isn't C++, it's pseudo language for describing algorithms. `:=` means assignment. – M.M Dec 03 '14 at 06:12
  • 4
    You're not confused by the rest of it if you're expecting it to be valid C++ code? – Praetorian Dec 03 '14 at 06:13
  • Sorry, let me clarify. I'm aware it's not valid C++ language, but it's a syntax I haven't seen before in pseudocode. – Mock Dec 03 '14 at 06:16

4 Answers4

1

:= is assignment in pseudocode.

Or ADA. But that's kinda like psuedocode anyway.

PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56
1

The code you posted is pseudo code, and is not intended to be valid C++.

In C++, the assignment operator is =.

In other languages such as Ada, BCPL, Cecil, Dylan, E, Eiffel, Maple, Mathematica, Modula-3, Pascal, Pliant, Sather, Simula, Smalltalk, SML, the assignment operator is :=.
GNU make also uses := for a way of assigning.

Since the code you posted is a comment, it is not intended to be valid C++.

Here is a closer representation of the code you posted in valid C++:

#include <iostream>
#include <string>
#include <queue>  

//A node might look like this:
struct Node{
    Node* left;
    Node* right;
};

//somewhere you have a node and a root
Node* node = new Node;
Node* root = new Node;

//a visit function is called in the pseudo code you posted
void visit(Node* node){
    // ... code ...
    return;
}

//here is what valid C++ that is similar to the pseudo code:
void levelorder(){

    //empty queue
    std::queue<Node*> q;

    //add the root to the queue
    q.push(root);

    do {
        visit(node);
        if (node->left != nullptr){
            q.push(node->left);
        }
        if (node->left != nullptr){
            q.push(node->right);
        }
    }while(!q.empty());

    return;
}

//I just added this main function so the whole code snippet compiles successfully
int main(){}
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • Ah, I see, so it's just a variation of "=" then? Funny that Google returns literally no results considering it's a known convention, but thank you to everyone for clearing that up. – Mock Dec 03 '14 at 06:19
  • @Mock Exactly. It's just not a valid syntax for C++. If you look up a "do while loop" for C++, you'll see that the syntax there is not quite C++ either, but it gets the idea across. – Trevor Hickey Dec 03 '14 at 06:21
  • Google strips out punctuation in searches, so obviously `:=` isn't going to return much. – PaulProgrammer Dec 03 '14 at 06:26
  • Haha I didn't realize that, that'd explain a lot! – Mock Dec 03 '14 at 06:28
  • It's much like Microsoft's index server used to strip away single letter filename extension, so impossible to search for header files. Perhaps it does that still. Don't know, but Google is just doomed to repeat Microsoft's mistakes, technical and other. – Cheers and hth. - Alf Dec 03 '14 at 06:30
0

It's inside a comment (per // prefix)... it's not compilable code and doesn't mean anything in C++.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

It's not a C++ operator. It is used in some languages such as Pascal to mean the same thing as what an assignment = does.

See: Assignment operator (Computer science)

cpx
  • 17,009
  • 20
  • 87
  • 142