Binary Search Tree sequence traversal C Language

Source: Internet
Author: User

The traversal of the sequence is complete.

Unlike pre-order traversal, middle-order traversal, and post-order traversal, sequence traversal does not use the stack mode, but uses a queue.

The data in the queue, that is, the queueitem is the Binary Search Tree node pointer, which can save the node and easily handle the return value when the stack is empty. That is, null can be returned.

Implemented using a function that accepts a tree-type variable. No return value is returned.

First, the root node of the tree enters the queue, and then the queue is not empty as the condition for loop. in the cycle, a node is first deleted from the queue, and the pointer of the node is obtained through the return value queueitem of deletequeue. this pointer can be processed, such as printing information. then, check the left subtree of the node and whether the right subtree is not empty. In turn, add the pointer of the Left subtree to the queue, and the loop body ends.

With the features of the queue, I feel that the data structure field is really very promising.

void level_inorder_traversal (const tree) <br/>{< br/> queue; <br/> queueitem temp; </P> <p> initializequeue (& Queue ); </P> <p> enqueue (& queue, & tree); <br/> while (! Queueisempty (& Queue) <br/>{< br/> temp = dequeue (& Queue); <br/> print_item (temp-> item ); <br/> If (temp-> left! = NULL) <br/> enqueue (& queue, & temp-> left); <br/> If (temp-> right! = NULL) <br/> enqueue (& queue, & temp-> right); <br/>}

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.