构建二叉树&宽度优先遍历&深度优先遍历-儿童编程

#include <bits/stdc++.h>

using namespace std;

 typedef struct TreeNode{
		int data =0;
		TreeNode *left_child = NULL;
		TreeNode *right_child = NULL;
} TreeNode; 

 

层序遍历/宽度优先遍历/BFS


void bfs(TreeNode *head){
	queue<TreeNode *> q;
	q.push(head);
	while(!q.empty()){
		TreeNode * tn = q.front();
		q.pop();
		if(tn != NULL){
			cout<<tn->data<<" ";
			if(tn->left_child != NULL)
				q.push(tn->left_child);
			if(tn->right_child != NULL)
				q.push(tn->right_child);
		}

	}
}

深度优先搜索 DFS : 前序优先

void preDFS(TreeNode *head){
	if(head == NULL){
		return;
	}
	else{
		cout<<head->data<<" ";
		preDFS(head->left_child);
		preDFS(head->right_child);
	}
}

深度优先搜索 DFS : 中序优先 和后序优先

void inDFS(TreeNode *head){
	if(head == NULL){
		return;
	}
	else{
		
		inDFS(head->left_child);
		cout<<head->data<<" ";
		inDFS(head->right_child);
	}
}


void postDFS(TreeNode *head){
	if(head == NULL){
		return;
	}
	else{
		
		postDFS(head->left_child);
		
		postDFS(head->right_child);
		cout<<head->data<<" ";
	}
}

 

 

main函数 & 构建二叉树: 数组方式

int main(){
	
	int n =0;	
	tn[1].data = 1;
	tn[2].data = 2;
	tn[3].data = 7;
	tn[1].left_child = &tn[2];
	tn[1].right_child = &tn[3];
	tn[4].data = 3;
	tn[5].data = 6;
	tn[2].left_child= &tn[4];
	tn[2].right_child = &tn[5];
	tn[6].data = 4;
	tn[7].data = 5;
	tn[4].left_child = &tn[6];
	tn[4].right_child = &tn[7];


	
	bfs(&tn[1]);
	
	return 0;
}

 

main函数 & 构建二叉树: 指针方式(节省内存)

int main(){

	TreeNode *head = (TreeNode *)malloc(sizeof(TreeNode));
	head->data = 1;
	TreeNode *temp_left;
	TreeNode *temp_right;
	TreeNode *left = (TreeNode *)malloc(sizeof(TreeNode));
	TreeNode *right = (TreeNode *)malloc(sizeof(TreeNode));
	left->data = 2;
	left->left_child = NULL;
	left->right_child = NULL;
	right->data = 7;
	right->left_child = NULL;
	right->right_child = NULL;
	head->left_child = left;
	head->right_child = right;
	temp_left = left;
	temp_right = right;
	left = (TreeNode *)malloc(sizeof(TreeNode));
	right = (TreeNode *)malloc(sizeof(TreeNode));
	left->data = 3;
	left->left_child = NULL;
	left->right_child = NULL;
	right->data = 6;
	right->left_child = NULL;
	right->right_child = NULL;
	temp_left->left_child = left;
	temp_left->right_child = right;
	temp_left = left;
	temp_right = right;
		left = (TreeNode *)malloc(sizeof(TreeNode));
	right = (TreeNode *)malloc(sizeof(TreeNode));
	left->data = 4;
	left->left_child = NULL;
	left->right_child = NULL;
	right->data = 5;
	right->left_child = NULL;
	right->right_child = NULL;
	temp_left->left_child= left;
	temp_left->right_child = right;
	
	bfs(head);
	cout<<endl;
	preDFS(head);
	cout<<endl;
	inDFS(head);
	cout<<endl;
	postDFS(head);
	cout<<"test";
	return 0;
}

 

代码完整版1

#include <bits/stdc++.h>

using namespace std;

 typedef struct TreeNode{
		int data =0;
		TreeNode *left_child = NULL;
		TreeNode *right_child = NULL;
} TreeNode; 






void bfs(TreeNode *head){
	queue<TreeNode *> q;
	q.push(head);
	while(!q.empty()){
		TreeNode * tn = q.front();
		q.pop();
		if(tn != NULL){
			cout<<tn->data<<" ";
			if(tn->left_child != NULL)
				q.push(tn->left_child);
			if(tn->right_child != NULL)
				q.push(tn->right_child);
		}

	}
}

