Non-recursive traversal method of binary tree in C++ 3-2

4 Code to implement non-recursive traversal of binary tree

As mentioned in "2 Binary Tree Traversal Methods", the binary tree traversal methods include pre-order traversal, in-order traversal, and post-order traversal, which belong to depth-first traversal. Next, take preorder traversal as an example, and implement the non-recursive binary tree traversal of this method through code.

4.1 Preorder traversal

4.1.1 Non-recursive steps of preorder traversal

Because the output order of the preorder traversal is the root node, the left subtree, and the right subtree, so the binary tree shown in Figure 1 is traversed in a preorder manner, and the output order should be "1->2->4->5- >3->6".

The non-recursive flow chart of preorder traversal is shown in Figure 2.

Figure 2 Non-recursive flowchart of preorder traversal

Starting from the root node (the node whose value is 1) in Figure 1, according to the flowchart in Figure 2, the steps shown in Figure 3 can be obtained.

Figure 3 Non-recursive step diagram of preorder traversal

It can be seen from the step diagram in Figure 3 that the output data is "1 2 3 5 3 6", that is, the preorder traversal.

4.1.2 Non-recursive implementation of preorder traversal

The non-recursive code for preorder traversal is shown below.

void preOrderTraveralWithStack(TreeNode* root)

{

    stack<TreeNode*> stk;

    TreeNode* treeNode = root;

    while (treeNode != NULL || !stk.empty())

    {

        while (treeNode != NULL)

        {

            cout << treeNode->data<<endl;

            stk.push(treeNode);

            treeNode = treeNode->leftChild;

        }

        if (!stk.empty())

        {

            treeNode = (TreeNode*)stk.top();

            treeNode = treeNode->rightChild;

            stk.pop();

        }

    }

}

Wherein, the parameter root of the preOrderTraveralWithStackstk() function is the starting node, that is, the root node. In this function, stk is the stack of the custom structure TreeNode, and the above code can be understood in combination with Figure 1, Figure 2 and Figure 3.

In the main function, after using the code mentioned in "3.3 Associated Nodes" to create nodes and associated nodes, call the preOrderTraveralWithStackstk() function to achieve preorder traversal.

preOrderTraveralWithStack(treenode1);

Among them, treenode1 is the root node in Figure 1. The result of the code output is shown in Figure 4.

Figure 4 Preorder traversal code output

4.2 Inorder traversal

The output order of inorder traversal is left subtree, root node, right subtree. Therefore, for the node graph shown in Figure 1, the output result of the in-order traversal is "4->2->5->1->3->6". The non-recursive flowchart of preorder traversal is shown in Figure 5.

Figure 5 Non-recursive flowchart of inorder traversal

The code for inorder traversal implementation is shown below.

void inOrderTraveralWithStack(TreeNode* root)

{

    stack<TreeNode*> stk;

    TreeNode* treeNode = root;

    while (treeNode != NULL || !stk.empty())

    {

        while (treeNode != NULL)

        {

            stk.push(treeNode);

            treeNode = treeNode->leftChild;

        }

        if (!stk.empty())

        {

            treeNode = (TreeNode*)stk.top();

            cout << treeNode->data << endl;

            treeNode = treeNode->rightChild;

            stk.pop();

        }

    }

}

The code output of the in-order traversal is shown in Figure 6.

Figure 6 Inorder traversal code output

4.3 Post-order traversal

The output order of inorder traversal is left subtree, right subtree, root node. Therefore, for the node graph shown in Figure 1, the output result of the in-order traversal is "4->5->2->6->3->1". The non-recursive flow chart of preorder traversal is shown in Figure 7.

Figure 7 Non-recursive flow chart of post-order traversal

The code for post-order traversal implementation is as follows.

void postOrderTraveralWithStack(TreeNode* root)

{

    stack<TreeNode*> stk;

    TreeNode* treeNode = root;

    TreeNode* lastVisit = nullptr;

    while (treeNode != nullptr || !stk.empty())

    {

        while (treeNode != nullptr)

        {

            stk.push(treeNode);

            treeNode = treeNode->leftChild;

        }

        if (!stk.empty())

        {

            treeNode = (TreeNode*)stk.top();

            stk.pop();

            if (treeNode->rightChild == nullptr || treeNode == lastVisit || treeNode->rightChild == lastVisit)

            {

                cout << treeNode->data << endl;

                lastVisit = treeNode;

                treeNode = nullptr;

            }

            else

            {

                stk.push(treeNode);

                treeNode = treeNode->rightChild;

            }           

        }

    }

}

The code output of the post-order traversal is shown in Figure 8.

 Figure 8 post-order traversal code output

Guess you like

Origin blog.csdn.net/hou09tian/article/details/128297156