二叉树创建、删除、(递归/非递归)先序(中序/后序)遍历

本文详细介绍了递归和非递归遍历二叉树的方法,包括前序、中序和后序遍历,并提供了具体的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<pre name="code" class="cpp">递归遍历树很容易实现,非递归前序和中序遍历也还简单,使用栈结构即可解决问题,稍微难点的是非递归后序遍历,具体实现如下:

#include <iostream>
#include <stack> 
using namespace std;
typedef struct Node{
	int data;
	struct Node* pLeft;
	struct Node* pRight;
}*pNode,node;

typedef struct BitTree{
	pNode* pRoot;
}*pBitTree;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

pNode createNode(int data)
{
	pNode pNewNode = new Node();
	pNewNode->data = data;
	pNewNode->pLeft = NULL;
	pNewNode->pRight = NULL;
	return pNewNode;
}

pBitTree* createBitTree()
{
	pBitTree pNewBTree = new BitTree();
	
	pNode root = new Node();
	root->data = 17;
	root->pLeft = NULL;
	root->pRight = NULL;
	pNewBTree->pRoot = &root;
	                                         /******17********************/
	pNode node1 = createNode(18);            //****/    \*******/
	root->pLeft = node1;                    //****/      \******/
	pNode node2 = createNode(19);          //****18      19*****/ 
	root->pRight = node2;                 //*****/ \    /  \****/ 
	                                     //*****20 21  22   23**/
	pNode node3 = createNode(20);       //*****/    \  /********/
	node1->pLeft = node3;              //*****24   25 26*****/
	pNode node4 = createNode(21);
	node1->pRight = node4;
	
	pNode node5 = createNode(22);
	node2->pLeft = node5;
	pNode node6 = createNode(23);
	node2->pRight = node6;
	
	pNode node7 = createNode(24);
	node4->pLeft = node7;
	
	pNode node8 = createNode(25);
	node5->pRight = node8;
	
	pNode node9 = createNode(26);
	node6->pLeft = node9;
	
	return &pNewBTree;
}
//递归先序遍历 
void recursivePreOrderTraversal(pNode root)
{
	if(root == NULL)
		return;
	cout << root->data << " ";
	recursivePreOrderTraversal(root->pLeft);
	recursivePreOrderTraversal(root->pRight);
}
//递归中序遍历 
void recursiveInOrderTraversal(pNode root)
{
	if(root == NULL)
		return;
	recursiveInOrderTraversal(root->pLeft);
	cout << root->data << " ";
	recursiveInOrderTraversal(root->pRight);
}
//递归后序遍历 
void recursiveAreOrderTraversal(pNode root)
{
	if(root == NULL)
		return;
	recursiveAreOrderTraversal(root->pLeft);
	recursiveAreOrderTraversal(root->pRight);
	cout << root->data << " ";
}
//非递归先序遍历 
void nonRecursivePreOrderTraversal(pNode root)
{
	if(root == NULL)
		return;
	stack<pNode> nodeStack;
	pNode curNode = root;
	while(curNode != NULL || !nodeStack.empty())
	{	
		while(curNode != NULL)
		{
			cout << curNode->data << " ";
			nodeStack.push(curNode);
			curNode = curNode->pLeft;	
		}
		curNode = nodeStack.top();
		nodeStack.pop();
		curNode = curNode->pRight; 
	} 
}

//删除树结点
void deleteNode(pNode theNode)
{
	if(theNode->pLeft != NULL)
		deleteNode(theNode->pLeft);
	if(theNode->pRight != NULL)
		deleteNode(theNode->pRight);
	if(theNode->pLeft == NULL && theNode->pRight == NULL)
	{
		delete(theNode);
		theNode = NULL;
	}
} 
//删除树结构
void deleteBitTree(pBitTree theBitTree)
{
	if(theBitTree == NULL)
		return;
	if(*theBitTree->pRoot == NULL)
		return;
	deleteNode(*theBitTree->pRoot);
	delete theBitTree;
} 
//非递归中序遍历 
void nonRecursiveInOrderTraversal(pNode root)
{
	if(root == NULL)
		return;
 	stack<pNode> nodeStack;
    pNode curNode = root;
	while(curNode != NULL || !nodeStack.empty())
	{
		while(curNode != NULL)
		{
			nodeStack.push(curNode);
			curNode = curNode->pLeft;
		}
		curNode = nodeStack.top();
		nodeStack.pop();
		cout << curNode->data << " ";
		curNode = curNode->pRight;
	}		
}
//非递归后序遍历 
void nonRecursiveAreOrderTraversal(pNode root)  
{
	pNode preVisited = NULL;
	if(root == NULL)
		return;
	stack<pNode> nodeStack;
	pNode curNode = root;
	while(curNode != NULL || !nodeStack.empty())
	{
		while(curNode != NULL)
		{
			nodeStack.push(curNode);
			curNode = curNode->pLeft;		
		}
		curNode = nodeStack.top();
		curNode = curNode->pRight;
		if(curNode == NULL || curNode == preVisited)  //此时,左子树已经为空,若右子树还为空则输出该结点,或者 
		{                                             //该结点的左子树和右子树均访问过,则输出该结点 
			curNode = nodeStack.top();
			cout << curNode->data << " ";
			preVisited = curNode;
			nodeStack.pop();
			curNode = NULL;
		} 
	}
}

int main(int argc, char *argv[]) {
	pBitTree* theBitTree = createBitTree();
	pNode root = *(*theBitTree)->pRoot;
	cout << "递归前序遍历:";
	recursivePreOrderTraversal(root);
	cout << endl;
	cout << "递归中序遍历:";
	recursiveInOrderTraversal(root);
	cout << endl;
	cout << "递归后序遍历:";
	recursiveAreOrderTraversal(root);
	cout << endl;
	cout << "非递归前序遍历:";
	nonRecursivePreOrderTraversal(root);
	cout << endl;
	cout << "非递归中序遍历:";
	nonRecursiveInOrderTraversal(root);
	cout << endl;
	cout << "非递归后序遍历:";
	nonRecursiveAreOrderTraversal(root);
	cout << endl;
	deleteBitTree(*theBitTree);
	
	return 0;
}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值