完整类实现:构造,析构,遍历二叉树

根据前面一个博文内容已经讲述了如何根据两种遍历方式进行构建二叉树

这里利用递归方式遍历二叉树,递归方式比较简单,后续补充其余非递归方式

再此主要是完善类的使用:
其中重点在于:接口定义

二叉树的析构删除

以及类成员变量中如果有指针,同时涉及复制构造函数和赋值操作符函数时需要用到的智能指针

如果接口方面定义不够好,还望包涵

如果有对智能指针不理解的地方,可以移步 http://blog.csdn.net/xietingcandice/article/details/39670269

.h文件

#include 
#include 
#include 
using namespace std;

struct BinaryTreeNode
{
	int Value;
	BinaryTreeNode * pLeft;
	BinaryTreeNode * pRight;
	BinaryTreeNode()
	{
		Value = 0;
		pLeft = NULL;
		pRight = NULL;
	}
};

BinaryTreeNode* ReconstructTree(int *startBackorder,int *endBackorder,int *startInorder,int *endInorder);
void DeleteRoot(BinaryTreeNode *pRoot);
class ScanBinaryTreeNode;
//<因为遍历的类中出现了指针,为了防止copy函数和赋值操作符出现内存泄露定义智能指针对应的类
class BinaryRoot
{
	friend class ScanBinaryTreeNode;  //<友元结构
	BinaryTreeNode *mpRoot;  
    size_t mUse;  
	BinaryRoot(	BinaryTreeNode *pRoot):mpRoot(pRoot),mUse(1) {}  //<初始化为1
	BinaryRoot():mpRoot(NULL),mUse(0){};//<默认构造函数
    ~BinaryRoot();
};
class ScanBinaryTreeNode
{
public:
	ScanBinaryTreeNode(int *startBackOrder,int *endBackOrder, int *startInOrder,int *endInOrder);
	ScanBinaryTreeNode(){};
	ScanBinaryTreeNode(const ScanBinaryTreeNode &CopyTemp);
	ScanBinaryTreeNode& operator = (const ScanBinaryTreeNode &CopyTemp);
	~ScanBinaryTreeNode();
	void ScanPreOrder();
	void ScanInOrder();
	void ScanBackOrder();
private:
	BinaryRoot* mpRootNode;
};


.c文件:

#include"BinaryTree.h"
#define _CRTDBG_MAP_ALLOC
#include 
#include 


 BinaryTreeNode* ReconstructTree(int *startBackorder,int *endBackorder,int *startInorder,int *endInorder)//<根据后续和中序遍历构建二叉树
{
	BinaryTreeNode * root = new BinaryTreeNode;
	root->Value = * endBackorder;
	if (startBackorder == endBackorder) //<满足条件返回对应的根节点的值
	{
		if (startInorder == endInorder && (*startInorder == *startBackorder))
		{
			return root;
		}
		else
		{
			throw std::exception("Invalid input");
		}
	}
	int * rootInoder = startInorder;
	while ((rootInoder <= endInorder) && (*rootInoder != root->Value))
	{
		rootInoder++;
	}
	if (rootInoder > endInorder)
	{
		throw std::exception("Invalid input");
	}
	int leftLength = rootInoder-startInorder;
	if (leftLength > 0)
	{
		root->pLeft = ReconstructTree(startBackorder,startBackorder+leftLength-1,startInorder,rootInoder-1);
	}
	if ((endBackorder-startBackorder) > leftLength)
	{
		root->pRight = ReconstructTree(startBackorder+leftLength,endBackorder-1,rootInoder+1,endInorder);
	}
	return root;
}
void DeleteRoot(BinaryTreeNode *pRoot) //<根据根节点删除整棵树
 {
	 if(pRoot == NULL)
	 {
		 return;
	 }
	 BinaryTreeNode * pLeft = pRoot->pLeft;
	 BinaryTreeNode * pRight = pRoot->pRight;
	 delete pRoot;
     pRoot = NULL;
	 if(pLeft)
	 {
		 DeleteRoot(pLeft);
	 }
	 if(pRight)
	 {
		 DeleteRoot(pRight);
	 }
	 return;
 }
