Binary tree recursive traversal can be written by colleagues who have learned data structures. However, during the application process, we often encounter writing a binary tree non-recursive traversal function, next, I wrote the previous article about the non-recursive traversal of Binary trees, which is difficult and easy to use.
First run the Code:
# Include "binarytree. H "# include <stack> # include <queue> # ifndef recu # warning (" recu is not defined ")/*** forward traversal (left and right of the root) ** 1. The current node is not empty. Access the current node and press its right sub-node. Consider its left sub-node * 2. The current node is null, output stack ** @ Param t to visit * @ Param visit point to a func */void pre_order (link T, void (* visit) (Link) {STD :: stack <link> mystack; while (T |! Mystack. empty () {If (t) {visit (t); mystack. push (t-> rchild); t = T-> lchild;} else {T = mystack. top (); mystack. pop () ;}}/ *** center-order traversal (left root and right) ** 1. The current node is not empty. before accessing the current node, you must first access its left subnode, * press the current node of the stack to determine its left sub-node and keep pressing the left sub-node of the stack. * 2. If the current node is null, the left sub-node accesses the stack earlier than the current node, * The current node is the role of the parent node of the right node, considering its right node **, role conversion is very important during traversal ** @ Param t to visit * @ Param visit point to a func */void in_order (link t, void (* visit) (Link) {STD: Stack <link> mystack; while (T |! Mystack. empty () {If (t) {mystack. push (t); t = T-> lchild;} else {T = mystack. top (); mystack. pop (); visit (t); t = T-> rchild ;}}/ *** post-order traversal (left and right root) ** 1. Because the left and right subtree of the current tree should be accessed when accessing the root node, * Add the right subtree to the stack, and then consider the left subtree. repeat this process until a left subtree is empty. * 2. If the current subtree is empty, * 1. if the top of the stack is not empty, it indicates that the right subtree of the tree corresponding to the top of the Second stack is not processed. * the top of the stack pops up and is processed in the next loop, and put an empty pointer into the stack to indicate that another sub-tree has been processed; * 2. if the top of the stack is also an empty tree, it indicates that the Left and Right sub-trees corresponding to the top of the Second stack are either empty or processed. * directly access the node at the top of the Second stack, after the access point, if the stack is still not empty, it indicates that the entire tree has not been traversed. * the top of the stack is displayed. If the stack is merged into the stack, an empty pointer indicates the Second stack. One of the top subtree has been processed. ** @ Param t to visit * @ Param visit point to a func */void post_order (link T, void (* visit) (Link) {STD :: stack <link> mystack; while (1) {If (t) {mystack. push (t); mystack. push (t-> rchild); t = T-> lchild;} else {T = mystack. top (); mystack. pop (); If (! T) {T = mystack. top (); mystack. pop (); visit (t); If (mystack. empty () break; t = mystack. top (); mystack. pop ();} mystack. push (null) ;}}# endif/*** layer traversal ** @ Param t to visit * @ Param visit point to a func */void level_order (link t, void (* visit) (Link) {STD: queue <link> myqueue; If (t) {myqueue. push (t); While (! Myqueue. Empty () {link TMP = myqueue. Front (); myqueue. Pop (); visit (TMP); If (TMP-> lchild! = NULL) myqueue. Push (TMP-> lchild); If (TMP-> rchild! = NULL) myqueue. Push (TMP-> rchild );}}}In non-recursive traversal functions, we use stacks and queues to focus on one thing. In the implementation of stacks and queues, what I first thought of was "ism, go to GitHub to download others' source code for implementation.
The download of a version does not achieve the desired effect, so the target is transferred to C ++ STL. The final version is to implement binary tree recursive functions on the C source file, implement non-recursive functions of Binary trees in the CPP file. therefore, when making, define a variable recu = y to apply recursive functions. Otherwise, apply non-recursive functions.
Because C and C ++ are public, we have to move our header files. Otherwise, unexpected situations may occur.
/* Binarytree. H */# ifndef binarytree_h # define binarytree_h # ifdef _ cplusplusextern "C" {# endiftypedef struct node * link; /*** redefinition of Data Types in the node */typedef unsigned char telemtype; struct node {telemtype item; Link lchild, rchild ;}; link Init (telemtype vlr [], telemtype lvr [], int N); void pre_order (link T, void (* visit) (Link); void in_order (link T, void (* visit) (Link); void post_order (link T, void (* visit) (Li NK); # ifndef recuvoid level_order (link T, void (* visit) (Link); # endifvoid pprint (link T); int count (link t ); int depth (link T); void destroy (link T);/*** http://www.cnblogs.com/bizhu/archive/2012/08/19/2646328.html algorithm diagram ** binary sort tree) the binary search tree is also called the binary search tree. * It is either an empty tree or a binary tree with the following properties: * (1) If the left subtree is not empty, then the values of all nodes on the left subtree are smaller than the value of its root node. * (2) if the right subtree is not empty, then, the values of all nodes on the right subtree are greater than the values of the root node. * (3) the left and right subtree are also Binary Decision Trees. * (4) sequential traversal of a binary tree The result is arranged from small to large. ** the advantage of the Binary Search Tree compared with other data structures is that the query and insertion time is less complex, which is O (log n ). * The Binary Search Tree is a basic data structure used to construct more abstract data structures, such as collections, et, and associated arrays. ** Search, insert, and delete operations have the same complexity as tree height, expect o (log n), and worst O (N) (sequential sequence, tree degraded into linear table) * The Binary Search Tree of the Ultimate Edition enables the tree to have O (logn) trees, such as SBT, aVL, and red/black trees. ** the program comes from Linux C Programming one-stop learning */link bstsearch (link T, telemtype key); Link bstinsert (link T, telemtype key); Link bstdelete (link t, telemtype key);/*** http://baike.baidu.com/view/593144.htm? Fr = Aladdin * balanced binary tree */# ifdef _ cplusplus} # endifThe _ cplusplus has been entangled for a long time. I am confused about what he is doing. I am stuck in the specific place for a long time. I finally tried to compile it myself, currently, only empty functions of the response are defined to check whether the compilation connection is OK. From this perspective, hands-on capabilities determine everything.
Make the following changes to the makefile:
#if you want to use recursive func,please make recu=yifeq (y, $(recu))CFLAGS += -DRECUendififeq (y, $(debug))CFLAGS += -gendifCC = gccCPLUS = g++CFLAGS += -WallTARGET = treeall:$(TARGET).c.o:$(CC) $(CFLAGS) -o [email protected] -c $<.cpp.o:$(CPLUS) $(CFLAGS) -o [email protected] -c $<$(TARGET): non_binarytree.o binarytree.o main.o$(CPLUS) $(CFLAGS) -o [email protected] $^test:@./tree > result.txt && python result.py | tree -b2.PHONY: all cleanclean:$(RM) $(TARGET) *.o
Two problems were encountered in the makefile change project:
1. I think the main program is a C program, so GCC is used during connection, and then the error "undefinedreference to '_ gxx_personality_v0'" is reported.
I have never met again. I only asked du Niang. The result is as follows:
For C ++ programs, GCC or G ++ can be used during compilation. However, it is best to use g ++ for connection, because g ++ will automatically connect to the C ++ standard library. You can also use GCC to connect to the C ++ program, however, you must manually connect to the C ++ standard library. Otherwise, an error such as undefined reference to '_ gxx_personality_v/0' may occur. It can be seen that lstdc ++ corresponds to the Standard C ++ library.
2. When the macro recu is defined, the function is repeatedly defined. Check the function definition and use "# dedef recu ***** # endif" in the C file to define the function ", in the C ++ file, "# ifndef recu ...... # endif "is separated." # warning () "is used to add printed information during the compilation process. It is strange to me to define whether recu is always compiled into non-recursive functions.
After careful troubleshooting, we found that no recu was defined when compiling the C ++ file.
The makefile content is changed as follows:
%. O: %. C $ (CC) $ (cflags)-O [email protected]-C $ <%. o: %. CPP $ (cplus) $ (cflags) -O [email protected]-C $ <=============================================== ====. c. o: $ (CC) $ (cflags)-O [email protected]-C $ <. CPP. o: $ (cplus) $ (cflags)-O [email protected]-C $ <
The problem is solved, but there is no difference between the two methods.
========================================================== ====================================
Scripts are automated artifacts. It is really troublesome to manually convert the output sequence into a graph using the tree tool. You may have thought that python was used to test the efficiency of the calculation formula before:
1. If Python outputs the parameters required for calculation, the print function is OK.
2. The test program uses scanf to determine whether the number of input parameters is OK.
3. Implementation means that the structure output by the python program is delivered to the test program through pipelines.
This method may be useful for automated testing.
Write a script to output the test program results in one row:
fp = open('result.txt')for line in fp.readlines():print(line)Add the following statement to makefile:
test:@./tree > result.txt && python result.py | tree -b2
Make test is required for testing.
Binary Tree Learning-non-recursive Traversal