Adament Cabin

  • コンピューター
    • アルゴリズム
    • Linux
    • Problems
    • AI
  • 日本語を勉強
  • ACG
    • 見られている番組
    • ゲーム
  • 日常
  • 雑貨
    • ドキュメント
    • 資源
    • 治療センター
  • リンク
  • ブランチサイト
  • Japanese
    • Chinese
Adament Cabin
恋厨症候群第二治療センター
  1. 首页
  2. コンピューター
  3. アルゴリズム
  4. 正文

二叉树的一些遍历模板

2022年4月21日 1957ヒート 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

社会ごみ

Like
< 前の投稿
次の投稿 >

コメント

  • 匿名

    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篇
    • アルゴリズム / 5篇
    • Linux / 6篇
    • Problems / 3篇
    • 日常 / 1篇
    • 日本語 / 2篇
    • ゲーム / 1篇
    • コンピューター / 14篇
    • ドキュメント / 4篇
    タグ
    ArchLinux C/C++/C# Arcaea LaTex Learning Magisk AI Physics
    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.
    このサイトにとって重要ではありませんが、それでも必要な プライバシーポリシー