【C/C++学院】0906-递归转栈/二叉树实现

简介: <p></p> <h2><span style="font-family:宋体; font-size:16pt">递归转栈</span><span style="font-family:宋体; font-size:16pt"></span></h2> <p></p> <p>用栈实现递归<span style="font-family:Calibri">.cpp</span></p

递归转栈

用栈实现递归.cpp

#include<stack>
#include <iostream>

using namespace std;

int printN(int n)
{
	if (n>0)
	{
		cout << n;
		return printN(n - 1);
	}
}

void printNS_shunxu(int n)
{
	stack<int>  mystack;
AAA:
	if (n > 0)
	{
		mystack.push(n);
		while (!mystack.empty())
		{
			cout << mystack.top();
			mystack.pop();
		}

		n -= 1;
	    goto AAA;
	}
}

void printNS_nixu(int n)
{
	stack<int>  mystack;
AAA:
	if (n > 0)
	{
		mystack.push(n);	

		n -= 1;
		goto AAA;
	}
	while (!mystack.empty())
	{
		cout << mystack.top();
		mystack.pop();
	}
}

int get100(int i)
{
	if (!i)
	{
		return 0;
	} 
	else
	{
		return get100(i - 1) + i;
	}
}


int getN(int i)
{
	stack<int>  mystack;
	int res = 0;
AA:	if (i)
	{
		mystack.push(i);

		i--;
		goto AA;
	}
	while (!mystack.empty())
	{
		res += mystack.top();
		mystack.pop();
	}
	return res;
}

void to2(int num)
{
	if (num)
	{
		cout << num % 2;
		to2(num / 2);	
	}
}

void mainA ()
{
	//cout << get100(100) << endl;
	printNS_nixu(9);
    printNS_shunxu(9);

	cout<<"\n"<<getN(100)<<"\n";

	to2(10);
	cin.get();	
}

双层递归转栈.cpp

#include<iostream>
#include <stack>

using namespace std;

int getF(int n)
{
	if (n==1 ||n==2)
	{
		return 1;
	} 
	else
	{
		return getF(n - 1) + getF(n - 2);
	}
}

int GetFF(int n)
{
	int  *p = new int[n];
	p[0] = p[1] = 1;
	for (int i = 2; i < n;i++)
	{
		p[i] = p[i - 1] + p[i - 2];
	}
	return p[n - 1];
}

int GetFFF(int n)
{
	stack<int>  mystack;

	int f1, f2, f3;
	f1 = f2 = 1;
	int i = 2;
	ABC:if (i<n)
	{		
		mystack.push(f1);
		mystack.push(f2);
		f3 = 0;
		while (!mystack.empty())
		{
				f3+= mystack.top();
				mystack.pop();
		}
		//f3 = f2 + f1;
		f1 = f2;//轮替
		f2 = f3;

		i++;
		goto ABC;
	}
	return  f3;
}

int GetFFFF(int n)
{
	int f1, f2, f3;
	f1 = f2 = 1;
	for (int i = 2; i < n; i++)
	{
		f3 = f2 + f1;
		f1 = f2;//轮替
		f2 = f3;
	}
	return  f3;
}


void mainFG()
{
	cout << getF(10) << endl;
	cout << GetFF(10) << endl;
	cout << GetFFF(10) << endl;

	cin.get();
}

栈模拟递归函数调用.cpp

#include<iostream>
#include <stack>

//递归,有顺序,逆序,栈吃一个吐一个,顺序,一次吃完再吐,逆序
//递归,数据保留中间结果,函数指针保留操作
//汉诺塔,斐波那契数列  递归计算表达式  ,栈,(熊飞,3人做一题)
//谭胜,汉诺塔,李桂龙,斐波那契 ,柳益民

using namespace std;
struct datas
{
	int n;
	void(*p)(int);
};

void printN(int n)
{
	if (n > 0)
	{
		cout << n;
		return printN(n - 1);
	}
}

void print(int n)
{
	cout << n;
}


