Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 递归如何遍历二叉树并计算列表节点的数量?_C_Recursion_Binary Search Tree_Traversal - Fatal编程技术网

C 递归如何遍历二叉树并计算列表节点的数量?

C 递归如何遍历二叉树并计算列表节点的数量?,c,recursion,binary-search-tree,traversal,C,Recursion,Binary Search Tree,Traversal,我无法从以下URL()可视化这些打印和大小函数: 假设您输入了输入序列 4 8 2 7 9 1 3 -1 你会看到一棵树,看起来像这样: 4 / \ / \ 2 8 / \ / \ 1

我无法从以下URL()可视化这些打印和大小函数:


假设您输入了输入序列

4 8 2 7 9 1 3 -1
你会看到一棵树,看起来像这样:

                           4
                          / \
                         /   \
                        2     8
                       / \   / \
                      1   3 7   9
print\u
的执行将如下所示:

print_ascending( node(4) )
{
  node(4) != NULL
  print_ascending( node(4)->left == node(2) )
  {
    node(2) != NULL
    print_ascending( node(2)->left == node(1) )
    {
      node(1) != NULL
      print_ascending( node(1)->left == NULL )
      {
        NULL == NULL
        return
      }
      print( 1 )
      print_ascending( node(1)->right == NULL)
      {
        NULL == NULL
        return
      }
      return
    }
    print( 2 )
    print_ascending( node(2)->right == node(3))
    {
      node(3) != NULL
      print_ascending( node(3)->left == NULL )
      {
        NULL = NULL
        return
      }
      print( 3 )
      print_ascending( node(3)->right == NULL )
      {
        NULL = NULL
        return
      }
    }
    print( 4 )
    print_ascending( node(4)->right == node(8) )
    {
      node(8) != NULL
      print_ascending( node(8)->left == node(7) )
      {
        node(7) != NULL
        print_ascending( node(7)->left == NULL )
        {
          NULL == NULL
          return
        }
        print( 7 )
        print_ascending( node(7)->right == NULL )
        {
          NULL == NULL
          return
        }
        return
      }
      print( 8 )
      print_ascending( node(8)->right == node(9) )
      {
        node(9) != NULL 
        print_ascending( node(9)->left == NULL )
        {
          NULL == NULL
          return
        }
        print( 9 )
        print_ascending( node(9)->right == NULL )
        {
          NULL == NULL
          return
        }
        return
      }
      return
    }
    return
  }
  return
}
return

希望这有助于可视化正在发生的事情,以及
size
函数中正在发生的事情。递归是需要一段时间才能理解的概念之一。

它比简单的链表更复杂:它是一棵树。对于任何给定的节点,函数
print\u ascending()
将跟踪小于
root->value
的项目,打印
root->value
,然后跟踪大于
root->value
的项目。类似地,函数
size()
对小于
root->value
的链接项求和,为
root->value
添加
1
,然后跟随大于
root->value
的项。当链接为
NULL
时,没有可计数或跟随的内容,因此它返回
0
。有什么特别让您感到困惑的地方吗?递归就是把一个问题分解成更小、类似的问题。(例如,一棵树的大小是它的子树加上当前节点的大小之和。)我只是在可视化递归方面有困难:(并且感谢@Weather Vane提到它在BST上,而不是在链表上;链表上的递归似乎对我来说更容易可视化。一个大约有7个节点的简单树的图表(深度3)会值很多字。当你用铅笔和纸看它时,不会有太多的节点让你感到困惑。@Budding_程序员对于初学者来说,程序有未定义的行为,因为指针根没有初始化。@Budding_程序员
size()
函数从根节点开始,它询问左边的节点在那一边有多少个节点。所以节点2说,等一下,我必须问我左边和右边的人他们链接到了多少个节点。每个人都会回答
1
,如果你按照它,节点2会从左边返回
1
,加上
1
,用于self plus
1
从其右侧
=3
到根节点。因此,根节点现在向其右侧的节点询问有多少个这样的节点,并通过相同的过程获得答案
3
。最后,根节点从其左侧报告
3
,再加上
1
从右侧报告self plus
=7
。非常感谢!这帮了大忙。
print_ascending( node(4) )
{
  node(4) != NULL
  print_ascending( node(4)->left == node(2) )
  {
    node(2) != NULL
    print_ascending( node(2)->left == node(1) )
    {
      node(1) != NULL
      print_ascending( node(1)->left == NULL )
      {
        NULL == NULL
        return
      }
      print( 1 )
      print_ascending( node(1)->right == NULL)
      {
        NULL == NULL
        return
      }
      return
    }
    print( 2 )
    print_ascending( node(2)->right == node(3))
    {
      node(3) != NULL
      print_ascending( node(3)->left == NULL )
      {
        NULL = NULL
        return
      }
      print( 3 )
      print_ascending( node(3)->right == NULL )
      {
        NULL = NULL
        return
      }
    }
    print( 4 )
    print_ascending( node(4)->right == node(8) )
    {
      node(8) != NULL
      print_ascending( node(8)->left == node(7) )
      {
        node(7) != NULL
        print_ascending( node(7)->left == NULL )
        {
          NULL == NULL
          return
        }
        print( 7 )
        print_ascending( node(7)->right == NULL )
        {
          NULL == NULL
          return
        }
        return
      }
      print( 8 )
      print_ascending( node(8)->right == node(9) )
      {
        node(9) != NULL 
        print_ascending( node(9)->left == NULL )
        {
          NULL == NULL
          return
        }
        print( 9 )
        print_ascending( node(9)->right == NULL )
        {
          NULL == NULL
          return
        }
        return
      }
      return
    }
    return
  }
  return
}
return