Three traversal methods of binary tree (non recursive)

Binary tree is a kind of data structure. The structure of simple binary tree can be divided into left sub node, right sub node, and a pointer to data...
Preorder traversal
Sequential traversal
Post order traversal

Binary tree is a kind of data structure. The structure of simple binary tree can be divided into left sub node, right sub node, and a pointer to data domain. The basic traversal methods of binary tree can be divided into four types, namely, pre order traversal, middle order traversal, post order traversal and sequence traversal. Here, the first three traversal methods are explained.
We take a binary tree as an example to explain.

The tree uses the following structure:

typedef struct treenode { int val; struct treenode *left; struct treenode *right; }TreeNode;

Preorder traversal

The basic rule of preorder traversal can be seen as root node - > left child node - > right child node, and then recursively use the rule to achieve traversal, which can be understood as dividing the tree into multiple sub trees, until the sub tree divided has no child nodes.
If shown

  1. First, the root node A of the whole number
  2. Root out the principle of traversal, and then access the left child B of the root node
  3. Because this rule is used recursively, node B still follows root node - > left child node - > right child node, so B - > d
  4. For D, we continue to use this rule, but because it is a leaf node and has no child nodes, we must root out the recursion principle and return the rule to node B
  5. Node B accesses its right node E
  6. For E, it is also A leaf node, so it returns to the rule of B, but the traversal of B ends and returns to node A
  7. Node A accesses its right node C
  8. Node C accesses left child node F
  9. The child node F is the leaf node and returns to the rule of C, while C has no right node so it returns the rule of A. at this time, the traversal is finished.
    According to the above traversal process, it is determined that the traversal order is a - > b - > D - > e - > C - > F
    code implementation
    Using non recursive recursive implementation
    It mainly uses the idea of a stack to build a recursive data storage method
vector<int> PreOrderTraverse(TreeNode *root) { vector<int> res; if (root == nullptr) { return res; } stack<TreeNode*>s1; TreeNode *head = root; while(!s1.empty()||head)//If several points are not empty or stack container is not empty, the recursion is not completed { if (head)//If the node is not empty, add it to the container { res.push_back(head->val); s1.push(head); head = head->left; } else//If the node is empty, the first node in the container is the node of the previous operation { head = s1.top(); s1.pop(); head = head->right; } } return res; }

Sequential traversal

The basic rule of middle order traversal can be seen as left child node - > root node - > right child node
That is to say, start from the root node and traverse the left child node in turn, then access the parent node, and then the right child node of the parent node
According to this rule, the order of traversal is: D - > b - > e - > A - > F - > C
Using non recursive recursive implementation
The principle of implementation is similar to that of preorder traversal, except that the preorder accesses the data domain first, and the middle order accesses the data domain later

vector<int> inOrderTraverse(TreeNode *root) { vector<int> res; if (root == nullptr) { return res; } stack<TreeNode*>s1; TreeNode *head = root; while (!s1.empty() || head) { if (head) { s1.push(head); head = head->left; } else { head = s1.top(); res.push_back(head->val);//Different from previous traversal, access data when fetching from stack area s1.pop(); head = head->right; } } return res; }

Post order traversal

The basic rule of middle order traversal can be seen as left child node, right child node and root node, so the root node is in the last bit
According to this rule, the order of traversal is: D - > e - > b - > F - > C - > A
Using non recursive recursive implementation
The non recursion of the subsequent traversal needs two stacks to assist the implementation. First, put the node on stack 1, and then take out the node from the top of the stack and put it on stack 2. In this way, the first node will pop up at the back. For the node, put the left sub node to stack 1, and then the right sub node to stack 1. Because the node needs to be taken out and put on stack 2, the right sub node will be taken out and put on stack 2 first , then the left sub node, so for stack 2, the left sub node is above the right sub node, so the left sub node will come out first, then the right sub node, and finally the root node.

vector<int> postOrderTraverse(TreeNode *root) { vector<int> res; if (!root) { return res; } stack<TreeNode*>s1; stack<TreeNode*>s2; s1.push(root); while (!s1.empty()) { TreeNode *temp = s1.top(); s1.pop(); s2.push(temp);//Put the top node of s1 into s2 if (temp->left)//Place left and right children in order s1.push(temp->left); if (temp->right) s1.push(temp->right); } while (!s2.empty())//After the sequence is completed, take the data { res.push_back(s2.top()->val); s2.pop(); } return res; }

I programming a small white, if there are errors welcome to correct!!

Program ape developing Published 13 original articles, won praise 4, visited 147 Private letter follow

11 January 2020, 05:18 | Views: 7037

Add new comment

For adding a comment, please log in
or create account

0 comments