Question: How to determine whether a binary tree is a Complete Binary Tree: 1. First, define the concept of a Complete Binary Tree. Except for the last layer, the number of nodes in all layers reaches the maximum value, and the last layer is continuously concentrated on the leftmost. The empty tree is also a Complete Binary Tree. 2. We can traverse this binary tree through sequence traversal and use a queue to store the traversal nodes. You can use the node at the last layer to solve the problem on the left side. For details, refer to the Code:
Code:
Bool iscompletetree (ptree pt) {queue Q; initqueue (& Q); enqueue (& Q, pt); // enter the root pnode PTR = NULL; while (PTR = dequeue (& Q ))! = NULL) // leave the queue continuously until an empty {enqueue (& Q, PTR-> left) is encountered; // The left child enters the queue, and the empty Node also enters the queue enqueue (& Q, PTR-> right); // enter the right child} while (! Isqueueempty (& Q) // If the queue is not empty, it is always a null node in the queue {PTR = dequeue (& Q); If (PTR! = NULL) // if a non-null node exists, it indicates that it is not a complete Binary Tree {return false ;}} return true ;}
Complete code:
/* Determine whether it is a Complete Binary Tree by rowandjj2014/8/19 */# include <iostream> using namespace STD; typedef struct _ node _ {int data; struct _ node _ * left; struct _ node _ * right;} node, * pnode, * ptree; typedef struct _ queuenode _ {pnode queue_data; struct _ queuenode _ * Next;} queuenode, * pqueuenode; typedef struct _ queue _ {pqueuenode phead; pqueuenode ptail; int size;} queue, * pqueue; void initqueue (pqueue PQ) {PQ-> phead = PQ-> ptail = (pqueuenode) Ma Lloc (sizeof (queuenode); If (! PQ-> ptail) {exit (-1);} PQ-> phead-> next = NULL; PQ-> size = 0;} void enqueue (pqueue PQ, pnode data) {If (PQ = NULL) {return;} pqueuenode pnew = (pqueuenode) malloc (sizeof (queuenode); If (! Pnew) {exit (-1);} pnew-> queue_data = data; pnew-> next = NULL; PQ-> ptail-> next = pnew; PQ-> ptail = pnew; PQ-> size ++;} pnode dequeue (pqueue PQ) {If (PQ = NULL) {return NULL ;} pqueuenode pdel = PQ-> phead-> next; If (pdel = NULL) {return NULL;} else {pnode result = pdel-> queue_data; if (PQ-> ptail = pdel) {PQ-> ptail = PQ-> phead;} PQ-> phead-> next = pdel-> next; PQ-> size --; free (pdel); return result ;}} bool isqueueempty (pqueue PQ) {return PQ-> size = 0;} bool iscompletetree (ptree pt) {queue Q; initqueue (& Q); enqueue (& Q, pt ); pnode PTR = NULL; while (PTR = dequeue (& Q ))! = NULL) {enqueue (& Q, PTR-> left); enqueue (& Q, PTR-> right);} while (! Isqueueempty (& Q) {PTR = dequeue (& Q); If (PTR! = NULL) {return false ;}} return true;} void createtree (ptree * proot) {int data; CIN> data; If (Data =-1) {return;} * proot = (pnode) malloc (sizeof (node); If (* proot = NULL) {exit (-1);} (* proot) -> DATA = data; (* proot)-> left = NULL; (* proot)-> right = NULL; createtree (& (* proot)-> left ); createtree (& (* proot)-> right);} void displaytree (ptree pt) {If (Pt = NULL) {return ;} cout <Pt-> data <"; if (Pt-> left) {displaytree (Pt-> left);} If (Pt-> right) {displaytree (Pt-> right) ;}} int main () {ptree P = NULL; createtree (& P); cout <iscompletetree (p) <Endl; displaytree (p); cout <Endl; return 0 ;}