"Reprint" Morris traverse Binary Tree & BST (binary search tree) Traverse & Space O (1) time O (n)

Source: Internet
Author: User

Because do a leetcode topic (front Blog has: link), need to use space O (1) spatial complexity of the sequence to traverse the tree,
Looked at the discuss, also searched the internet, found that the space O (1) can use Morris traversal method. Methods are described below:
Http://www.cnblogs.com/AnnieKim/archive/2013/06/15/MorrisTraversal.html
Among them, the middle sequence traversal and the pre-sequence traversal is convenient to solve:
In general, there are two common methods for implementing a two-fork-tree pre-order (preorder), Middle-order (inorder), post-sequence (postorder) traversal:
One is recursion (recursive),
The second is the iteration version (Stack+iterative) that is implemented with the stack.
Both methods are the spatial complexity of O (n) (the recursion itself occupies the stack space or the user-defined stack). The Morris traversal method can do these two points, unlike the first two methods in which the method requires only O (1) space and can also be completed in O (n) time. To traverse with O (1) space, the biggest difficulty is how to return to the parent node when traversing to the child node (assuming that the node does not have a P pointer pointing to the parent node).
Because the stack cannot be used as a secondary space. To solve this problem,
The Morris method uses the concept of the Clue two fork tree (threaded binary trees) .
In the Morris method, there is no need to assign additional pointers to each node to its predecessor (predecessor) and subsequent nodes (successor).
It is only necessary to use the left and right pointers in the leaf nodes to point to a precursor or successor under some sort of sequential traversal (in the implementation, the pointer always points to the successor node). The original version, Morris only provides the method of the middle sequence traversal, on the basis of the sequential traversal of a slight modification can achieve the preamble, and the follow-up will be a little more attention.
Middle Sequence TraversalThe steps are as follows:
1. If the left child of the current node is empty, the current node is output and its right child is the current node. 2. If the left child of the current node is not empty, the current node in the left subtree of the current node is found in the precursor node under the middle sequence traversal.   a) If the right child of the precursor node is empty, set its right child to the current node. The current node (Cur) is updated to the left child of the current node
Note ( instead of changing the contents of the node, move the cursor cur to the left child ). b) If the right child of the precursor node is the current node, reset its right child to empty ( restore the tree's shape ). Outputs the current node. The current node is updated to the right child of the current node. 3. Repeat the above 1, 2 until the current node (Cur) is empty. The following illustrations are very easy to understand.

In fact, the complexity of time is also O (n). The analysis is as follows:

Space complexity: O (1), because only two auxiliary pointers are used. Time complexity: O (n). Proving that the time complexity is O (n), the biggest doubt is the time complexity of finding the precursor node of all nodes in the two-fork tree under the middle sequence, i.e. the following two lines of code: 1 while (prev->right! = NULL && prev->right!) = cur) 2     prev = prev-> right;
Intuitively, the complexity of it is O (NLGN), as the precursor node of a single node is related to the height of the tree. In fact, it takes only O (n) time to find the precursor node for all nodes.
n nodes of the two fork tree in a total of n-1 edge, the whole process of a maximum of 2 times per edge, one is to locate a node, and another is to find a node above the precursor node,
As shown, where red is to locate a node, the black line is to find the precursor node. So the complexity is O (n).

Pre-order traversal (only slightly different order of output from the current node compared to the middle sequence traversal)
Step: 1. If the left child of the current node is empty, the current node is output and its right child is the current node. 2. If the left child of the current node is not empty, the current node in the left subtree of the current node is found in the precursor node under the middle sequence traversal.   a) If the right child of the precursor node is empty, set its right child to the current node. Output the current node (output here, which is different from the middle sequence traversal only one by one points).
The current node is updated to the left child of the current node. b) If the right child of the precursor node is the current node, reset its right child to null. The current node is updated to the right child of the current node. 3. Repeat the above 1, 2 until the current node is empty.

Post-routing traversal (need to add two small tricks, one is the dump node, the second is the reverse output path)

The subsequent traversal is slightly more complex, requiring a temporary node dump to root the left child. and a sub-process is required to output the various nodes on the path between the two nodes in reverse. Step: The current node is set to temporary node dump. 1. If the left child of the current node is empty, then its right child is the current node. 2. If the left child of the current node is not empty, the current node in the left subtree of the current node is found in the precursor node under the middle sequence traversal.   a) If the right child of the precursor node is empty, set its right child to the current node. The current node is updated to the left child of the current node.   b) If the right child of the precursor node is the current node, reset its right child to null. The reverse output is from the left child of the current node to all nodes on this path of the precursor node .
The current node is updated to the right child of the current node. 3. Repeat the above 1, 2 until the current node is empty.

Analysis of Complexity:

The spatial complexity is also O (1) , and the time complexity is O (n), and the reverse output process simply increases the constant coefficient .

The above three sequential (pre-order, middle-order, post-order) traversal methods can be obtained from the original author's GitHub: GitHub

Summary: Previously only known recursion and Stack + iterations to implement the two-fork tree traversal method, you should now understand the use of O (1) spatial complexity of the method.

Reprint Morris Traversal binary Tree & BST (binary search tree) Traverse & Space O (1) time O (n)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.