C++ method based on recursive and non recursive algorithms for binary tree images

  • 2020-05-19 05:21:47
  • OfStack

In this paper, the example of C++ based on recursive and non-recursive algorithm to find a two-tree image. I will share it with you for your reference as follows:


/* o 2 Tree image  --  Recursive and non-recursive methods are used 
 Debugging and running source code and analysis as follows: 
***/
#include <stdlib.h>
#include <iostream>
#include <queue>
using std::cout;
using std::cin;
using std::endl;
using std::queue;
/*2 Fork tree node definition */
typedef struct BTreeNode
{
  char elem;
  struct BTreeNode *pleft;
  struct BTreeNode *pright;
}BTreeNode;
/*
 o 2 Tree image 
 Recursive method steps: 
 if proot for NULL , is an empty tree, and returns; 
 if proot Don't for NULL Exchange, proot The left and right nodes, and then the mirror images of the left and right subtrees; 
*/
/* Recursive o 2 Tree image */
void get_bitree_mirror(BTreeNode* proot)
{
  if (proot == NULL)
    return ;
  BTreeNode* temp_node = proot->pleft;
  proot->pleft = proot->pright;
  proot->pright = temp_node;
  get_bitree_mirror(proot->pleft);
  get_bitree_mirror(proot->pright);
  return ;
}
/*
 The steps of the non-recursive method are as follows: 
 With the help of a queue 
 First, put the root node proot The team; 
 The first 1 Step: when the queue is not empty, get the total number of nodes at the current level, that is, the length of the current queue; The first 2 Step; 
 The first 2 Step: according to the total number of nodes in the current layer, go out of the queue to traverse the nodes. 
     Switch left and right nodes. If left and right nodes exist, join the team. 
     When all the nodes in the current layer have been traversed, traverse 1 Layer, perform the first 1 Step. 
*/
void get_bitree_mirror_leveltraverse(BTreeNode* proot)
{
  if(proot == NULL)
    return ;
  queue <BTreeNode*> que;
  que.push(proot);
  int level_nodes_number = 0;
  while (!que.empty())// Level traversal 
  {
    level_nodes_number = que.size();
    int level_count = 0;
    while (level_count < level_nodes_number)
    {
      ++level_count;
      proot = que.front();
      que.pop();
      // Switch left and right child nodes 
      BTreeNode* temp_node = proot->pleft;
      proot->pleft = proot->pright;
      proot->pright = temp_node;
      if(proot->pleft != NULL)
        que.push(proot->pleft);
      if(proot->pright != NULL)
        que.push(proot->pright);
    }
  }
  return ;
}
/* 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);
  }
}
/* The first sequence traversal */
void pre_order_traverse(BTreeNode* proot)
{
  if(proot == NULL)
    return;
  cout<< proot->elem << " ";
  pre_order_traverse(proot->pleft);
  pre_order_traverse(proot->pright);
  return;
}
int main()
{
  int tree_node_number = 0;
  BTreeNode *bt;
  btree_init(bt);// Initializes the root node 
  pre_crt_tree(bt);// create 2 tree 
  cout << " The sequence traversal output is as follows: " << endl;
  cout << " Before calling the mirror function: " << endl;
  pre_order_traverse(bt);
  cout << endl;
  get_bitree_mirror(bt);
  cout << " After recursively calling the mirror function: " << endl;
  pre_order_traverse(bt);
  cout << endl;
  cout << " After the non-recursive call to the mirror function: " << endl;
  get_bitree_mirror_leveltraverse(bt);
  pre_order_traverse(bt);
  cout << endl;
  system("pause");
  return 0;
}


/*
 Operation results: 
a b c # # # d e # # #
------ So that's the input -----------
------ Here is the output -----------
 The sequence traversal output is as follows: 
 Before calling the mirror function: 
a b c d e
 After recursively calling the mirror function: 
a d e b c
 After the non-recursive call to the mirror function: 
a b c d e
 Please press any key to continue . . .
---------------------------------
 Created for this example 2 Fork tree shape: 
    a
  b    d
c     e
 Call the recursion 2 Fork tree mirror shape: 
   a
d    b
  e    c
 Call the non-recursive search again 2 Fork tree mirror shape (that is, the mirror image of the mirror image) :
    a
  b    d
c     e
*/

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


Related articles: