To judge a complete binary tree:
Complete binary tree, except the last layer, each layer of the node tree has reached the maximum value; On the last layer only a few nodes on the right are missing!
Algorithm ideas:
Traversing a binary tree by hierarchy (top to bottom, left to right), when the left subtree of a node is empty, the right subtree of the node must be empty, and the node that is traversed behind the left
The right subtree must be empty, otherwise it is not a complete binary tree.
Code:
BOOL Iscompletebtree (btree* proot) {if (Proot = = NULL) return false;queue<btree *> Q;q.push (proot); bool Musthavenochild = False;bool result = True;while (!q.empty ()) {btree* Pnode = Q.front (); Q.pop (); if (musthavenochild)// If a node has no child nodes, as long as the node of the empty tree appears, the leaf node must appear (the right subtree of the left tree must be empty) {if (Pnode->m_nleft! = NULL | | Pnode->m_nright! = NULL) {result = False;break; }}else{if (Pnode->m_nleft! = NULL && pnode->m_nright! = null) {Q.push (pnode->m_nleft); Q.push (pnode- >m_nright);} else if (pnode->m_nleft! = null && Pnode->m_nright = = null) {Musthavenochild = True;q.push (pnode->m_ Nleft);} else if (Pnode->m_nleft = = NULL && pnode->m_nright! = null) {result = False;break;} Else{musthavenochild = True;}}} return result;}
Complete test Code:
IsCompleteBTree.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <queue>using namespace std;//node data structure class Btree{public: int m_nvalue; Btree* M_nleft; btree* m_nright;public:btree (int value) {M_nvalue = value;}};/ /Two insert implementation of the fork Tree void Insert (int value, btree* &root) {if (root = NULL) {root = new BTree (value);} else if (value < Root->m_nvalue) Insert (value,root->m_nleft), else if (value > Root->m_nvalue) Insert ( Value,root->m_nright); else;} BOOL Iscompletebtree (btree* proot) {if (Proot = = NULL) return false;queue<btree *> Q;q.push (proot); bool Musthavenochild = False;bool result = True;while (!q.empty ()) {btree* Pnode = Q.front (); Q.pop (); if (musthavenochild)// If a node has no child nodes, as long as the node of the empty tree appears, the leaf node must appear (the right subtree of the left tree must be empty) {if (Pnode->m_nleft! = NULL | | Pnode->m_nright! = NULL) {result = False;break; }}else{if (Pnode->m_nleft! = NULL && pnode->m_nright! = null) {Q.push (pnode->m_nleft); Q.push (pnode- >m_nright);} else if (pnode->m_Nleft = null && pnode->m_nright = = null) {Musthavenochild = True;q.push (pnode->m_nleft);} else if (Pnode->m_nleft = = NULL && pnode->m_nright! = null) {result = False;break;} Else{musthavenochild = True;}}} return result;} int _tmain (int argc, _tchar* argv[]) {btree* m_proot = new BTree (5); insert (6,m_proot); insert (3,m_proot); Insert (4,m_ Proot); Insert (2,m_proot);cout<< "is a complete binary tree:" <<iscompletebtree (M_proot) <<endl;getchar (); return 0 ;}
Whether the C + + algorithm is a complete binary tree