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. Traverse the binary tree by layer
The first step: need to use the queue, first the root node proot queued;
Second step: When the queue is not empty, get the first element of the team and out of the team, assigned to Proot, the third step;
The third step: if the Proot left node exists, then the queue; if the proot right node exists, then the queue; second step.
void Leveltraverse (btreenode_t *proot) { if (proot = = NULL) return; Queue <btreenode_t *> que; Que.push (proot); while (!que.empty ()) { proot = Que.front (); Que.pop (); Visit (proot); if (proot->m_pleft! = NULL) que.push (proot->m_pleft); if (proot->m_pright! = NULL) que.push (proot->m_pright); } return;}
Binary tree (6)----traversing a binary tree by layer