//1+100
void printall(int n)
{
	stack<datas>  mystack;
AAA:
	if (n > 0)
	{
		datas s1;
		s1.n = n;
		s1.p = print;
		mystack.push(s1);
		while (!mystack.empty())
		{
			datas stemp = mystack.top();
			stemp.p(stemp.n);
			mystack.pop();
		}

		n -= 1;
		goto AAA;
	}
}

void main()
{
	printall(10);

	cin.get();
}

二叉树实现

#include<iostream>
#include <string>
#include <stack>

using namespace std;

struct MyStruct
{
	int Nodedata=0;
	MyStruct *pLeft=nullptr;
	MyStruct *pRight = nullptr;

}BTree,*pBTree;

//中序,前序,后序,递归遍历,非递归遍历
//查找,修改,  删除,插入,排序
MyStruct * insertnode(MyStruct *proot,int 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;
}

int findmax(MyStruct *proot)
{
	int max = -99999;

	MyStruct * pcurr = proot;//记录根节点
	MyStruct * mystack[100];//指针数据
	int top = 0;
	while (top != 0 || pcurr != nullptr)
	{
		while (pcurr != nullptr)
		{
			mystack[top++] = pcurr;

			pcurr = pcurr->pLeft;
		}
		if (top > 0)
		{
			top--;
			pcurr = mystack[top];
			///cout << "  " << pcurr->Nodedata << endl;

			if (max< pcurr->Nodedata)
			{
				max = pcurr->Nodedata;
			}
			pcurr = pcurr->pRight;
		}
	}
	return max;
}

void zhong(MyStruct *proot)
{
	if (proot!=nullptr)
	{
		if (proot->pLeft!=nullptr)
		{
			zhong(proot->pLeft);
		}
		cout << " " << proot->Nodedata << endl;
		if (proot->pRight!= nullptr)
		{
			zhong(proot->pRight);
		}
	}
}

void  stackzhong(MyStruct *proot)
{
	//stack<MyStruct> mystack;

	MyStruct * pcurr = proot;//记录根节点
	MyStruct * mystack[100];//指针数据
	int top = 0;
	 while ( top!=0 || pcurr !=nullptr)
	 {
		 while (pcurr != nullptr)
		 {
			 mystack[top++] = pcurr;
			 pcurr = pcurr->pLeft;
		 }
		 if (top>0)
		 {
			 top--;
			 pcurr = mystack[top];
			 cout << "  " << pcurr->Nodedata << endl;
			 pcurr = pcurr->pRight;
		 }
	 }
}

void  stackzhongA(MyStruct *proot)
{
	//stack<MyStruct> mystack;
	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;
		}
	}
}

void  show(MyStruct *proot  ,int n)
{
	if (proot==nullptr)
	{
		return;
	} 
	else
	{
		show(proot->pLeft, n + 1);
		for (int i = 0; i < n;i++)
		{
			cout << "   ";
		}
		cout << proot->Nodedata << endl;
		show(proot->pRight, n + 1);
	}
}

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;
}

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;
}



void   ceng(MyStruct *proot)
{
	if (proot ==nullptr)
	{
		return;
	}
	MyStruct * myq[100];
	int tou = 0;
	int wei = 0;
	MyStruct * pcurr = nullptr;
	
	myq[wei++] = proot;//存入队列第一个节点,入队
	while (tou !=wei)
	{
		pcurr = myq[tou];
		tou++;//出队
		cout << pcurr->Nodedata << endl;

		if (pcurr->pLeft!=nullptr)
		{
			myq[wei++] = pcurr->pLeft;//入队
		}
		if (pcurr->pRight != nullptr)
		{
			myq[wei++] = pcurr->pRight;//入队
		}
	}
}


void mainA()
{
	MyStruct *pRoot;//根
	MyStruct sarray[100];
	pRoot = sarray;
	//0  1  2  3 4   --99
	//
	for (int i = 1; i <= 100;i++)
	{
		sarray[i].Nodedata = i;
	}
	//2 * i + 2<<99
	for (int i = 0; i <= 50;i++)
	{
		if (i<=(99-1)/2)
		{
			sarray[i].pLeft = &sarray[2 * i + 1];
		}
		if (i<=(99-2)/2)
		{
			sarray[i].pRight = &sarray[2 * i + 2];
		}	
	}

	show(pRoot, 1);
	cin.get();
}

