Depth-first traversal, breadth-first traversal, and non-recursive traversal of binary trees

Traversal of binary tree:

D: visit the root node, L: traverse the left subtree of the root node, R: traverse the right subtree of the root node.

Given the preorder traversal sequence and inorder traversal sequence of a binary tree, a binary tree can be uniquely determined.

The non-recursive general practice of depth-first traversal of binary trees is to use stacks, and the non-recursive general practice of breadth-first traversal is to use queues.

Depth-first traversal of a binary tree.

1. Recursive algorithm of in-order traversal (LDR):

if the binary tree is empty, the algorithm ends; otherwise:

    in-order traverse the left subtree of the root node;

    visit the root node;

    in-order traverse the right subtree of the root node.

2. Recursive algorithm of preorder traversal (DLR):

if the binary tree is empty, the algorithm ends, otherwise:

    visit the root node;

    preorder traverse the left subtree of the root node;

    preorder traverse the right subtree of the root node.

3. Recursive algorithm of postorder traversal (LRD):

if the binary tree is empty, the algorithm ends, otherwise:

    postorder traverse the left subtree

    of the root node; postorder traverse the right subtree of the root node;

    visit the root node.



Breadth-first traversal of a binary tree.

The breadth-first traversal of the binary tree (level order traversal) is implemented by using a queue, starting from the first level (root node) of the binary tree, and traversing layer by layer from top to bottom; in the same layer, according to the order from left to right. Nodes are accessed one by one.

Visit the nodes of a binary tree in the order from the root node to the leaf node, from the left subtree to the right subtree. Algorithm:

    1 Initialize a queue and put the root node into the queue;

    2 When the queue is not empty, execute steps 3 to 5 in a loop, otherwise execute 6;

    3 Get a node out of the queue and visit the node;

    4 If the left subtree of the node is not empty, then the node is The left subtree of the node is put into the queue;

    5 If the right subtree of the node is not empty, the right subtree of the node is put into the queue;

    6 End.



Non-recursive depth-first traversal of a binary tree.

A stack is the most commonly used structure to implement recursion. A stack is used to record the nodes or subtrees to be traversed for later access. The recursive depth-first traversal can be changed to a non-recursive algorithm.

1. Non-recursive preorder traversal: When encountering a node, visit the node, push the node into the stack, and then descend to traverse its left subtree. After traversing its left subtree, remove the node from the top of the stack, and traverse the right subtree structure of the node according to the address indicated by its right link.

2. Non-recursive in-order traversal: When a node is encountered, push it onto the stack and traverse its left subtree. After traversing the left subtree, remove the node from the top of the stack and visit it, and then traverse the right subtree of the node according to the address indicated by its right link.

3. Non-recursive postorder traversal: encounter a node, push it onto the stack, and traverse its left subtree. After the traversal is over, the node at the top of the stack cannot be accessed immediately, but the right subtree of the node must be traversed according to the address indicated by its right link structure. Only after traversing the right subtree can the node be removed from the top of the stack and accessed. In addition, it is necessary to add a flag to each element in the stack, so that when a node is removed from the top of the stack, the difference is whether to return from the left of the top element of the stack (then continue to traverse the right subtree) or from the right. (the left and right subtrees of the node have traveled). The feature of Left indicates that it has entered the left subtree of the node and will return from the left; the feature of Right indicates that it has entered the right subtree of the node and will return from the right.

4. Concise non-recursive preorder traversal: When encountering a node, visit the node, push the non-empty right node of this node onto the stack, and then descend to traverse its left subtree. After traversing the left subtree, take a node from the top of the stack, and traverse the right subtree structure of the node according to the address indicated by its right link.

-------------------------------------------------- --------------------

    The depth-first search method of the graph is a generalization of the root traversal of the tree. Its basic idea is: starting from a certain vertex v0 of the graph G , visit v0, then select a vertex vi that is adjacent to v0 and has not been visited, and then select a vertex vj that is adjacent to vi and has not been visited from vi to visit, and continue in turn. If all adjacent vertices of the currently visited vertex have been visited, return to the last vertex w in the visited vertex sequence that has an unvisited adjacent vertex, and traverse forward in the same way starting from w , until all vertices in the graph are visited.
    The breadth-first search of a graph is a generalization of the hierarchical traversal of the tree. Its basic idea is to first visit the initial point vi and mark it as visited, and then visit all the unvisited adjacent points vi1 and vi2 of vi. , …, vi t, and mark them as visited, and then visit all the unvisited adjacent points of each vertex in the order of vi1, vi2, …, vi t, and mark them as visited, in turn And so on, until all vertices in the graph that have a path to the initial point vi have been visited.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058420&siteId=291194637