此为头文件.h:
#pragma once
class CMyAVL
{
public:
typedef struct _NODE
{
int nData;
_NODE* pLeft;
_NODE* pRight;
}NODE, *PNODE;
public:
CMyAVL();
virtual ~CMyAVL();
bool Insert(int nEle);// 插入数据
bool Erase(int nEle);// 删除数据
PNODE Find(int nFind);// 找数据
bool Change(int nFind, int nChange); // 更改数据
//// 遍历
void PreOrder();// 前序
void MidOrder();// 中序
void LastOrder();// 后序
void GetHeight();//获取总高度,左子树,右子数高度
// 用于演示
void show();
protected:
// 内部接口
PNODE m_pRoot;//头指针
bool Insert(PNODE &pInsert, int nEle);
bool Erase(PNODE &pDelete, int nEle);// 删除数据
void Release(PNODE &pRelease); //释放空间
int GetHeight(PNODE &pHeight); //获取高度
bool GetMax(PNODE pMax, int &nMax); // 获取最大值
bool GetMin(PNODE pMin, int &nMin); // 获取最小值
PNODE Find(PNODE &pNode, int nFind);// 找数据
void PreOrder(const PNODE &pPreOrder);// 前序
void MidOrder(const PNODE &pMidOrder);// 中序
void LastOrder(const PNODE &pLastOrder);// 后序
// 用于演示
void PrintTree(PNODE& pNode, int nLayer);
};
此为源文件.cpp:
#include "stdafx.h"
#include "MyAVL.h"
#include <iostream>
using std::cout;
using std::endl;
CMyAVL::CMyAVL() :m_pRoot(nullptr)
{
}
CMyAVL::~CMyAVL()
{
Release(m_pRoot);
}
bool CMyAVL::Insert(int nEle)
{
return Insert(m_pRoot, nEle);
}
bool CMyAVL::Insert(PNODE &pInsert, int nEle)
{
if (pInsert == nullptr) //到叶子节点插入
{
pInsert = new NODE;
pInsert->nData = nEle;
pInsert->pLeft = nullptr;
pInsert->pRight = nullptr;
return true;
}
if (nEle>pInsert->nData) //向右插入
{
return Insert(pInsert->pRight, nEle);
}
if (nEle < pInsert->nData) //向左插入
{
return Insert(pInsert->pLeft, nEle);
}
return false;
}
bool CMyAVL::Erase(int nEle)
{
return Erase(m_pRoot, nEle);
}
bool CMyAVL::Erase(PNODE &pDelete, int nEle)
{
if (pDelete == nullptr) //到叶子节点还未找到
{
return false;
}
if (nEle > pDelete->nData) //向右找
{
return Erase(pDelete->pRight, nEle);
}
else if (nEle < pDelete->nData) //向左找,前面的else不能丢否则会丢失数据
{
return Erase(pDelete->pLeft, nEle);
}
else //找到了
{
if (pDelete->pLeft==nullptr&&pDelete->pRight==nullptr)//叶子节点
{
delete[] pDelete;
pDelete = nullptr;
return true;
}
//先求左右子树高
int dwLeft = GetHeight(pDelete->pLeft);
int dwRight = GetHeight(pDelete->pRight);
//去较高的那一侧找替换点删除
if (dwLeft>dwRight)
{//去左子树找最大的值
int nMax = 0;
GetMax(pDelete->pLeft,nMax);//用最大的值替换
pDelete->nData = nMax;
return Erase(pDelete->pLeft, nMax);//删除刚才最大的值
}
else
{//去右子树找最小的值
int nMin = 0;
GetMin(pDelete->pRight,nMin);//用最小的值替换
pDelete->nData = nMin;
return Erase(pDelete->pRight, nMin);//删除刚才最小的值
}
}
return false;
}
void CMyAVL::Release(PNODE &pRelease)
{
if (pRelease)
{
Release(pRelease->pLeft);
Release(pRelease->pRight);
delete[] pRelease;
pRelease = nullptr;
}
}
int CMyAVL::GetHeight(PNODE &pHeight)
{
if (pHeight == nullptr)
{
return 0;
}
int dwLeft = GetHeight(pHeight->pLeft);
int dwRight= GetHeight(pHeight->pRight);
return (dwLeft>dwRight?dwLeft:dwRight)+1;//返回子数高的那一个
}
void CMyAVL::GetHeight()
{
cout << "总数高:" << GetHeight(m_pRoot)<<endl;
cout << "左子树高:" <<GetHeight(m_pRoot->pLeft)<< endl;
cout << "右子树高:" << GetHeight(m_pRoot->pRight) << endl << endl;
}
bool CMyAVL::GetMax(PNODE pMax, int &nMax)
{
while (pMax->pRight)
{
pMax = pMax->pRight;
}
nMax = pMax->nData;
return true;
}
bool CMyAVL::GetMin(PNODE pMin, int &nMin)
{
while (pMin->pLeft)
{
pMin = pMin->pLeft;
}
nMin = pMin->nData;
return true;
}
CMyAVL::PNODE CMyAVL::Find(int nFind)
{
return Find(m_pRoot, nFind);
}
CMyAVL::PNODE CMyAVL::Find(PNODE &pNode, int nFind)
{
if (pNode == nullptr)
{
return false;
}
if (nFind > pNode->nData)
{
return Find(pNode->pRight, nFind);
}
if (nFind < pNode->nData)
{
return Find(pNode->pLeft, nFind);
}
}
bool CMyAVL::Change(int nFind, int nChange)
{
if (Erase(nFind))
{
return Insert(nFind);
}
return false;
}
void CMyAVL::PreOrder(const PNODE &pPreOrder)
{
if (pPreOrder==nullptr)
{
return;
}
cout<<pPreOrder->nData<<" ";
PreOrder(pPreOrder->pLeft);
PreOrder(pPreOrder->pRight);
}
void CMyAVL::PreOrder()
{
PreOrder(m_pRoot);
}
void CMyAVL::MidOrder(const PNODE &pMidOrder)
{
if (pMidOrder == nullptr)
{
return;
}
MidOrder(pMidOrder->pLeft);
cout << pMidOrder->nData << " ";
MidOrder(pMidOrder->pRight);
}
void CMyAVL::MidOrder()
{
MidOrder(m_pRoot);
}
void CMyAVL::LastOrder(const PNODE &pLastOrder)
{
if (pLastOrder == nullptr)
{
return;
}
LastOrder(pLastOrder->pLeft);
LastOrder(pLastOrder->pRight);
cout << pLastOrder->nData << " ";
}
void CMyAVL::LastOrder()
{
LastOrder(m_pRoot);
}
void CMyAVL::PrintTree(PNODE& pNode, int nLayer)
{
if (pNode == nullptr)
{
return;
}
PrintTree(pNode->pRight, nLayer + 1);
//不同的层次,用空格来分割
for (int i = 0; i < nLayer; i++)
{
printf(" ");
}
printf("%6d\n", pNode->nData);
PrintTree(pNode->pLeft, nLayer + 1);
}
void CMyAVL::show()
{
PrintTree(m_pRoot, 0);
}
#include "stdafx.h"
#include "MyAVL.h"
#include <iostream>
using std::cout;
using std::endl;
int _tmain(int argc, _TCHAR* argv[])
{
CMyAVL obj;
obj.Insert(50);
obj.Insert(40);
obj.Insert(55);
obj.Insert(35);
obj.Insert(42);
obj.Insert(60);
obj.Insert(52);
obj.Insert(51);
obj.Insert(70);
obj.Insert(53);
obj.show();
cout << "前序遍历" << endl;
obj.PreOrder();
cout << endl;
cout << "中序遍历" << endl;
obj.MidOrder();
cout << endl;
cout <<"后序遍历" << endl;
obj.LastOrder();
cout << endl;
cout << "删除55" << endl;
obj.Erase(55);
obj.show();
cout << "添加54" << endl;
obj.Insert(54);
obj.show();
obj.GetHeight();
cout << "查找35,返回地址" << endl;
cout << obj.Find(35) << endl;
system("pause");
return 0;
}
效果演示(树形图是左边为顶点):