Step-by-step write algorithm (the binary tree breadth traversal)

Source: Internet
Author: User

Original: Step-by-step writing algorithm (the binary tree breadth traversal)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


In the traversal of a binary tree, there is a traversal method that is not common, that is, breadth traversal. Unlike the other three traversal methods, the breadth traversal of a binary tree requires additional data structures to help? What data structure is it? That's the queue. Because the queue has a FIFO feature, this feature requires us to iterate over the last data before traversing the new layer of data. For the time being, a friend who has not mastered the knowledge of the queue can take a look at my blog-queue.

A) The following is the newly added queue data structure, where the parts of the database are replaced with pointers to tree node pointers:

typedef struct _QUEUE{INT head;int tail;int length; tree_node** Phead;} QUEUE;
Note: Head means start, tail means end, length is Phead, Phead represents the start address of the pointer


B) Create the queue, as it involves length issues, so 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);p QUEUE = (queue*) malloc (sizeof (QUEUE)); assert (NULL! = pqueue); memset (Pqueue , 0, sizeof (QUEUE));p Queue->phead = (tree_node**) malloc (sizeof (tree_node*) * count); assert (NULL! = Pqueue->phead ); memset (pqueue->phead, 0, sizeof (tree_node*) * count);p queue->head = Pqueue->tail = 0;pqueue->length = Count;return Pqueue;}


c) Implementation of queue data join and data popup operation

void Insert_node_into_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 circular queue, so the data press-in and eject is relatively simple, directly to head and tail processing can be


D) Iterate over the nodes, get the data by layer, and finally the pointer data from the pqueue->phead is the result of the output by the 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);/* First node Join queue */insert_node_into_queue (Pqueue, Pnode);p node = Get_node_from_queue ( Pqueue), while (Pnode) {if (pnode->left) insert_node_into_queue (Pqueue, Pnode->left), if (pnode->right) insert _node_into_queue (Pqueue, pnode->right);p node = get_node_from_queue (Pqueue);} return pqueue;}

Expansion section:

The above method can be implemented in the queue of the output of the layer, then if you want to directly implement the data in the node structure of the layer access to do? In fact, two steps away can be: 1) in the existing Tree_node add prev and Next, (2) according to the pqueue->phead results just obtained, in turn, the Prev and next to assign values. I don't know, my friends, do you understand?




Step-by-step write algorithm (the binary tree breadth traversal)

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.