Fortune left Method 8 Title based class4- known a complete binary tree, the number of nodes which seek

1. Title: Given a complete binary tree, find the number of its nodes. Requirements: time complexity is less than O (N), N is the number of nodes of the tree

2. Analysis

Since the requirement is less than the time complexity O (N), the method can not be used to traverse, consider the nature of the full binary tree: the height is k, the number of nodes is 2 ^ k - 1. The complete binary tree sub-ever solution for the full binary tree, and then quickly obtain the number of nodes.
The number of left border node calculation, to obtain the deepest H, traversing the right subtree of the current node, it is determined whether or reaches the last layer, if it reaches the last level is full binary tree below the left, then right subtree recursion: Thinking . If not to the right sub-tree Height - 1 full binary tree, then recursively the left subtree.

Here Insert Picture Description
EG
Here Insert Picture Description① current node is x, is the deepest 4, left and right sub-tree has reached the deepest 4, on the left side of the full binary tree, node 2 ^ 3-1 = 7, 8 coupled with the top node.
② In this case the calculation of the right subtree nodes, most 4, 3 is not the right subtree to the deepest, then the right subtree of the full binary tree. 1 -1 + 1'd = ^ 2 2
③ Calculation ,, most left subtree node 4, 3 is a right subtree, then the right subtree of the full binary tree ^ 2 = 1 0 -1 +1
last point of the last left ④, and finally reached the level returns 1
⑤8 + 2 + 1 + 1 = 12 is
can be seen each decomposition if the right side to reach the deepest, left for the full binary tree, on the contrary, the right subtree binary tree is full. Whichever side full binary tree, the current head node will be calculated in, and and 2 ^ l - 1, -1, offset.

3. core code

In the actual programming, the full binary tree will also be required to record the current number of layers in order to calculate the full story binary tree.
Here Insert Picture Description

(1) The main function

The number of computing nodes, the first node is empty directly returns 0, the air-conditioning is not a function of computing nodes

int nodeNum(Tree* head)
{
	if(head == NULL)
		return 0;
	return bs(head,1,most_level(head,1));
}

(2) Depth of the tree

//该节点在level层,计算此节点深度
int most_level(Tree *head,int level)
{
	while(head != NULL)
	{
		level++;
		head = head->left;
	}
	return level - 1;
}

(3) counting the number of node

Right subtree leftmost depth and the overall depth of the same, left full binary trees, the number of computing nodes recursively right subtree, otherwise the right to full binary tree (height smaller than the left 1), count the number of right node, and then left recursion .

int bs(Tree *head,int level,int h)
{
	if(level == h)//当前层为最后一层,只有一个节点(叶节点)
		return 1;
	if(most_level(head->right,level + 1) == h)//右子树最左深度和整体深度比较,子树需要level + 1
		//左树节点个数 2^(h - level) -1 + 当前节点个数1 再加上递归的右边的节点个数
		return (1 << (h - level)) + bs(head->right,level + 1,h);
	else
		//右树节点个数 2^(h - level -1) -1 + 当前节点个数1 加上递归的左边的节点个数
		//h - level -1是因为右树不满的情况下,高度比左树还要小1
		return (1 << (h - level - 1)) + bs(head->left,level + 1,h);
}

1 << xRepresents 2 ^ x

4. The 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;
	}
};

int most_level(Tree *head, int level);
int bs(Tree *head,int level,int h);

int nodeNum(Tree* head)
{
	if(head == NULL)
		return 0;
	return bs(head,1,most_level(head,1));
}

int bs(Tree *head,int level,int h)
{
	if(level == h)//当前层为最后一层,只有一个节点(叶节点)
		return 1;
	if(most_level(head->right,level + 1) == h)//右子树最左深度和整体深度比较
		//左树节点个数 2^(h - level) -1 + 当前节点个数1 加上递归的右边的节点个数
		return (1 << (h - level)) + bs(head->right,level + 1,h);
	else
		//右树节点个数 2^(h - level -1) -1 + 当前节点个数1 加上递归的左边的节点个数
		//h - level -1是因为右树不满的情况下,高度比左树还要小1
		return (1 << (h - level - 1)) + bs(head->left,level + 1,h);
}

//该节点在level层,计算此节点深度
int most_level(Tree *head,int level)
{
	while(head != NULL)
	{
		level++;
		head = head->left;
	}
	return level - 1;
}

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->left->left->left= new Tree(7);

	
	cout<<nodeNum(head)<<endl;

	system("pause");
	return 0; 
}

5. Complexity Analysis

Recursive, since the side of the tree always full, then each layer need only to traverse a node, left or right, a total required current node traversed O (logN), during each traversal, the depth of the tree needed Qiuzi, complexity is still O (logN), the overall complexity is O [(logN)] ^ 2

Published 51 original articles · won praise 1 · views 1370

Guess you like

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