Non-Recursive Algorithms for first-order traversal tree in Java
Source: Internet
Author: User
In C language, we implement non-recursion of the first-order traversal tree. AlgorithmThis is often the case:
Void Prev (node * root)
{Node * P, * node [Max];
Int Top = 0; P = root;
Do
{While (P! = NULL)
{Printf ("% d,", root-> data );
Node [Top] = P; top ++;
P = p-> LCH;
}
If (top> 0)
{Top--; P = node [Top]; P = p-> RCH ;}
} While (top> 0 | P! = NULL );
}
The basic principle is to save the information of each node in the node array during the traversal, so as to implement backtracking after the left subtree is edited, you can use the node information in the node array to obtain information about the right subtree.
This section ProgramWe can almost completely migrate to Java for use, but we can change it and use the java stack class to simplify the operation.
Stack todo = new stack ();
Todo. Push (Root );
While (hasnext ())
{
Object old = todo. Pop ();
If (! Old. Right (). isempty () Todo. Push (old. Right ());
If (! Old. Left (). isempty () Todo. Push (old. Left ());
Return old;
}
For ease of interpretation, some adopt pseudo CodeThe key lies in the two lines of red. The requirement is to first let the right side into the stack, and then let the left side into the stack, so that access
This method has a small amount of code. Because the JVM encapsulates the stack function, it is very functional, its space allocation, space movement
State scaling and data movement in the space are completely done by JVM. If the above C code needs to consider some space problems, it is estimated that the amount of code is better than that of Java
I wrote a lot more.
However, we must note that the Stack class inherits from the vector. Source codeYou should know that the vector itself is composed
Array writing, that is, the real implementation method is no different from the above C code. I often hear some people say: Java programmers do not need
Focus on the data structure, but on the architecture and mode. This is actually not true. If you use the data structure such as stack in a program
Without considering its use time and efficiency, it seems that you have made a great deal, but the efficiency of the program is very low, especially the tree structure, your efficiency will become
Reduction of geometric series.
Therefore, from a certain perspective, Java programmers are facing greater challenges before the data structure. They must understand the principles of the underlying data structure and be good at organizing and planning during use. the data structure in JVM is indeed very useful, but it also brings us trouble. We need to understand them in essence and not be blinded by their gorgeous appearance.
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