BinaryRoot::~BinaryRoot()  
    {  
		DeleteRoot(mpRoot); 
    }  
ScanBinaryTreeNode::ScanBinaryTreeNode(int *startBackOrder,int *endBackOrder, int *startInOrder,int *endInOrder)
{
		BinaryTreeNode *node = ReconstructTree(startBackOrder,endBackOrder,startInOrder,endInOrder);
		mpRootNode = new BinaryRoot(node); //<初始化一个指向根节点的指针
}
ScanBinaryTreeNode::ScanBinaryTreeNode(const ScanBinaryTreeNode &CopyTemp)
{
	mpRootNode = CopyTemp.mpRootNode;
	++mpRootNode->mUse;
}
ScanBinaryTreeNode::~ScanBinaryTreeNode()
{
	if((--mpRootNode->mUse) == 0) //<指针没有对象的时候进行删除
	{
		delete mpRootNode;
		mpRootNode = NULL;
	}
}
ScanBinaryTreeNode& ScanBinaryTreeNode::operator = (const ScanBinaryTreeNode &CopyTemp)
{
	++ CopyTemp.mpRootNode->mUse;  //
    if ( -- mpRootNode->mUse == 0)  
		delete mpRootNode;  
    mpRootNode = CopyTemp.mpRootNode;  
    return *this; 
}
void PreOrder(BinaryTreeNode *pRoot)
{
	if(pRoot == NULL)
	{
		return;
	}
	printf("%d ",pRoot->Value);
	BinaryTreeNode *pLeft = pRoot->pLeft;
	BinaryTreeNode *pRight = pRoot->pRight;
	if(pLeft)
	{
		PreOrder(pLeft);
	}
	if(pRight)
	{
		PreOrder(pRight);
	}
	return;

}
void BackOrder(BinaryTreeNode *pRoot)
{
	if(pRoot == NULL)
	{
		return;
	}
	BinaryTreeNode *pLeft = pRoot->pLeft;
	BinaryTreeNode *pRight = pRoot->pRight;
	if(pLeft)
	{
		BackOrder(pLeft);
	}
	if(pRight)
	{
		BackOrder(pRight);
	}
	printf("%d ",pRoot->Value);
	return;

}
void InOrder(BinaryTreeNode *pRoot)
{
	if(pRoot == NULL)
	{
		return;
	}
	BinaryTreeNode *pLeft = pRoot->pLeft;
	BinaryTreeNode *pRight = pRoot->pRight;
	if(pLeft)
	{
		InOrder(pLeft);
	}
	printf("%d ",pRoot->Value);
	if(pRight)
	{
		InOrder(pRight);
	}
	return;

}
void ScanBinaryTreeNode::ScanPreOrder()
{
	PreOrder(mpRootNode->mpRoot);
}
void ScanBinaryTreeNode::ScanBackOrder()
{
	BackOrder(mpRootNode->mpRoot);
}
void ScanBinaryTreeNode::ScanInOrder()
{
	InOrder(mpRootNode->mpRoot);
}
void Order()
{
	int BackArry[8] = {7,4,2,5,8,6,3,1};
	int MiddleArry[8] = {4,7,2,1,5,3,8,6};
	if (BackArry == NULL || MiddleArry == NULL)
	{
		return;
	}
	ScanBinaryTreeNode root(BackArry,BackArry+7,MiddleArry,MiddleArry+7);
	ScanBinaryTreeNode root1 = root;
	ScanBinaryTreeNode root2(root1);
	ScanBinaryTreeNode root3(root);
	root.ScanBackOrder();
	printf("\n");
	root.ScanInOrder();
	printf("\n");
	root.ScanPreOrder();
}
int main()
{  
	Order();
	//int* i = new int;
	_CrtDumpMemoryLeaks();
	return 0;
}


你可能感兴趣的:(算法,C++,面试题目)