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