Data Structure Tree exercises (III)

Source: Internet
Author: User

 

Program to determine whether the Tree represented by a binary linked list is a Complete Binary Tree.

Basic Idea: You can use a queue to traverse the layer from top to bottom and from left to right. When a leaf node or no right child node appears for the first time, all the subsequent nodes should be leaf nodes. At the same time, in the traversal, we use the principle that the full Binary Tree "if a node has no left child, there should be no right child" to judge.

Code:

Int judgecomplete (bitree BT) {int tag = 0; bitree P = BT, Q []; If (P = NULL) {return 1;} queueinit (Q ); queuein (Q, P) while (! Queueempty (q) {P = queueout (Q); If (p-> lchild &&! Tag) {queuein (Q, P-> lchild) ;}else {If (p-> lchild) {return 0 ;}else {tag = 1 ;}} if (p-> rchild &&! Tag) {queuein (Q, P-> rchild) ;}else {If (p-> rchild) {return 0 ;}else {tag = 1 ;}}}}

 

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.