Fortune left-law basis class4- title 7-2 to determine whether a tree is a complete binary tree

1. Title: determining whether the tree is a complete binary tree

Here Insert Picture Description

2. Prior knowledge: by layer traversal

Here Insert Picture Description
Since the need to use complete binary tree is determined traversal sequence, first explain the lower order traversal, the traversal results in layers is 5,3,8,2,4,6,10,7 FIG.

(1) ideas

Use queue for storage, the first node first in the queue, the queue is not empty in the case of the first node of the team around the child into the queue, respectively, and then the first team out of the queue.

(2) the core code

void SeqTraverse(Tree *head)
{
	queue<Tree*> t;
	if(head == NULL)
		return; 
	t.push(head);//头节点进队列
	while(!t.empty())
	{
		head = t.front();
		cout<<t.front()->val<<" "; 
			
		//队首的左右孩子入队列
		if(head->left!=NULL)
			t.push(head->left);
		if(head->right!=NULL)
			t.push(head->right);
		t.pop();
	}
}

(3) complete code

#include<iostream>
#include<queue>
using namespace std;

//结构体定义
class Tree
{
public:
	int val;
	Tree *left;
	Tree *right;
	Tree(int x){
		this->val = x;
		this->left = NULL;
		this->right = NULL;
	}
};
//按层遍历
void SeqTraverse(Tree *head)
{
	queue<Tree*> t;
	if(head == NULL)
		return; 
	t.push(head);//头节点进队列
	while(!t.empty())
	{
		head = t.front();
		cout<<t.front()->val<<" "; 
			
		//队首的左右孩子入队列
		if(head->left!=NULL)
			t.push(head->left);
		if(head->right!=NULL)
			t.push(head->right);
		t.pop();//出队
	}
}

int main()
{
	Tree *head = new Tree(5);
	head->left = new Tree(3);
	head->left->left = new Tree(2);
	head->left->right = new Tree(4);
	head->right = new Tree(8);
	head->right->left = new Tree(6);
	head->right->right = new Tree(10);
	head->right->left->right= new Tree(7);

	SeqTraverse(head);
	system("pause");
	return 0; 
}

3. judged complete binary tree

(1) ideas

The whole idea is to directly return true if the tree is empty, if the tree is not empty, then for each node by traversing the sequence
① If the current node has no left and right child subtree is not necessarily complete binary tree
② If a node is not about the whole child (the child is not left empty, the right children are empty; or about children are empty) the node after the node is a leaf node (no children around).
Fully meet ①② is complete binary tree.

(2) the core code

The above-mentioned ② That is not all about children, regarded as judge turned a leaf node stage, the use of leaf boolean variable represents 1 open, 2 does not open. In general there are two cases directly returned false: After entering the stage of determining if the leaves head->left != NULL || head->right != NULLindicate the presence of about a child, not a leaf node, returns false when, also in head->left == NULL && head->right != NULLthe case, namely the right there is also returns false when the left does not exist. Here both the combined code

//开启叶子节点阶段后,若后续节点不是叶子节点返回false
//没开启时,右孩子有左孩子无,返回false
if(leaf && (head->left != NULL || head->right != NULL)
	||  
	head->left == NULL && head->right != NULL)
	return false;

When open leaf node stage?
A total of four cases about children, A left and a right there, B left without a right, C left and right have no, D left and right no no. A normal queues, C is not a binary situation, B, D stage where the leaf node is determined to enter
the B, D can be written in the case if(head->left == NULL || head->right == NULL), it may be summarized as determining the leaf node into the stage in a case where the right does not exist

if(head->right != NULL)
	q.push(head->right);
else //等价于if(head->left == NULL || head->right == NULL)
	leaf = true;

Analyzing the whole complete binary function is as follows:

bool isCBT(Tree *head)
{
	if(head == NULL)
		return true;
	queue<Tree *>q;
	bool leaf = false;
	q.push(head);
	while(!q.empty())
	{
		head = q.front();
		
		//开启叶子节点阶段后,若后续节点不是叶子节点返回false
		//没开启时,右孩子有左孩子无,返回false
		if(leaf && (head->left != NULL || head->right != NULL)
			||  
			head->left == NULL && head->right != NULL)
			return false;
		if(head->left != NULL)
			q.push(head->left);
		if(head->right != NULL)
			q.push(head->right);
		else //等价于if(head->left == NULL || head->right == NULL)
			leaf = true;
		q.pop();
	}
	return true;
}

(3) complete code

#include<iostream>
#include<queue>
using namespace std;

class Tree
{
public:
	int val;
	Tree *left;
	Tree *right;
	Tree(int x){
		this->val = x;
		this->left = NULL;
		this->right = NULL;
	}
};

bool isCBT(Tree *head)
{
	if(head == NULL)
		return true;
	queue<Tree *>q;
	bool leaf = false;
	q.push(head);
	while(!q.empty())
	{
		head = q.front();

		//开启叶子节点阶段后,若后续节点不是叶子节点返回false
		//没开启时,右孩子有左孩子无,返回false
		if(leaf && (head->left != NULL || head->right != NULL)
			||  
			head->left == NULL && head->right != NULL)
			return false;
		if(head->left != NULL)
			q.push(head->left);
		if(head->right != NULL)
			q.push(head->right);
		else //等价于if(head->left == NULL || head->right == NULL)
			leaf = true;
		q.pop();
	}
	return true;
}

int main()
{
	Tree *head = new Tree(5);
	head->left = new Tree(3);
	head->left->left = new Tree(2);
	head->left->right = new Tree(4);
	head->right = new Tree(8);
	head->right->left = new Tree(6);
	head->right->right = new Tree(10);


	//test
	head->right->left->left= new Tree(7);
	head->right->left->right= new Tree(7);
	//head->right->right->left= new Tree(7);
	//head->right->right->right= new Tree(7);
	head->left->left->left= new Tree(7);
	head->left->left->right= new Tree(7);
	head->left->right->left= new Tree(7);
	head->left->right->right= new Tree(7);

	cout<<isCBT(head);

	system("pause");
	return 0; 
}
Published 51 original articles · won praise 1 · views 1371

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/104050660