Improvement of root first order traversal of binary trees

  • 2020-04-02 02:10:00
  • OfStack

Binary tree features: the maximum degree of each node can not exceed 2, and the left and right subtrees can not be reversed

Binary tree storage structure: the following USES chain storage to elaborate, (link: #) the use of the sequential storage structure of the binary tree, first look at the following structure storage

Sequential storage:



#define  MAX_TREE_SIZE 100
typedef  TElemType  SqBiTree[MAX_TREE_SIZE];

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201401/201419151213718.jpg" >

Chain storage:



typedef struct BiTNode
{
 TElemType data;
 BiTNode  *lchild,*rchild;
}
BiTNode, *BiTree;

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201401/201419151310421.jpg" >

The type of TElemType here is as follows, where I used the definition of int:


#define INT_TYPE
#ifdef INT_TYPE
typedef int TElemType;
#elif defined FLOAT_TYPE
typedef float TElemType
#elif defined STRING_TYPE
typedef char  *TElemType
#endif

Binary tree creation: recursive creation is required here, as shown below


int CreateBiTree(BiTree &T)
{
 int nData;
 printf("Please Enter BiTree Node data:n"); 
 scanf_s("%d", &nData);
 if (nData == 0)
 {
  T = NULL;
  return OK;
 }
 else if (nData > 0 && nData < 100)
 {
  T = (BiTNode*)malloc(sizeof(BiTNode));
  if (!T)
  {
   return BTOVERFLOW;
  }
  T->data = nData;
  CreateBiTree(T->lchild);
  CreateBiTree(T->rchild);
  return OK;
 }
#ifdef _DEBUG
 printf("Enter data Error ! n");
#endif // _DEBUG

 return ERROR;
}

Here, I have limited the data to facilitate testing. Here, only 0 to 100 data is accepted. If it is 0, it indicates that there is no node with children (left child or right child)
Traversing the binary tree: after creating the binary tree, I must test whether the data in the created binary tree is correct



int PreOrderTraverse(BiTree T, int (*VisitNode)(TElemType))
{
 if (T)
 {
  if (VisitNode(T->data))
  {
   if (PreOrderTraverse(T->lchild, VisitNode))
   {
    if (PreOrderTraverse(T->rchild, VisitNode))
    {
     return OK;
    }
   }
  }
  return ERROR;
 }
 return OK; //If there are no children, this is the time to backtrack to see that the right child must be true
}

Here, a function is adopted during the traversal. Notice that the parameter passed is the function pointer, which is just a simple output of node data, as follows:


int VisitNode(TElemType data)
{
#if defined(INT_TYPE) || defined(FLOAT_TYPE)
 printf("%d ", data);
#elif defined(STRING_TYPE)
 printf("%s ", data);
#endif
 return OK;
}

But when traversing, why return OK (1) when T is NULL? Here is mainly the reason for the above traversal function, because here must first traverse the left child and the return value is true, in order to traverse the right child, so can't return ERROR (0), feel the return value is a little strange, modify as follows


int PreOrderTraverseEx(BiTree T, int (*VisitNode)(TElemType))
{
 if (T)
 {
  if (VisitNode(T->data))
  {
   PreOrderTraverse(T->lchild, VisitNode);
   PreOrderTraverse(T->rchild, VisitNode);
   return OK;
  }
 }
 return ERROR; //If there are no children, this is the time to backtrack
}

It's a lot more comfortable to look at, because you don't actually have to use any return value, you don't have to do anything else to basically go through the left and right subtrees, and if there's anything else, you can't do without a return value, and it doesn't really matter whether you return OK or not, because I'm not judging at all
Test case: as follows


 BiTree T;
 if (CreateBiTree(T))
 {
  PreOrderTraverseEx(T, VisitNode);
  printf("n");
 }

There are certain requirements for the data input of the test. If the root is 12 and the child node is 34, 78, the input data should be 12, 34, 0, 0, 78, 0, 0. Only in this way can the test exit normally.
Please Enter BiTree Node data:
12
Please Enter BiTree Node data:
34
Please Enter BiTree Node data:
0
Please Enter BiTree Node data:
0
Please Enter BiTree Node data:
78
Please Enter BiTree Node data:
0
Please Enter BiTree Node data:
0
12, 34, 78

From the front, I can see here is the recursive algorithm, are used as we all know the ills of recursive maximum is in the next layer of each dive, be sure to save the current level of data, specific what data is managed by the operating system OS, but like every parameter T will stack, easy on the levels of exit to return to a layer of use, so it can adopt the way of non-recursive algorithm to modify: how to do?

Please refer to (link: #)


Related articles: