“:=”是指什么?

时间:2014-12-03 06:10:57

标签: binary-search-tree pseudocode breadth-first-search

我正在查看二进制搜索树的广度优先排序算法,并且使用了一个我无法理解的符号。有趣的是,Google的结果为零。

//  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)

这里使用的操作是什么?我对这条线很困惑。

4 个答案:

答案 0 :(得分:1)

:=是伪代码分配。

或ADA。但无论如何,这有点像伪代码。

答案 1 :(得分:1)

您发布的代码是伪代码,并不是有效的C ++。

在C ++中,赋值运算符为=

在其他语言中,如Ada,BCPL,Cecil,Dylan,E,Eiffel,Maple,Mathematica,Modula-3,Pascal,Pliant,Sather,Simula,Smalltalk,SML,赋值运算符为:=
GNU make也使用:=作为分配方式。

由于您发布的代码是注释,因此它不是有效的C ++。

以下是您在有效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(){}

答案 2 :(得分:0)

它在评论中(每个//前缀)...它不是可编译的代码,在C ++中没有任何意义。

答案 3 :(得分:0)

它不是C ++运算符。它在某些语言中使用,例如Pascal,与=赋值的含义相同。

请参阅:Assignment operator (Computer science)

相关问题