C++ USES recursive and non recursive algorithms to calculate the number of binary tree leaves

  • 2020-05-19 05:20:52
  • OfStack

In this paper, the example of C++ using recursive and non-recursive algorithm to achieve the number of two-fork tree leaves node calculation method. I will share it with you for your reference as follows:


/* o 2 The number of tree leaves  --  Recursive and non-recursive methods are used 
 Debugging and running source code and analysis as follows: 
***/
#include <stdlib.h>
#include <iostream>
#include <stack>
using std::cout;
using std::cin;
using std::endl;
using std::stack;
/*2 Fork tree node definition */
typedef struct BTreeNode
{
  char elem;
  struct BTreeNode *pleft;
  struct BTreeNode *pright;
}BTreeNode;
/*
 o 2 Number of tree leaf nodes 
 Leaf node: a node that has no left or right subtree 
 Recursive method steps: 
 If I give you a node proot for NULL , is an empty tree, and the leaf node is 0 To return to 0 ; 
 If I give you a node proot The left and right subtrees are both NULL , is the leaf node, and the number of leaf nodes is 1 To return to 1 ; 
 If I give you a node proot Not both left and right subtrees NULL , is not a leaf node proot Is the number of subtree leaf nodes of the root node =proot Number of leaf nodes of the left subtree +proot Number of leaf nodes of the right subtree. 
/* Recursion to find the number of leaf nodes */
int get_leaf_number(BTreeNode *proot)
{
  if(proot == NULL)
    return 0;
  if(proot->pleft == NULL && proot->pright == NULL)
    return 1;
  return (get_leaf_number(proot->pleft) + get_leaf_number(proot->pright));
}
/* Non-recursive: this example USES sequential traversal 
 Determine whether the currently accessed node is a leaf node, and then sum up the leaf nodes. 
 **/
int preorder_get_leaf_number(BTreeNode* proot)
{
  if(proot == NULL)
    return 0;
  int num = 0;
  stack <BTreeNode*> st;
  while (proot != NULL || !st.empty())
  {
    while (proot != NULL)
    {
      cout << " Nodes: " << proot->elem << endl;
      st.push(proot);
      proot = proot->pleft;
    }
    if (!st.empty())
    {
      proot = st.top();
      st.pop();
      if(proot->pleft == NULL && proot->pright == NULL)
        num++;
      proot = proot -> pright;
    }
  }
  return num;
}
/* Initialize the 2 Forked root node */
BTreeNode* btree_init(BTreeNode* &bt)
{
  bt = NULL;
  return bt;
}
/* Create the first sequence 2 tree */
void pre_crt_tree(BTreeNode* &bt)
{
  char ch;
  cin >> ch;
  if (ch == '#')
  {
    bt = NULL;
  }
  else
  {
    bt = new BTreeNode;
    bt->elem = ch;
    pre_crt_tree(bt->pleft);
    pre_crt_tree(bt->pright);
  }
}
int main()
{
  int tree_leaf_number = 0;
  BTreeNode *bt;
  btree_init(bt);// Initializes the root node 
  pre_crt_tree(bt);// create 2 tree 
  tree_leaf_number = get_leaf_number(bt);// recursive 
  cout << "2 The number of leaf nodes is :" << tree_leaf_number << endl;
  cout << " The non-recursive first order traversal process is as follows: " << endl;
  tree_leaf_number = preorder_get_leaf_number(bt);// non-recursive 
  cout << "2 The number of leaf nodes is :" << tree_leaf_number << endl;
  system("pause");
  return 0;
}
/*

 Operation results: 
a b c # # # d e # # f # #
--- So that's the input ---
--- Here is the output ---
2 The number of leaf nodes is :3
 The non-recursive traversal process is as follows: 
 Nodes: a
 Nodes: b
 Nodes: c
 Nodes: d
 Nodes: e
 Nodes: f
2 The number of leaf nodes is :3
 Please press any key to continue . . .

 Created for this example 2 Fork tree shape: 
    a
  b    d  
c     e  f
*/

I hope this article is helpful to you C++ programming.


Related articles: