1, Binary tree definition:
typedef struct BTREENODEELEMENT_T_ { void *data;} btreenodeelement_t;typedef struct Btreenode_t_ { btreenodeelement_t *m_pelemt; struct Btreenode_t_ *m_pleft; struct btreenode_t_ *m_pright;} btreenode_t;
2, Middle sequence traversal
Definition: First access the left subtree, then access the root node, and finally access the right subtree
(1) Recursive implementation
If the root node is null, the return
If the root node is not NULL, the left subtree is first accessed, then the root node is accessed, and the right subtree is accessed last
void Inordertraverse (btreenode_t *proot) { if (proot = = NULL) return; Inordertraverse (proot->m_pleft); Visiste (proot); Inordertraverse (proot->m_pright);}
(2) Non-recursive implementation
The first step: given the root node proot, determine whether the proot is empty, if not empty, then take the second step, if it is empty, then the third step;
The second step: the Proot into the stack, the Proot left node assigned to Proot, and then the first step;
The third step: Determine whether the stack is empty, if it is empty, then the end, if not empty, then remove the top of the stack to Proot, and out of the stack, access to Proot, and then assign the right node of Proot to Proot, and then take the first step.
void Inordertraverse (btreenode_t *proot) { if (proot = = NULL) return; Stack < btreenode_t *> St; while (proot! = NULL | |!st.empty ()) { while (proot! = null) { st.push (proot); Proot = proot->m_pleft; } if (!st.empty ()) { proot = St.top (); St.pop (); Visit (proot); Proot = proot->m_pright; } } return;}
Binary tree (2)----sequence traversal, recursive and non-recursive implementations