设为首页 加入收藏

TOP

C++算法之 判断是否为平衡二叉树 求二叉树的镜像(一)
2015-07-20 17:25:12 来源: 作者: 【 】 浏览:9
Tags:算法 判断 是否 平衡

1:判断是否为平衡二叉树:

//方法1:
int TreeDepth(BTree* pRoot)
{
	if (pRoot == NULL)
		return 0;
	int nLeftDepth = TreeDepth(pRoot->m_pLeft);
	int nRightDepth = TreeDepth(pRoot->m_pRight);

	return (nLeftDepth > nRightDepth)? (nLeftDepth+1):(nRightDepth+1);
}

bool IsBalanced(BTree* pRoot)
{
	if (pRoot == NULL)
		return true;
	int nLeftDepth = TreeDepth(pRoot->m_pLeft);
	int nRightDepth = TreeDepth(pRoot->m_pRight);
	int diff = nRightDepth - nLeftDepth;
	if (diff > 1 || diff < -1)
		return false;

	return IsBalanced(pRoot->m_pLeft)&&IsBalanced(pRoot->m_pRight);
	
}
/*
上面的方法:在求该节点的左右子树的深度的时候遍历一遍树,再次判断子树的平衡性的时候又要遍历
一遍树结构,造成遍历多次!
*/
bool IsBalanced3(BTree* pRoot, int& depth)
{
	if(pRoot== NULL)
	{
		depth = 0;
		return true;
	}

	int nLeftDepth;
	bool bLeft= IsBalanced3(pRoot->m_pLeft, nLeftDepth);
	int nRightDepth;
	bool bRight = IsBalanced3(pRoot->m_pRight, nRightDepth);
	
	if (bLeft && bRight && abs(nLeftDepth - nRightDepth) <=1)
	{
		depth = 1+(nLeftDepth > nRightDepth ? nLeftDepth : nRightDepth);
		return true;	
	}
	else
	{
		return false;
	}
}

bool IsBalanced3(BTree* pRoot)
{
	int depth = 0;
	return IsBalanced3(pRoot, depth);
}


2:求二叉树的镜像

/*
求二叉树的镜像:

方法1: 前序遍历每个节点,如果遍历到的节点有子节点,就交换它的两个子节点。(先交换左子树和右子树,再对左子树和右子树进行镜像操作)
方法2: 如果二叉树不为空,求左子树和右子树的镜像,然后再交换左子树和右子树

*/

void Mirror(BTree* &pRoot)
{
	if(pRoot == NULL)
		return;
	if(pRoot->m_pLeft ==NULL && pRoot->m_pRight == NULL)
		return;

	BTree* pTemp = pRoot->m_pLeft;
	pRoot->m_pLeft = pRoot->m_pRight;
	pRoot->m_pRight = pTemp;

	if(pRoot->m_pLeft)
		Mirror(pRoot->m_pLeft);
	if(pRoot->m_pRight)
		Mirror(pRoot->m_pRight);
}

BTree* Mirror2(BTree* pRoot)
{
	if(pRoot == NULL)
		return NULL;

	BTree*  pLeft = Mirror2(pRoot->m_pLeft);
	BTree*  pRight = Mirror2(pRoot->m_pRight);


	pRoot->m_pLeft = pRight;
	pRoot->m_pRight = pLeft;

	return pRoot;
}


完整测试代码:

// BalanceOfBT.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
  
   
using namespace std;

class BTree
{
public:
	int     m_nValue;
	BTree*  m_pLeft;
	BTree*  m_pRight;

	BTree(int m):m_nValue(m)
	{
		m_pLeft = m_pRight = NULL;
	}

};
//二叉树的插入实现
void Insert(int value, BTree* &root)
{
	if (root == NULL)
	{
		root = new BTree(value);
	}
	else if(value < root->m_nValue)
		Insert(value,root->m_pLeft);
	else if(value > root->m_nValue)
		Insert(value,root->m_pRight);
	else
		;
}

//方法1:
int TreeDepth(BTree* pRoot)
{
	if (pRoot == NULL)
		return 0;
	int nLeftDepth = TreeDepth(pRoot->m_pLeft);
	int nRightDepth = TreeDepth(pRoot->m_pRight);

	return (nLeftDepth > nRightDepth)? (nLeftDepth+1):(nRightDepth+1);
}

bool IsBalanced(BTree* pRoot)
{
	if (pRoot == NULL)
		return true;
	int nLeftDepth = TreeDepth(pRoot->m_pLeft);
	int nRightDepth = TreeDepth(pRoot->m_pRight);
	int diff = nRightDepth - nLeftDepth;
	if (diff > 1 || diff < -1)
		return false;

	return IsBalanced(pRoot->m_pLeft)&&IsBalanced(pRoot->m_pRight);
	
}
/*
上面的方法:在求该节点的左右子树的深度的时候遍历一遍树,再次判断子树的平衡性的时候又要遍历
一遍树结构,造成遍历多次!
*/
bool IsBalanced3(BTree* pRoot, int& depth)
{
	if(pRoot== NULL)
	{
		depth = 0;
		return true;
	}

	int nLeftDepth;
	bool bLeft= IsBalanced3(pRoot->m_pLeft, nLeftDepth);
	int nRightDepth;
	bool bRight = IsBalanced3(pRoot->m_pRight, nRightDepth);
	
	if (bLeft && bRight && abs(nLeftDepth - nRightDepth) <=1)
	{
		depth = 1+(nLeftDepth > nRightDepth ? nLeftDepth : nRightDepth);
		return true;	
	}
	else
	{
		return false;
	}
}

bool IsBalanced3(BTree* pRoot)
{
	int depth = 0;
	return IsBala
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇NYOJ 972 核桃的数量 (最小公倍数) 下一篇归并排序(MergeSort)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python 数据分析与可 (2025-12-26 21:51:20)
·从零开始学Python之 (2025-12-26 21:51:17)
·超长干货:Python实 (2025-12-26 21:51:14)
·为什么 Java 社区至 (2025-12-26 21:19:10)
·Java多线程阻塞队列 (2025-12-26 21:19:07)