【问题标题】:Getting all paths from root to leaf nodes获取从根到叶节点的所有路径
【发布时间】:2012-07-29 06:41:17
【问题描述】:
          a
        /    \ 
       a       a
      / \     /  \
     a   c    a   f 
    / \      / \ 
   b   d    e   g

我有一棵看起来像上面的树,由链接结构表示:

   class Node
    {
       Node* leftChild; 
       Node* rightChild; 
       char data;
    }

class Tree
{
   Node* root;
}

我的目标是找到从根到叶节点的所有路径。

我的树遍历算法是这样的:

 void inorder()
  {
    in(root);
  }

  void in(CharNode* currentNode)
  {
    if(currentNode)
      {   
        in(currentNode->leftChild);
        cout << currentNode->data << endl;
        in(currentNode->rightChild);
      }   
  }

当我运行它时,我确信树正在构建,如图所示。我已经测试过了。但是,我无法弄清楚为什么我的树遍历分段错误。

我得到的输出是:

b

Segmentation fault.

我已经在高度较小的树上进行了测试,它确实有效。但由于某种原因,它不适用于高度大于 2 的树。我认为这棵树出了问题,我已经检查并打印了每个父母,左孩子和右孩子,他们打印出来如图所示.所以肯定是遍历算法。

【问题讨论】:

  • 遍历算法对我来说看起来不错。发布一个可编译的示例,我相信很快就会发现错误。
  • 什么是CharNode?你确定你构建的树正确吗?
  • @Donotalo,他确定,但我怀疑他错了。
  • null 赋值给 Node 的构造函数中的指针。
  • 好吧,我的代码有点复杂,我简化了很多例子,但是我可以发布一个可编译的版本。我认为没有人愿意阅读全部内容

标签: c++ data-structures tree binary-tree


【解决方案1】:

在构建树时,请务必在节点上将 leftChild 和 rightChild 初始化为 NULL (0)。这对于叶节点和缺少 leftChild 或 rightChild 的节点至关重要。

class Node
      : leftChild(0)
      , rightChild(0)
      , data(0)
{
   Node* leftChild; 
   Node* rightChild; 
   char data;
}

【讨论】:

  • 就是这样。非常感谢。我不敢相信我错过了这么简单的事情。如果它们未初始化,它们是否不会自动分配给 NULL 指针?
  • 在 C/C++ 中不正确。在 Java 中是正确的,这可能会引起混淆。
【解决方案2】:
/* 
** 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 2018-01-29
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多