Whether the C + + algorithm is a complete binary tree

Source: Internet
Author: User

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

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.