[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
In binary tree traversal, A Traversal method is not common, that is, breadth traversal. Different from the other three traversal methods, the breadth traversal of a binary tree requires an additional data structure to help? What data structure? That is the queue. Because the queue has the characteristics of first-in-first-out, this feature requires that we must end the previous data traversal before traversing a new layer of data. If you have no queue knowledge, you can take a look at my blog-queue.
A) The following is the newly added queue data structure, in which the data part is replaced with the pointer of the Tree node pointer:
Typedef struct _ QUEUE
{
Int head;
Int tail;
Int length;
TREE_NODE ** pHead;
} QUEUE;
Typedef struct _ QUEUE
{
Int head;
Int tail;
Int length;
TREE_NODE ** pHead;
} QUEUE; Note: head indicates start, tail indicates end, length indicates the length of pHead, and pHead indicates the start address of the pointer.
B) Create a queue. Because length is involved, you need to calculate the total number of nodes in the binary tree.
QUEUE * create_queue_for_tree (const TREE_NODE * pTreeNode)
{
QUEUE * pQueue;
Int count;
If (NULL = pTreeNode)
Return NULL;
Count = count_all_node_number (pTreeNode );
PQueue = (QUEUE *) malloc (sizeof (QUEUE ));
Assert (NULL! = PQueue );
Memset (pQueue, 0, sizeof (QUEUE ));
PQueue-> pHead = (TREE_NODE **) malloc (sizeof (TREE_NODE *) * count );
Assert (NULL! = PQueue-> pHead );
Memset (pQueue-> pHead, 0, sizeof (TREE_NODE *) * count );
PQueue-> head = pQueue-> tail = 0;
PQueue-> length = count;
Return pQueue;
}
QUEUE * create_queue_for_tree (const TREE_NODE * pTreeNode)
{
QUEUE * pQueue;
Int count;
If (NULL = pTreeNode)
Return NULL;
Count = count_all_node_number (pTreeNode );
PQueue = (QUEUE *) malloc (sizeof (QUEUE ));
Assert (NULL! = PQueue );
Memset (pQueue, 0, sizeof (QUEUE ));
PQueue-> pHead = (TREE_NODE **) malloc (sizeof (TREE_NODE *) * count );
Assert (NULL! = PQueue-> pHead );
Memset (pQueue-> pHead, 0, sizeof (TREE_NODE *) * count );
PQueue-> head = pQueue-> tail = 0;
PQueue-> length = count;
Return pQueue;
}
C) add data to the queue and pop-up data
Void insert_node_pai_queue (QUEUE * pQueue, TREE_NODE * pNode)
{
If (NULL = pQueue | NULL = pQueue-> pHead | NULL = pNode)
Return;
PQueue-> pHead [pQueue-> tail ++] = pNode;
Return;
}
TREE_NODE * get_node_from_queue (QUEUE * pQueue)
{
If (NULL = pQueue | NULL = pQueue-> pHead)
Return NULL;
If (pQueue-> head = pQueue-> tail)
Return NULL;
Return pQueue-> pHead [pQueue-> head ++];
}
Void insert_node_pai_queue (QUEUE * pQueue, TREE_NODE * pNode)
{
If (NULL = pQueue | NULL = pQueue-> pHead | NULL = pNode)
Return;
PQueue-> pHead [pQueue-> tail ++] = pNode;
Return;
}
TREE_NODE * get_node_from_queue (QUEUE * pQueue)
{
If (NULL = pQueue | NULL = pQueue-> pHead)
Return NULL;
If (pQueue-> head = pQueue-> tail)
Return NULL;
Return pQueue-> pHead [pQueue-> head ++];
} Note: the queue defined here is not a cyclic queue, so it is relatively simple to press in and pop up data. You can directly process the head and tail.
D) traverse the node, get data by layer, and finally get the pointer data in pQueue-> pHead, Which is output by layer.
QUEUE * traverse_node_by_layer (TREE_NODE * pNode)
{
QUEUE * pQueue;
If (NULL = pNode)
Return NULL;
PQueue = create_queue_for_tree (pNode );
Assert (NULL! = PQueue );
/* Add the first node to the queue */
Insert_node_pai_queue (pQueue, pNode );
PNode = get_node_from_queue (pQueue );
While (pNode ){
If (pNode-> left)
Insert_node_pai_queue (pQueue, pNode-> left );
If (pNode-> right)
Insert_node_pai_queue (pQueue, pNode-> right );
PNode = get_node_from_queue (pQueue );
}
Return pQueue;
}
QUEUE * traverse_node_by_layer (TREE_NODE * pNode)
{
QUEUE * pQueue;
If (NULL = pNode)
Return NULL;
PQueue = create_queue_for_tree (pNode );
Assert (NULL! = PQueue );
/* Add the first node to the queue */
Insert_node_pai_queue (pQueue, pNode );
PNode = get_node_from_queue (pQueue );
While (pNode ){
If (pNode-> left)
Insert_node_pai_queue (pQueue, pNode-> left );
If (pNode-> right)
Insert_node_pai_queue (pQueue, pNode-> right );
PNode = get_node_from_queue (pQueue );
}
Return pQueue;
}
Expansion part:
The above method can achieve layer-by-layer output of the queue. What if I want to directly implement layer-by-layer access to data in the node structure? In fact, you can do this in two steps: 1) Add prev and next to the existing TREE_NODE; (2) assign values to prev and next according to the pQueue-> pHead result obtained just now. Do you know what my friends understand?