2013-01-03 54 views
-2

我已经创建了一个基于具有2个指针的Node类的通用树:接下来指向该节点的第一个子节点,bros指向该节点的下一个兄弟。 每个节点有一个容量(int),每个叶子被认为是一个需求单元,目标是说天气与否树木支持需求(意味着树的每个分支都有足够的容量来提供所有的需求)。在普通树中递归

树建筑部分工作得很好,但它似乎有我缺乏供应商和递归函数中的错误。一般说明: isSupplier(节点) - 检查节点是否有足够的容量来支持其分支下的所有树叶。 canDemandBeAnswered(节点) - 应该调用isSupplier的递归函数,用于所有节点以叶子向上的方式开始。

的问题是,经过递归遇到的第一个叶子它卡住未知节点(在身高1零个儿子这是不可能的,因为如果该节点是叶递归不叫!)

希望有人能找到我错过的东西,谢谢!

 // This method checks if this node can supply all of its leafs. 

    bool isSupplier() 
    { 
     if (this->isLeaf()) { return 1;} 
     else 
     { 
      this->num_of_leafs = this->Count_Leafs(); 
      Node* iter = this->next; 
      while ((iter != NULL)) 
      { 
       if (iter->isLeaf() == 0) 
       { this->num_of_leafs += iter->num_of_leafs; } 
       iter = iter->bros; 
      } 
     } 
     if (this->capacity < this->num_of_leafs) { return 0; } 
     else { return 1; } 
    } 

    bool canDemandBeAnswered(Node* root) 
    { 
     cout << "Height: " << root->getHeight() << " , sons: " << root->Count_Sons() << " ,bros: " << root->getNumBros() << " ,leafs: " << root->getNumLeafs() << " \n"; 
     if (root->isLeaf()) { return 1; } 
     Node* iter = root->next; 
     while (iter != NULL) 
     { 
      canDemandBeAnswered(iter); 
      iter->getNextBro(); 
     } 
     return root->isSupplier(); 
    }; 

树创建和递归调用:

Node* s8 = new Node(8); 
Node* s5 = new Node(5); 
Node* s6 = new Node(6); 
for(int i=0; i < 2 ; i++){ 
    s6->addChild(new Node());  
} 

Node* s7 = new Node(7); 
Node* s2 = new Node(2); 
for(int i=0; i < 3 ; i++){ 
    s2->addChild(new Node()); 
} 

Node* s3 = new Node(3); 
Node* s2_2 = new Node(2); 
s2_2->addChild(new Node()); 

Node* s4 = new Node(4); 
for(int i=0; i < 5 ; i++){ 
    s4->addChild(new Node()); 
} 

Node* s1 = new Node(1); 
for(int i=0; i < 2 ; i++){ 
    s1->addChild(new Node());  
} 
Node* s2_3 = new Node(2); 
for(int i=0; i < 4 ; i++){ 
    s2_3->addChild(new Node()); 
} 
Node* s2_4 = new Node(2); 
for(int i=0; i < 3 ; i++){ 
    s2_4->addChild(new Node()); 
} 
s8->addChild(s5); 
s8->addChild(s6);  
s5->addChild(s7); 
s5->addChild(s2); 
s6->addChild(s3); 
s6->addChild(s2_2); 
s7->addChild(s4); 
s7->addChild(s1); 
s3->addChild(s2_3); 
s3->addChild(s2_4); 
cout << "s8 height: " << s8->getHeight() << " , sons: " << s8->Count_Sons() << " , bros: " << s8->getNumBros() << " , num of leaf: " << s8->getNumLeafs() << " \n"; 
cout << "s5 height: " << s5->getHeight() << " , sons: " << s5->Count_Sons() << " , bros: " << s5->getNumBros() << " , num of leaf: " << s5->getNumLeafs() << " \n"; 
cout << "s6 height: " << s6->getHeight() << " , sons: " << s6->Count_Sons() << " , bros: " << s6->getNumBros() << " , num of leaf: " << s6->getNumLeafs() << " \n"; 
cout << "s7 height: " << s7->getHeight() << " , sons: " << s7->Count_Sons() << " , bros: " << s7->getNumBros() << " , num of leaf: " << s7->getNumLeafs() << " \n"; 
cout << "s2 height: " << s2->getHeight() << " , sons: " << s2->Count_Sons() << " , bros: " << s2->getNumBros() << " , num of leaf: " << s2->getNumLeafs() << " \n"; 
cout << "s3 height: " << s3->getHeight() << " , sons: " << s3->Count_Sons() << " , bros: " << s3->getNumBros() << " , num of leaf: " << s3->getNumLeafs() << " \n"; 
cout << "s2_2 height: " << s2_2->getHeight() << " , sons: " << s2_2->Count_Sons() << " , bros: " << s2_2->getNumBros() << " , num of leaf: " << s2_2->getNumLeafs() << " \n"; 
cout << "s4 height: " << s4->getHeight() << " , sons: " << s4->Count_Sons() << " , bros: " << s4->getNumBros() << " , num of leaf: " << s4->getNumLeafs() << " \n"; 
cout << "s1 height: " << s1->getHeight() << " , sons: " << s1->Count_Sons() << " , bros: " << s1->getNumBros() << " , num of leaf: " << s1->getNumLeafs() << " \n"; 
cout << "s2_3 height: " << s2_3->getHeight() << " , sons: " << s2_3->Count_Sons() << " , bros: " << s2_3->getNumBros() << " , num of leaf: " << s2_3->getNumLeafs() << " \n"; 
cout << "s2_4 height: " << s2_4->getHeight() << " , sons: " << s2_4->Count_Sons() << " , bros: " << s2_4->getNumBros() << " , num of leaf: " << s2_4->getNumLeafs() << " \n"; 
bool ans = hw4->canDemandBeAnswered(s8); 

和大结局,我的调试输出: enter image description here

+0

请求其他人为你调试你的代码不是建设性的;)。您应该使用调试器(或添加打印语句)来跟踪代码的进度,并将其与您的预期进行比较。一旦你解决了问题,那么你可以创建一个最小的测试用例。 –

+0

另外,请粘贴您的控制台输出的实际文本,而不是输出的图像! –

+0

您好奥利,我添加了打印评论,并且还添加了输出图像,以及在递归过程中发生错误的位置。 这不是我找不到它,我只是无法解释它。 – Ned

回答

2

你的循环

while (iter != NULL) 
{ 
    canDemandBeAnswered(iter); 
    iter->getNextBro(); 
} 

不会做任何事情因为你永远不会修改iter

我怀疑你的意思是说

iter = iter->getNextBro(); 

你还忽略了递归调用,这可能不是你打算什么的返回值,但它不是完全清楚什么应该做的。

+0

非常感谢你molbdnilo!不能相信我错过了迭代器的事情。 任何想法如何使用递归调用的返回值? 的想法是,如果所有递归调用返回1,则函数返回1,如果一个或多个返回0,则该函数返回0。 – Ned