void preDFS(TreeNode *head){
	if(head == NULL){
		return;
	}
	else{
		cout<<head->data<<" ";
		preDFS(head->left_child);
		preDFS(head->right_child);
	}
}

void inDFS(TreeNode *head){
	if(head == NULL){
		return;
	}
	else{
		
		inDFS(head->left_child);
		cout<<head->data<<" ";
		inDFS(head->right_child);
	}
}


void postDFS(TreeNode *head){
	if(head == NULL){
		return;
	}
	else{
		
		postDFS(head->left_child);
		
		postDFS(head->right_child);
		cout<<head->data<<" ";
	}
}

TreeNode tn[100];

int main(){
	
	int n =0;	
	tn[1].data = 1;
	tn[2].data = 2;
	tn[3].data = 7;
	tn[1].left_child = &tn[2];
	tn[1].right_child = &tn[3];
	tn[4].data = 3;
	tn[5].data = 6;
	tn[2].left_child= &tn[4];
	tn[2].right_child = &tn[5];
	tn[6].data = 4;
	tn[7].data = 5;
	tn[4].left_child = &tn[6];
	tn[4].right_child = &tn[7];


	
	bfs(&tn[1]);
	cout<<endl;
	preDFS(&tn[1]);
	cout<<endl;
	inDFS(&tn[1]);
	cout<<endl;
	postDFS(&tn[1]);
	cout<<"test";
	return 0;
}

代码完整版2: 指针方式

#include <bits/stdc++.h>

using namespace std;

 typedef struct TreeNode{
		int data =0;
		TreeNode *left_child = NULL;
		TreeNode *right_child = NULL;
} TreeNode; 






void bfs(TreeNode *head){
	queue<TreeNode *> q;
	q.push(head);
	while(!q.empty()){
		TreeNode * t = q.front();
		q.pop();
		if(t != NULL){
			cout<<t->data<<" ";
			if(t->left_child != NULL)
				q.push(t->left_child);
			if(t->right_child != NULL)
				q.push(t->right_child);
		}

	}
}

void preDFS(TreeNode *head){
	if(head == NULL){
		return;
	}
	else{
		cout<<head->data<<" ";
		preDFS(head->left_child);
		preDFS(head->right_child);
	}
}

void inDFS(TreeNode *head){
	if(head == NULL){
		return;
	}
	else{
		
		inDFS(head->left_child);
		cout<<head->data<<" ";
		inDFS(head->right_child);
	}
}


void postDFS(TreeNode *head){
	if(head == NULL){
		return;
	}
	else{
		
		postDFS(head->left_child);
		
		postDFS(head->right_child);
		cout<<head->data<<" ";
	}
}

//TreeNode tn[100];

int main(){

	TreeNode *head = (TreeNode *)malloc(sizeof(TreeNode));
	head->data = 1;
	TreeNode *temp_left;
	TreeNode *temp_right;
	TreeNode *left = (TreeNode *)malloc(sizeof(TreeNode));
	TreeNode *right = (TreeNode *)malloc(sizeof(TreeNode));
	left->data = 2;
	left->left_child = NULL;
	left->right_child = NULL;
	right->data = 7;
	right->left_child = NULL;
	right->right_child = NULL;
	head->left_child = left;
	head->right_child = right;
	temp_left = left;
	temp_right = right;
	left = (TreeNode *)malloc(sizeof(TreeNode));
	right = (TreeNode *)malloc(sizeof(TreeNode));
	left->data = 3;
	left->left_child = NULL;
	left->right_child = NULL;
	right->data = 6;
	right->left_child = NULL;
	right->right_child = NULL;
	temp_left->left_child = left;
	temp_left->right_child = right;
	temp_left = left;
	temp_right = right;
		left = (TreeNode *)malloc(sizeof(TreeNode));
	right = (TreeNode *)malloc(sizeof(TreeNode));
	left->data = 4;
	left->left_child = NULL;
	left->right_child = NULL;
	right->data = 5;
	right->left_child = NULL;
	right->right_child = NULL;
	temp_left->left_child= left;
	temp_left->right_child = right;
	
	bfs(head);
	cout<<endl;
	preDFS(head);
	cout<<endl;
	inDFS(head);
	cout<<endl;
	postDFS(head);
	cout<<"test";
	return 0;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值