int  getba(MyStruct *pRoot,int num)
{
	if (pRoot==nullptr)
	{
		return 0;
	}
	if (pRoot->pLeft!=nullptr && pRoot->pLeft->Nodedata==num)
	{
		return pRoot->Nodedata;
	}
	if (pRoot->pRight != nullptr && pRoot->pRight->Nodedata == num)
	{
		return pRoot->Nodedata;
	}
	getba(pRoot->pLeft, num);
	getba(pRoot->pRight, num);
}

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

void main213213()
{
	MyStruct *pRoot;//根

	MyStruct s1;
	MyStruct s2;
	MyStruct s3;
	MyStruct s4;
	MyStruct s5;
	MyStruct s6;
	MyStruct s7;
	MyStruct s8;
	pRoot = &s1;
	s1.Nodedata = 1;
	s2.Nodedata = 2;
	s3.Nodedata = 3;
	s4.Nodedata = 4;
	s5.Nodedata = 5;
	s6.Nodedata = 16;
	s7.Nodedata = 7;
	s8.Nodedata = 8;
	s1.pLeft = &s2;
	s1.pRight = &s3;

	s2.pLeft = &s4;
	s2.pRight = &s5;

	s3.pLeft = &s6;
	s3.pRight = &s7;

	cout << findmax(pRoot) << endl;


	cin.get();
}

void mainasds()
{
	MyStruct *pRoot;//根

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

	s2.pLeft = &s4;
	s2.pRight = &s5;

	s3.pLeft = &s6;
	s3.pRight = &s7;

	//s4.pLeft = &s8;
	ceng(pRoot);
	cout << "\n\n\n\n\n\n\n";
	cout << getyenum(pRoot)<<"\n\n\n";
	cout << getheight(pRoot) << "\n\n\n";
	//show(pRoot, 1);
	zhong(pRoot);


	cout << "\n\n\n\n";
	stackzhong(pRoot);

	cout << "\n\n\n\n";
	stackzhongA(pRoot);
	cin.get();
}

void main()
{
	MyStruct *pRoot=nullptr;//根
	for (int i = 6; i < 10; i++)
	{
		pRoot = insertnode(pRoot, i);
	}
	for (int i = 5; i >=0; i--)
	{
		pRoot = insertnode(pRoot, i);
	}
	show(pRoot, 1);
	cin.get();
}






