이진 검색 트리의 너비 우선 정렬 알고리즘을 찾고 있는데 이해할 수없는 기호가 사용되었습니다. 재미있게도 Google은 결과를 0 개 반환합니다.
// 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)
여기서 사용되는 작업은 무엇입니까? 이 줄에 꽤 혼란 스러워요.
게시 한 코드는 의사 코드이며 유효한 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(){}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다