为什么打开gdb后无法调试?这是因为我的程序哪里有问题嘛?
以下是我的程序:
#include
#include
#include
#include
using namespace std;
struct BinaryTreeNode{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->m_nValue = value;
pNode->m_pLeft = NULL;
pNode->m_pRight = NULL;
return pNode;
}
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
if(pParent != NULL)
{
pParent->m_pLeft = pLeft;
pParent->m_pRight = pRight;
}
}
void PrintTreeNode(BinaryTreeNode* pNode)
{
if(pNode != NULL)
{
printf("value of this node is: %d\n", pNode->m_nValue);
if(pNode->m_pLeft != NULL)
printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);
else
printf("left child is null.\n");
if(pNode->m_pRight != NULL)
printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);
else
printf("right child is null.\n");
}
else
{
printf("this node is null.\n");
}
printf("\n");
}
void PrintTree(BinaryTreeNode* pRoot)
{
PrintTreeNode(pRoot);
if(pRoot != NULL)
{
if(pRoot->m_pLeft != NULL)
PrintTree(pRoot->m_pLeft);
if(pRoot->m_pRight != NULL)
PrintTree(pRoot->m_pRight);
}
}
void DestroyTree(BinaryTreeNode* pRoot)
{
if(pRoot != NULL)
{
BinaryTreeNode* pLeft = pRoot->m_pLeft;
BinaryTreeNode* pRight = pRoot->m_pRight;
delete pRoot;
pRoot = NULL;
DestroyTree(pLeft);
DestroyTree(pRight);
}
}
void FindPathToLeaf(BinaryTreeNode* pNode,int expectedSum,
vector& ivec, int& currentSum);
void FindPath(BinaryTreeNode* pRoot, int expectedSum)
{
cout<<"Begin in here:";
if(pRoot==NULL)
return;
vector ivec;
int currentSum=0;
FindPathToLeaf(pRoot,expectedSum,ivec,currentSum);
}
void FindPathToLeaf(BinaryTreeNode* pNode,int expectedSum,
vector& ivec, int& currentSum)
{
currentSum+=pNode->m_nValue;
ivec.push_back(pNode->m_nValue);
bool isLeaf=pNode->m_pLeft==NULL&&pNode->m_pRight==NULL;
if(isLeaf&&expectedSum==currentSum)
{
cout<<"A path is:";
for(vector<int>::iterator i=ivec.begin();i<ivec.end();i++)
{
cout<<(*i)<<" ";
}
}
if(pNode->m_pLeft!=NULL)
FindPathToLeaf(pNode->m_pLeft, expectedSum, ivec, currentSum);
if(pNode->m_pRight!=NULL)
FindPathToLeaf(pNode->m_pRight, expectedSum, ivec, currentSum);
currentSum-=pNode->m_nValue;
ivec.pop_back();
}
void Test(char* testName, BinaryTreeNode* pRoot, int expectedSum)
{
if(testName != NULL)
printf("%s begins:\n", testName);
FindPath(pRoot, expectedSum);
printf("\n");
}
// 10
// / \
// 5 12
// /\
// 4 7
// 有两条路径上的结点和为22
void Test1()
{
BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
ConnectTreeNodes(pNode10, pNode5, pNode12);
ConnectTreeNodes(pNode5, pNode4, pNode7);
printf("Two paths should be found in Test1.\n");
char str[]="Test1";
Test(str, pNode10, 22);
DestroyTree(pNode10);
}
int main(int argc,char* argv[])
{
Test1();
return 0;
}