## Traversal of binary tree (front/middle/back/level, recursive & non-recursive)

#### 1. Preorder traversal

##### recursion
• Returns empty if the binary tree is empty
• Traverse the root node, left subtree, right subtree in turn
``````void PreOrder(PBTNode pRoot)
{
if (NULL == pRoot)
return;
else
{
printf("%c", pRoot->_data);
PreOrder(pRoot->pLeft);
PreOrder(pRoot->pRight);
}
}``````
##### non-recursive
• If the binary tree is empty, return directly
• Using the stack structure, first push the root node into the stack, then save the root node, let the root node pop out of the stack, and then judge whether the root node has a left subtree and a right subtree, and push it into the stack if there is. Loop in turn, when the stack is empty, the traversal ends.
• Don't forget to destroy the stack
``````void PreOrderNor(PBTNode pRoot)
{
PBTNode pCur = NULL;
Stack s;
StackInit(&s);

if (NULL == pRoot)
return;

StackPush(&s, pRoot);

while (!StackEmpty(&s))
{
pCur = StackTop(&s);
StackPop(&s);
printf("%c", pCur->_data);

if (pCur->pRight)
StackPush(&s, pCur->pRight);
if (pCur->pLeft)
StackPush(&s, pCur->pLeft);
}

StackDestory(&s);
}``````

#### 2. In-order traversal

##### recursion
• Returns empty if the binary tree is empty
• Traverse the left subtree, the root node, and the right subtree in turn
``````void InOrder(PBTNode pRoot)
{
if (NULL == pRoot)
return;
else
{
InOrder(pRoot->pLeft);
printf("%c", pRoot->_data);
InOrder(pRoot->pRight);
}
}``````
##### non-recursive
• Returns if the binary tree is empty;
• Access the left subtree of the cyclic binary tree, if it exists, push it into the stack; if it does not exist, print the root node, pop the node from the stack, and visit the right subtree of the node
``````void InOrderNor(PBTNode pRoot)
{
Stack s;
PBTNode pCur = NULL;
if (NULL == pRoot)
return;

StackInit(&s);
pCur = pRoot;

while (!StackEmpty(&s)||pCur)
{
while (pCur)
{
StackPush(&s, pCur);
pCur = pCur->pLeft;
}

pCur = StackTop(&s);
printf("%c", pCur->_data);
StackPop(&s);

pCur = pCur->pRight;
}
}``````

#### 3. Post-order traversal

##### recursion
• Returns empty if the binary tree is empty
• Traverse the left subtree, right subtree, and root node in turn
``````void PostOrder(PBTNode pRoot)
{
if (NULL == pRoot)
return;
else
{
PostOrder(pRoot->pLeft);
PostOrder(pRoot->pRight);
printf("%c", pRoot->_data);
}
}``````
##### non-recursive
• Returns if the binary tree is empty
• Determine whether the node has left and right subtrees, if not, print it. and pop the node
• If a tree has a right subtree, save its right subtree, and judge for the second time whether the node has been visited. If it has been visited, it will not be visited.
``````void PostOrderNor(PBTNode pRoot)
{
Stack s;
PBTNode pCur = NULL;
PBTNode pPre = NULL;
PBTNode pTop = NULL;

if (NULL == pRoot)
return;
StackInit(&s);
pCur = pRoot;
while (!StackEmpty(&s) || pCur)
{
while (pCur)
{
StackPush(&s, pCur);
pCur = pCur->pLeft;

}

pTop = StackTop(&s);
if (NULL == pTop->pRight || pPre == pTop->pRight)
{
printf("%c", pTop->_data);
pPre = pTop;
StackPop(&s);
}
else
{
pCur = pTop->pRight;
}
}
}``````

#### 4. Layer order traversal

• Returns if the binary tree is empty
• Queue the pointer of the root node of the binary tree
• If the queue is not empty, dequeue the head element in the queue, then put the left child pointer (if any) of the head element into the queue, and then put the right child pointer (if any) of the head element into the queue, repeat this process process until there are no elements in the queue
``````void LevelOrder(PBTNode pRoot)
{
Queue q;
PBTNode pCur = NULL;

if (NULL == pRoot)
return;

QueueInit(&q);
QueuePush(&q, pRoot);

while (!QueueEmpty(&q))
{
pCur = QueueFrontData(&q);
QueuePop(&q);
printf("%c", pCur->_data);

if (pCur->pLeft)
QueuePush(&q, pCur->pLeft);
if (pCur->pRight)
QueuePush(&q, pCur->pRight);
}

QueueDestory(&q);
}``````

### Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326833820&siteId=291194637
Recommended
Ranking
Daily