Search code examples
binary-search-treepseudocodebreadth-first-search

What does ":=" refer to?


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.


Solution

  • 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(){}