目录
相关文章
基本二叉树与排序二叉树(C++源码)
本程序实现二叉树基本操作与二叉排序树应用。支持前序建树、四种遍历、求深度、叶子数、第K层节点数及查找功能;并实现二叉排序树的构建、中序输出与查找比较次数统计,分析不同插入顺序对树形态和查找效率的影响。
|
算法 C++
算法笔记:递归(c++实现)
算法笔记:递归(c++实现)
347 1
|
存储 C语言 C++
【C++数据结构——栈与队列】顺序栈的基本运算(头歌实践教学平台习题)【合集】
本关任务:编写一个程序实现顺序栈的基本运算。开始你的任务吧,祝你成功!​ 相关知识 初始化栈 销毁栈 判断栈是否为空 进栈 出栈 取栈顶元素 1.初始化栈 概念:初始化栈是为栈的使用做准备,包括分配内存空间(如果是动态分配)和设置栈的初始状态。栈有顺序栈和链式栈两种常见形式。对于顺序栈,通常需要定义一个数组来存储栈元素,并设置一个变量来记录栈顶位置;对于链式栈,需要定义节点结构,包含数据域和指针域,同时初始化栈顶指针。 示例(顺序栈): 以下是一个简单的顺序栈初始化示例,假设用C语言实现,栈中存储
1438 77
|
存储 设计模式 C语言
C++中的栈和队列
C++中的栈和队列
286 0
|
存储 C++ 索引
【C++数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】
【数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】初始化队列、销毁队列、判断队列是否为空、进队列、出队列等。本关任务:编写一个程序实现环形队列的基本运算。(6)出队列序列:yzopq2*(5)依次进队列元素:opq2*(6)出队列序列:bcdef。(2)依次进队列元素:abc。(5)依次进队列元素:def。(2)依次进队列元素:xyz。开始你的任务吧,祝你成功!(4)出队一个元素a。(4)出队一个元素x。
710 13
【C++数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】
|
Java C++
【C++数据结构——树】二叉树的基本运算(头歌实践教学平台习题)【合集】
本关任务:编写一个程序实现二叉树的基本运算。​ 相关知识 创建二叉树 销毁二叉树 查找结点 求二叉树的高度 输出二叉树 //二叉树节点结构体定义 structTreeNode{ intval; TreeNode*left; TreeNode*right; TreeNode(intx):val(x),left(NULL),right(NULL){} }; 创建二叉树 //创建二叉树函数(简单示例,手动构建) TreeNode*create
758 12
【C++数据结构——树】二叉树的性质(头歌实践教学平台习题)【合集】
本文档介绍了如何根据二叉树的括号表示串创建二叉树,并计算其结点个数、叶子结点个数、某结点的层次和二叉树的宽度。主要内容包括: 1. **定义二叉树节点结构体**:定义了包含节点值、左子节点指针和右子节点指针的结构体。 2. **实现构建二叉树的函数**:通过解析括号表示串,递归地构建二叉树的各个节点及其子树。 3. **使用示例**:展示了如何调用 `buildTree` 函数构建二叉树并进行简单验证。 4. **计算二叉树属性**: - 计算二叉树节点个数。 - 计算二叉树叶子节点个数。 - 计算某节点的层次。 - 计算二叉树的宽度。 最后,提供了测试说明及通关代
484 10
|
存储 C语言 C++
【C++数据结构——栈与队列】链栈的基本运算(头歌实践教学平台习题)【合集】
本关任务:编写一个程序实现链栈的基本运算。开始你的任务吧,祝你成功!​ 相关知识 初始化栈 销毁栈 判断栈是否为空 进栈 出栈 取栈顶元素 初始化栈 概念:初始化栈是为栈的使用做准备,包括分配内存空间(如果是动态分配)和设置栈的初始状态。栈有顺序栈和链式栈两种常见形式。对于顺序栈,通常需要定义一个数组来存储栈元素,并设置一个变量来记录栈顶位置;对于链式栈,需要定义节点结构,包含数据域和指针域,同时初始化栈顶指针。 示例(顺序栈): 以下是一个简单的顺序栈初始化示例,假设用C语言实现,栈中存储整数,最大
511 9
【C++数据结构——栈和队列】括号配对(头歌实践教学平台习题)【合集】
【数据结构——栈和队列】括号配对(头歌实践教学平台习题)【合集】(1)遇到左括号:进栈Push()(2)遇到右括号:若栈顶元素为左括号,则出栈Pop();否则返回false。(3)当遍历表达式结束,且栈为空时,则返回true,否则返回false。本关任务:编写一个程序利用栈判断左、右圆括号是否配对。为了完成本关任务,你需要掌握:栈对括号的处理。(1)遇到左括号:进栈Push()开始你的任务吧,祝你成功!测试输入:(()))
750 7
|
存储 算法 测试技术
【C++数据结构——树】二叉树的遍历算法(头歌教学实验平台习题) 【合集】
本任务旨在实现二叉树的遍历,包括先序、中序、后序和层次遍历。首先介绍了二叉树的基本概念与结构定义,并通过C++代码示例展示了如何定义二叉树节点及构建二叉树。接着详细讲解了四种遍历方法的递归实现逻辑,以及层次遍历中队列的应用。最后提供了测试用例和预期输出,确保代码正确性。通过这些内容,帮助读者理解并掌握二叉树遍历的核心思想与实现技巧。
916 3