【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();
}


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