C++ 获取从根节点到叶节点的所有路径
我有一棵树,看起来像上面,由一个链接结构表示:C++ 获取从根节点到叶节点的所有路径,c++,data-structures,tree,binary-tree,C++,Data Structures,Tree,Binary Tree,我有一棵树,看起来像上面,由一个链接结构表示: a / \ a a / \ / \ a c a f / \ / \ b d e g 我的目标是找到从根节点到叶节点的所有路径 我的树遍历算法如下所示: class Node { Node* leftChild; Node* rightChild;
a
/ \
a a
/ \ / \
a c a f
/ \ / \
b d e g
我的目标是找到从根节点到叶节点的所有路径
我的树遍历算法如下所示:
class Node
{
Node* leftChild;
Node* rightChild;
char data;
}
class Tree
{
Node* root;
}
我已经在高度较小的树上进行了测试,效果良好。但由于某些原因,它不适用于高度大于2的树。我认为这是树出了问题,我仔细检查并打印了每一位家长、左边的孩子和右边的孩子,他们打印出来,如图所示。所以这绝对是遍历算法 在构建树时,请确保在节点上将leftChild和rightChild初始化为NULL(0)。这对于叶节点和缺少leftChild或rightChild的节点至关重要
b
Segmentation fault.
/*
**二叉树问题
**在二叉树中打印所有根到叶路径
*/
#包括
#包括
#定义尺寸20
#定义最大(A,B)A>B?A:B;
typedef结构二进制树
{
int数据;
结构二进制树*左;
结构二进制树*右;
}英国理工学院;
inta[SIZE]={10,12,15,17,8,18,9,3,11,14,2,1,16,10};
int no_of_节点=14;
BST*新节点(整数数据)
{
BST*节点;
节点=(BST*)malloc(sizeof(BST));
如果(!节点)
返回NULL;
节点->数据=数据;
节点->左=空;
节点->右=空;
返回节点;
}
BST*插入(BST*根,整数d,整数l)
{
if(root==NULL)
返回(newNode(d));
其他的
{
如果(ddata)
根->左=插入(根->左,d,++l);
其他的
根->右=插入(根->右,d,+l);
返回(根);
}
}
BST*CreateTree(BST*root1)
{
int i=0;
对于(i=0;ileft);
printf(“%3d”,root1->data);
顺序(root1->right);
}
无效预订单(BST*root1)
{
if(root1==NULL)
返回;
printf(“%3d”,root1->data);
预订单(root1->left);
预订单(root1->右侧);
}
无效打印阵列(整数*阵列,整数长度)
{
静态int路径号=0;
int i;
printf(“\n路径%d->”,++pathNo);
对于(i=0;idata;
pathLen++;
if(root->left==NULL&&root->right==NULL)
{
PrintArr(pathArr,pathLen);
返回;
}
其他的
{
打印r2lpath(root->left,pathArr,pathLen);
打印r2lpath(root->right,pathArr,pathLen);
}
}
int main()
{
int结果=0;
BST*root1=NULL;
int pathArr[SIZE];
root1=CreateTree(root1);
printf(“\n\n--------------------------------------------\n”);
printf(“\n\n优先遍历树:”;
前序(root1);
printf(“\n\n树的非顺序遍历:”);
顺序(root1);
printf(“\n\n--------------------------------------------\n”);
printf(“\n打印路径\n\n”);
printr2lpath(root1,pathArr,0);
printf(“\n\n--------------------------------------------\n”);
getchar();
返回(0);
}
遍历算法在我看来很好。发布一个可编译的示例,我相信会很快找到错误。什么是CharNode
?你确定你正确地构建了树吗?@Donotalo,他肯定,但我怀疑他错了。为节点的构造函数中的指针指定null。我的代码有点复杂,我想对这个例子进行了很多改进,但是我可以发布一个可编译的版本。我没想到有人会想全部阅读它。非常感谢。我不敢相信我错过了这么简单的东西。如果它们没有初始化,它们不是自动分配给空指针吗?在C/C++中不是这样。在Java中是这样,这可能会引起混淆。
b
Segmentation fault.
class Node
: leftChild(0)
, rightChild(0)
, data(0)
{
Node* leftChild;
Node* rightChild;
char data;
}
/*
** Binary Tree Problems
** Printing all Root to Leaf paths in a Binary Tree
*/
# include <stdio.h>
# include <stdlib.h>
# define SIZE 20
# define MAX(A,B) A>B?A:B;
typedef struct BinaryTree
{
int data;
struct BinaryTree *left;
struct BinaryTree *right;
}BST;
int A[SIZE]={10,12,15,17,8,18,9,3,11,14,2,1,16,10};
int no_of_nodes=14;
BST* newNode(int data)
{
BST *node;
node=(BST *)malloc(sizeof(BST));
if(!node)
return NULL;
node->data = data;
node->left=NULL;
node->right=NULL;
return node;
}
BST *Insert(BST *root,int d,int l)
{
if(root==NULL)
return(newNode(d));
else
{
if(d < root->data)
root->left=Insert(root->left,d,++l);
else
root->right=Insert(root->right,d,++l);
return(root);
}
}
BST* CreateTree(BST *root1)
{
int i=0;
for(i=0;i<no_of_nodes;i++)
{
root1=Insert(root1,A[i],1);
}
return(root1);
}
void Inorder(BST *root1)
{
if(root1==NULL)
return;
Inorder(root1->left);
printf(" %3d ", root1->data);
Inorder(root1->right);
}
void Preorder(BST *root1)
{
if(root1==NULL)
return;
printf(" %3d ", root1->data);
Preorder(root1->left);
Preorder(root1->right);
}
void PrintArr(int *arr,int len)
{
static int pathNo=0;
int i;
printf("\nPath %d ->",++pathNo);
for(i=0;i<len;i++)
printf(" %d ",arr[i]);
return;
}
void PrintR2LPaths(BST *root,int pathArr[],int pathLen)
{
if(root==NULL)
return;
pathArr[pathLen]=root->data;
pathLen++;
if(root->left==NULL && root->right==NULL)
{
PrintArr(pathArr,pathLen);
return;
}
else
{
PrintR2LPaths(root->left,pathArr,pathLen);
PrintR2LPaths(root->right,pathArr,pathLen);
}
}
int main()
{
int result=0;
BST *root1=NULL;
int pathArr[SIZE];
root1=CreateTree(root1);
printf("\n\n---------------------------------------------------\n");
printf("\n\nPreorder Traversal of Tree : ");
Preorder(root1);
printf("\n\nInorder Traversal of Tree : ");
Inorder(root1);
printf("\n\n---------------------------------------------------\n");
printf("\nPrinting Paths\n\n");
PrintR2LPaths(root1,pathArr,0);
printf("\n\n---------------------------------------------------\n");
getchar();
return(0);
}