【12】二叉树常用算法

【0】二叉树节点

struct MyStruct
{
	int Nodedata = 0;
	MyStruct *pleft = nullptr;
	MyStruct *pright = nullptr;	   
}BTree,*pBTree;

【1】前序遍历

//前序遍历
void qian(MyStruct *proot)
{
	if (proot != nullptr)
	{
		cout << " " << proot->Nodedata;
		if (proot->pleft != nullptr)
		{
			qian(proot->pleft);
		}		
		if (proot->pright != nullptr)
		{
			qian(proot->pright);
		}

	}
}

【2】中序遍历

//中序遍历
void zhong(MyStruct *proot)
{	
	if (proot!=nullptr)
	{
		if (proot->pleft!=nullptr)
		{
			zhong(proot->pleft);
		}
		cout << " " << proot->Nodedata;
		if (proot->pright != nullptr)
		{
			zhong(proot->pright);
		}

	}
}

【3】非递归中序遍历

//非递归中序遍历
void  stackzhong(MyStruct *proot)
{		
	MyStruct * pcurr = proot;//记录根节点
	stack<MyStruct *> mystack;//指针栈
	
	//指针栈和根节点非空则开始循环遍历
	while (!mystack.empty() || pcurr != nullptr)
	{
		//遍历左边的节点,存入指针栈
		while (pcurr != nullptr)
		{
			mystack.push(pcurr);
			pcurr = pcurr->pleft;
		}
		//遍历栈中的元素
		if (!mystack.empty())
		{
			pcurr = mystack.top();
			cout << "  " << pcurr->Nodedata << endl;
			mystack.pop();
			pcurr = pcurr->pright;
		}
	}
}

【4】后序遍历

void hou(MyStruct *proot)
{
	if (proot != nullptr)
	{
		if (proot->pleft != nullptr)
		{
			hou(proot->pleft);
		}		
		if (proot->pright != nullptr)
		{
			hou(proot->pright);
		}
		cout << " " << proot->Nodedata;
	}
}

【5】插入节点

//插入节点
MyStruct * insertnode(MyStruct *proot, int num)
{
	//如果二叉树原来是空树,新建一个节点,存放数据num
	if (proot == nullptr)
	{		
		MyStruct *pnew = new MyStruct;
		pnew->Nodedata = num;
		proot = pnew;
	}
	//如果被存放的值小于根节点,则向左遍历,递归遍历
	else  if (num <= proot->Nodedata)
	{
		proot->pleft = insertnode(proot->pleft, num);

	}
	//如果被存放的值大于根节点,则向右遍历,递归遍历
	else
	{
		proot->pright = insertnode(proot->pright, num);

	}
	return proot;
}

【6】寻找最大值

//寻找二叉树中最大值
//二叉树左边小,右边大,每一颗子二叉树也满足该要求
int findmax(MyStruct *proot)
{
	int max = -99999;
	MyStruct * pcurr = proot;//记录根节点
	MyStruct * mystack[100];//指针数据
	int top = 0;
	//循环条件,深度不为0或者根节点不为空
	while (top != 0 || pcurr != nullptr)
	{
		//如果根节点不为0开始循环
		while (pcurr != nullptr)
		{
			//遍历左边的每一个节点的地址,并放在指针数组中
			//第一次是遍历左子树上的所有左节点
			//第二次是遍历右子树上的所有左节点
			mystack[top++] = pcurr;
			pcurr = pcurr->pleft;
		}
		//如果深度不为0
		if (top > 0)
		{
			//深度从0到top-1,所以top--;
			top--;
			pcurr = mystack[top];
			///cout << "  " << pcurr->Nodedata << endl;
			//如果max小于当前节点,则把当前节点的值赋予max
			//因为是从左子树的最下方最左端开始向上遍历的
			if (max < pcurr->Nodedata)
			{
				max = pcurr->Nodedata;
			}
			//若左节点小于最大值,则判断右节点
			pcurr = pcurr->pright;
		}
	}
	return max;
}

【7】叶子节点个数

//叶子节点
int getyenum(MyStruct *proot)
{
	int left = 0;
	int right = 0;
	//递归终止条件
	if (proot == nullptr)
	{
		return 0;
	}
	if (proot->pleft == nullptr && proot->pright == nullptr)
	{
		return 1;
	}
	//递归调用
	left = getyenum(proot->pleft);
	right = getyenum(proot->pright);
	return left + right;

}

【8】树的高度

//获取树的高度
int  getheight(MyStruct *proot)
{
	int height = 0;
	int left = 0;
	int right = 0;
	if (proot == nullptr)
	{
		return 0;
	}
	left = getheight(proot->pleft);
	right = getheight(proot->pright);
	height = left > right ? left : right;
	return height + 1;
}

【9】获取某个数的父节点

//获取某个值的父节点
int  getba(MyStruct *pRoot, int num)
{
	//【1】递归终止条件
	if (pRoot == nullptr)
	{
		return 0;
	}
	//如果当前节点的左节点不为空,且当前节点的左节点为num,则返回当前节点
	if (pRoot->pleft != nullptr && pRoot->pleft->Nodedata == num)
	{
		return pRoot->Nodedata;
	}
	//如果当前节点的右节点不为空,且当前节点的右节点为num,则返回当前节点
	if (pRoot->pright != nullptr && pRoot->pright->Nodedata == num)
	{
		return pRoot->Nodedata;
	}
	//【2】调用递归
	//递归遍历,直至找到
	getba(pRoot->pleft, num);
	getba(pRoot->pright, num);

}

【10】返回左兄弟

//返回父节点的左节点,即左兄弟
int  getleft(MyStruct *pRoot, int num)
{
	if (pRoot == nullptr)
	{
		return 0;
	}
	if (pRoot->pright && pRoot->pright->Nodedata == num)
	{
		if (pRoot->pleft)
		{
			return  pRoot->pleft->Nodedata;
		}
	}
	getleft(pRoot->pleft,num);
	getleft(pRoot->pright,num);
}

【11】 完整程序

// 二叉树.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include<string>
#include<stack>

using namespace std;

struct MyStruct
{
	int Nodedata = 0;
	MyStruct *pleft = nullptr;
	MyStruct *pright = nullptr;	   
}BTree,*pBTree;

int main()
{
	MyStruct *pRoot;
	MyStruct s1;
	MyStruct s2;
	MyStruct s3;
	MyStruct s4;
	MyStruct s5;
	MyStruct s6;
	MyStruct s7;

	pRoot = &s1;
	s1.Nodedata =1;
	s2.Nodedata =2;
	s3.Nodedata =3;
	s4.Nodedata =4;
	s5.Nodedata =5;
	s6.Nodedata =6;
	s7.Nodedata =7;

	s1.pleft = &s2;
	s1.pright = &s3;
	s2.pleft = &s4;
	s2.pright = &s5;
	s3.pleft = &s6;
	s3.pright = &s7;

	cout << "前序遍历二叉树" << endl;
	qian(pRoot);
	cout << endl;
	cout << "中序遍历二叉树" << endl;
	zhong(pRoot);
	cout << endl;
	cout << "后序遍历二叉树" << endl;
	hou(pRoot);
	cout << endl;


	MyStruct * pRootBack=insertnode(pRoot,17);
	cout << "插入节点后序遍历二叉树" << endl;
	hou(pRoot);
	cout << endl;

	int maxnum = findmax(pRoot);
	cout<<"二叉树中的最大值:"<< maxnum<<endl;
	cin.get();
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章