Adament Cabin

  • 计算机
    • Algorithm
    • Linux
    • Problems
    • AI
  • 日语学习
  • ACG
    • 追番列表
    • 游戏
  • 常日说
  • 杂物
    • 资料
    • 资源
    • 治疗中心
  • ao的链接
  • 分站
  • Chinese
    • Japanese
Adament Cabin
恋厨综合症第二治疗中心
  1. 首页
  2. 计算机
  3. Algorithm
  4. 正文

二叉树的一些遍历模板

2022年4月21日 1951点热度 1人点赞 1条评论

这是一棵二叉树

struct tree{
	int data;
	tree * lchild;
	tree * rchild;
};

遍历二叉树:

void printtree_f(tree *BT){ //前序遍历
	if(!BT) return;
	else{
		cout << BT->data << " ";
		printtree_f(BT->lchild);
		printtree_f(BT->rchild);
	}
}

void printtree_m(tree *BT){ //中序遍历
	if(!BT) return;
	else{
		printtree_m(BT->lchild);
		cout << BT->data << " ";
		printtree_m(BT->rchild);
		
	}
}

void printtree_b(tree *BT){ //后序遍历
	if(!BT) return;
	else{
		printtree_b(BT->lchild);
		printtree_b(BT->rchild);
		cout << BT->data << " ";
	}
}

void printtree_s(tree *BT){ //按层遍历
	queue<tree*> vec;
	vec.push(BT);
	while(!vec.empty()){
		tree * top = vec.front();
		if(top->lchild!=NULL) vec.push(top->lchild);
		if(top->rchild!=NULL) vec.push(top->rchild);
		cout << top->data << " ";
		vec.pop();
	}
	return;
}

先序遍历和中序遍历构建二叉树:

tree * createtree(int prestart,int preend,int instart,int inend,int pre[],int in[]){
	tree * BT = new tree;
	if(prestart>preend||instart>inend) return NULL;
	if(prestart==preend&&instart==inend){
		BT->lchild=NULL;
		BT->rchild=NULL;
		BT->data=pre[prestart];
		return BT;
	}
	int front=pre[prestart];int i;
	for(i=0;i<n;i++) if(in[i]==front) break;
	int partleft=i-instart;
	int partright=inend-i;
	BT->data=front;
	BT->lchild=createtree(prestart+1,prestart+partleft,instart,i-1,pre,in);
	BT->rchild=createtree(prestart+1+partleft,prestart+partleft+partright,i+1,i+partright,pre,in);
	return BT;
}

后序和中序构建二叉树:

tree * createfront(int postfront,int postend,int infront,int inend,int post[],int in[]){
	if(postfront>postend||infront>inend) return NULL;
	if(postfront==postend&&infront==inend){
		tree * leave=new tree;
		leave->data=post[postend];
		leave->lchild=NULL;
		leave->rchild=NULL;
		return leave;
	}
	int chroot=post[postend];
	int i;
	for(i=postfront;i<=postend;i++) if(in[i]==chroot) break;
	tree * BT=new tree;
	BT->data=chroot;
	BT->lchild=createfront(postfront,postfront+i-infront-1,infront,i-1,post,in);
	BT->rchild=createfront(postend+i-inend,postend-1,i+1,inend,post,in);
	return BT;
}
标签: C/C++/C#
最后更新:2022年8月10日

BiyiAdopac

是铁龙鸣。

点赞
< 上一篇
下一篇 >

文章评论

  • 匿名

    6的

    2023年5月25日
    回复
  • razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
    取消回复

    文章目录
    • 遍历二叉树:
    • 先序遍历和中序遍历构建二叉树:
    • 后序和中序构建二叉树:
    分类
    • AI / 1篇
    • Algorithm / 5篇
    • Linux / 6篇
    • Problems / 3篇
    • 常日说 / 1篇
    • 日本語 / 2篇
    • 游戏 / 1篇
    • 计算机 / 14篇
    • 资料 / 4篇
    标签聚合
    Physics LaTex Arcaea Learning AI Magisk C/C++/C# ArchLinux
    The World of Scarlet
    https://www.adament.xyz/wp-content/uploads/2024/10/the-world-of-scarlet-Piano.mp3

    COPYRIGHT © 2025 adament.xyz. ALL RIGHTS RESERVED.
    本站虽然不重要但是还是有的 隐私政策