二叉树的创建搜索以及遍历,简单易懂

包括了前中后递归和迭代写法,以及层序遍历分组输出,还有搜索修改子串,求深度节点数叶子节点数
有时间再写一个二叉搜索树的版本,然后再把这个版本写好看点

#include<bits/stdc++.h>
using namespace std; 
//前 中 后序 层序遍历二叉树 创建,搜索修改二叉树 
struct TreeNode{
	char val;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int x):val(x),left(NULL),right(NULL){}
};



//找到x值将其修改为newV值 
void search(TreeNode* &root,char x,char newV)
{
	if(!root) return;
	if(root->val==x)
	{
		root->val=newV;
		cout<<"修改成功"<<endl;
	}
	cout<<"x:"<<root->val<<endl;
	search(root->left,x,newV);
	search(root->right,x,newV);
}



TreeNode* Create(TreeNode* &root)
{
	char c;
	cin>> c;
	if(c=='#')
	{
		root=NULL;
	}
	else
	{
		root =new TreeNode(c);
		Create(root->left);
		Create(root->right);
	}
}
//前序遍历递归写法
//中 左 右
void Pre(TreeNode* root)
{
	cout<<root->val<<" ";
	if(root->left) Pre(root->left);
	if(root->right) Pre(root->right);
}

//前序迭代 
void PreN(TreeNode* root)
{
	stack<TreeNode*> st;
	st.push(root);
	while(!st.empty())
	{
		TreeNode* node=st.top();
		st.pop();
		cout<<node->val<<" ";
		if(node->right) st.push(node->right);
		if(node->left) st.push(node->left);
	}
}

//中序递归
void Mid(TreeNode* root)
{
	if(root->left) Mid(root->left);
	cout<<root->val<<" ";
	if(root->right) Mid(root->right);
} 

//中序非递归
void MidN(TreeNode* node)
{
	stack<TreeNode*>st;
	TreeNode* root=node;
	while(!st.empty()||root)
	{
		//遍历到最左结点 压入这些结点 
		while(root)
		{
			st.push(root);
			root=root->left;
		}
        //如果st不为空 弹出top 然后走右结点,
		//如果右结点不为空 则继续找到右结点的最左结点 重复上述步骤
		//如果右结点为空 则不操作 继续弹出 
		if(!st.empty())
		{
			root=st.top();
			cout<<root->val<<" ";
			st.pop();
			root=root->right;
		}
	}
} 

//层序遍历
void level(TreeNode* root)
{
	queue<TreeNode*>que;
	que.push(root);
	while(!que.empty())
	{
		TreeNode* node=que.front();
		cout<<node->val<<" ";
		que.pop();
		if(node->left) que.push(node->left);
		if(node->right) que.push(node->right);
	}
} 
//层序遍历分组输出
vector<vector<char> >levelGroup(TreeNode* root)
{
	queue<TreeNode*>que;
	que.push(root);
	vector<vector<char> >ans;
	while(!que.empty())
	{
		vector<char> temp;
		int len=que.size();
		for(int i=0;i<len;i++)
		{
			TreeNode* node=que.front();
			temp.push_back(node->val);
			que.pop();
			if(node->left) que.push(node->left);
			if(node->right) que.push(node->right);
		}

		ans.push_back(temp);
	}
	return ans;
} 

//后序递归 
void Aft(TreeNode* root)
{
	if(root->left) Aft(root->left);
	if(root->right) Aft(root->right);
	cout<<root->val<<" ";
}

//后序迭代
void AftN(TreeNode* root)
{
	stack<TreeNode*>st;
	st.push(root);
	vector<char>temp;
	while(!st.empty())
	{
		TreeNode* node=st.top();
		temp.push_back(node->val);
		st.pop();
		if(node->left) st.push(node->left);
		if(node->right) st.push(node->right);
	}	
	reverse(temp.begin() ,temp.end());
	for(int i=0;i<temp.size();i++)
	{
		cout<<temp[i]<<" ";
	}
} 
void printDoubleV(vector<vector<char> >temp)
{
	for(int i=0;i<temp.size();i++)
	{
		for(int j=0;j<temp[i].size();j++)
		{
			cout<<temp[i][j]<<" ";
		}
		cout<<endl;
	}
}

int NodeNum=0;
int leaf=0;
int depth=0;
//采用后序遍历求二叉树的深度、结点数及叶子数的递归算法
int TreeDepth(TreeNode* T)
{   
	int hl,hr,max;
    if(T)
	{  
		hl=TreeDepth(T->left);   	//求左深度
		hr=TreeDepth(T->right);    //求右深度
		max=hl>hr? hl:hr;           //取左右深度的最大值
		NodeNum=NodeNum+1;         //求结点数.
		if(hl==0&&hr==0) leaf=leaf+1;  //若左右深度为0,即为叶子。
		return(max+1);
	}
		else return(0);
}
int main() 
{
	//124##5##36##7##
	TreeNode* tree;
	Create(tree);
	cout<<"前序遍历:";
	PreN(tree);
	cout<<endl;
	cout<<"中序遍历:";
	MidN(tree);
	cout<<endl;
	cout<<"后序遍历:";
	AftN(tree);
	cout<<endl;
	cout<<"每一层层序遍历:";
	vector<vector<char> >temp;
	temp=levelGroup(tree);
	printDoubleV(temp);
	cout<<endl;
	search(tree,'7','8');
	cout<<"每一层层序遍历:";
//	vector<vector<char> >temp;
	temp=levelGroup(tree);
	printDoubleV(temp);

	depth=TreeDepth(tree);
	cout<<"深度:"<<depth<<"节点数"<<NodeNum<<"叶子数"<<leaf<<endl;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值