二叉树结构常用操作
来源:互联网 发布:淘宝居家日用类目 编辑:程序博客网 时间:2024/06/06 01:17
我写的是不带头节点的二叉树链式结构。
#include<iostream>
#include<queue>
using namespace std;
#include<queue>
using namespace std;
#define maxn 1005
int a[maxn],b[maxn];
struct Tree
{
int v;
Tree* lc,*rc;
};
queue<Tree *> q;
Tree* root;
int a[maxn],b[maxn];
struct Tree
{
int v;
Tree* lc,*rc;
};
queue<Tree *> q;
Tree* root;
int max(int x,int y)
{
return x>y?x:y;
}
{
return x>y?x:y;
}
Tree* build(int *a,int *b,int n)
{
int i;
Tree* ss;
for(i=0;i<n;i++)
{
if(a[0]==b[i])
{
ss=(Tree* )malloc(sizeof(Tree));
ss->v=b[i];
ss->lc=build(a+1,b,i);
ss->rc=build(a+i+1,b+i+1,n-i-1);
return ss;
}
}
return NULL;
}
{
int i;
Tree* ss;
for(i=0;i<n;i++)
{
if(a[0]==b[i])
{
ss=(Tree* )malloc(sizeof(Tree));
ss->v=b[i];
ss->lc=build(a+1,b,i);
ss->rc=build(a+i+1,b+i+1,n-i-1);
return ss;
}
}
return NULL;
}
void xianxu_bianli(Tree* T)
{
if(T==NULL) return ;
cout<<T->v;
xianxu_bianli(T->lc);
xianxu_bianli(T->rc);
}
{
if(T==NULL) return ;
cout<<T->v;
xianxu_bianli(T->lc);
xianxu_bianli(T->rc);
}
void zhongxu_bianli(Tree* T)
{
if(T==NULL) return ;
zhongxu_bianli(T->lc);
cout<<T->v;
zhongxu_bianli(T->rc);
}
{
if(T==NULL) return ;
zhongxu_bianli(T->lc);
cout<<T->v;
zhongxu_bianli(T->rc);
}
void houxu_bianli(Tree* T)
{
if(T==NULL) return;
houxu_bianli(T->lc);
houxu_bianli(T->rc);
cout<<T->v<<' ';
}
{
if(T==NULL) return;
houxu_bianli(T->lc);
houxu_bianli(T->rc);
cout<<T->v<<' ';
}
void bfs(Tree* T)
{
if(!T) return;
q.push(T);
while(!q.empty())
{
Tree *u=q.front();
q.pop();
cout<<u->v;
if(u->lc!=NULL) q.push(u->lc);
if(u->lc!=NULL) q.push(u->rc);
}
}
{
if(!T) return;
q.push(T);
while(!q.empty())
{
Tree *u=q.front();
q.pop();
cout<<u->v;
if(u->lc!=NULL) q.push(u->lc);
if(u->lc!=NULL) q.push(u->rc);
}
}
int Nodecount(Tree* T)
{
if(T==NULL) return 0;
return Nodecount(T->lc)+Nodecount(T->rc)+1;
}
{
if(T==NULL) return 0;
return Nodecount(T->lc)+Nodecount(T->rc)+1;
}
int Treedepth(Tree *T)
{
if(T==NULL) return 0;
return max(Treedepth(T->lc),Treedepth(T->rc))+1;
}
{
if(T==NULL) return 0;
return max(Treedepth(T->lc),Treedepth(T->rc))+1;
}
void remove_tree(Tree* T)
{
if(T==NULL) return;
remove_tree(T->lc);
remove_tree(T->rc);
delete(T);
T=NULL;
}
{
if(T==NULL) return;
remove_tree(T->lc);
remove_tree(T->rc);
delete(T);
T=NULL;
}
void copy_tree(Tree* T,Tree *NT)
{
if(!T) {NT=NULL;return;}
NT->v=T->v;
copy_tree(T->lc,NT->lc);
copy_tree(T->rc,NT->rc);
}
{
if(!T) {NT=NULL;return;}
NT->v=T->v;
copy_tree(T->lc,NT->lc);
copy_tree(T->rc,NT->rc);
}
Tree* translate(Tree* T) //交换左右子树
{
if(!T||(T->lc==NULL&&T->rc==NULL))
return T;
else
{
Tree* p;
p=T->lc;
T->lc=T->rc;
T->rc=p;
T->lc=translate(T->rc);
T->rc=translate(T->lc);
return T;
}
}
{
if(!T||(T->lc==NULL&&T->rc==NULL))
return T;
else
{
Tree* p;
p=T->lc;
T->lc=T->rc;
T->rc=p;
T->lc=translate(T->rc);
T->rc=translate(T->lc);
return T;
}
}
int main()
{
int n,i;
{
int n,i;
while(scanf("%d",&n)!=0)
{
for(i=0;i<n;i++)
{
cin>>a[i];
}
{
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
cin>>b[i];
}
root=build(a,b,n);
houxu_bianli(root);
}
return 0;
}
{
cin>>b[i];
}
root=build(a,b,n);
houxu_bianli(root);
}
return 0;
}
阅读全文
0 0
- 二叉树结构常用操作
- 二叉树常用操作
- 二叉树常用操作
- 二叉树的常用操作
- 二叉树的常用操作
- 二叉树的常用操作
- 常用的二叉树操作
- 用C语言实现二叉树的结构和常用操作
- 二叉搜索树常用操作函数
- 二叉树的常用操作(C++实现)
- 二叉查找树常用操作(接上篇)
- 二叉树的遍历及常用操作
- 二叉搜索树的常用操作
- C++二叉树结构的建立和操作
- 程序实现二叉树的结构及其操作
- 程序实现线索二叉树的结构及其操作
- C++二叉树结构的建立与基本操作
- 严蔚敏 数据结构 二叉树链式存储结构 遍历等操作
- 工作中遇到的问题
- 将项目上传到GitHub笔记
- EventBus的传值
- Android TextUtils.isEmpty()的误解
- 洛谷 P1439 【模板】最长公共子序列
- 二叉树结构常用操作
- Stata Editor for Sublime Text 3
- Java方法
- 关于python调用C++
- IIO子系统文档 ring.txt 翻译
- 我的知乎专栏与公众号
- splay操作集合
- 随机生成个数50和20的字符串分别存在file1,file2中并将在file1中出现且不在file2中的字符存在flie3中
- 无限轮播1