二叉树

此为头文件.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;
}


效果演示(树形图是左边为顶点):


资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在本文中,我们将探讨如何通过 Vue.js 实现一个带有动画效果的“回到顶部”功能。Vue.js 是一款用于构建用户界面的流行 JavaScript 框架,其组件化和响应式设计让实现这种交互功能变得十分便捷。 首先,我们来分析 HTML 代码。在这个示例中,存在一个 ID 为 back-to-top 的 div 元素,其中包含两个 span 标签,分别显示“回到”和“顶部”文字。该 div 元素绑定了 Vue.js 的 @click 事件处理器 backToTop,用于处理点击事件,同时还绑定了 v-show 指令来控制按钮的显示与隐藏。v-cloak 指令的作用是在 Vue 实例渲染完成之前隐藏该元素,避免出现闪烁现象。 CSS 部分(backTop.css)主要负责样式设计。它首先清除了一些默认的边距和填充,对 html 和 body 进行了全屏布局,并设置了相对定位。.back-to-top 类则定义了“回到顶部”按钮的样式,包括其位置、圆角、阴影、填充以及悬停时背景颜色的变化。此外,与 v-cloak 相关的 CSS 确保在 Vue 实例加载过程中隐藏该元素。每个 .page 类代表一个页面,每个页面的高度设置为 400px,用于模拟多页面的滚动效果。 接下来是 JavaScript 部分(backTop.js)。在这里,我们创建了一个 Vue 实例。实例的 el 属性指定 Vue 将挂载到的 DOM 元素(#back-to-top)。data 对象中包含三个属性:backTopShow 用于控制按钮的显示状态;backTopAllow 用于防止用户快速连续点击;backSeconds 定义了回到顶部所需的时间;showPx 则规定了滚动多少像素后显示“回到顶部”按钮。 在 V
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值