我正在研究TreeDecomposition,樹中的每個節點可以有多個圖中的頂點。
現在,我試圖找到第一個節點其中包含頂點u從樹的根。在樹中遍歷
int Tree::traversing(Node* node, int u){
//search in current node
int j = node->point_in_bag(u); //this function returns position of vertex u in node if not available -1
if(j != -1){
return node->index;
}
else{
// traverse in its children
for(int k=0; k<node->children.size(); k++){ // children has the node's childs in it
Node* nod = node->children[k];
cout << "nod index is " << nod->index << endl;
traversing(nod,u); // if vertex isn't found in node, traverse again in its children
}
}
}
我已經嘗試像上面那樣,但它沒有返回確切的節點索引。我在哪裏做錯了。
你的縮排和評論是一團糟。實際上,一些顯然應該在註釋中的字符不是,從而導致代碼格式不正確。請努力爲其他人提供可讀代碼進行審閱。 – 2013-03-06 09:26:00
對不起。我會盡力即興創作。 – user322 2013-03-06 09:35:44
這個問題是算法中的一個基本問題。首先嚐試瞭解如何以遞歸或非遞歸方式遍歷二叉樹。 – user1929959 2013-03-06 09:38:10