二叉树的创建和遍历

二叉树示意图

二叉树示意图# 二叉树的创建
二叉树具有递归特性,所以利用递归的方式实现二叉树的创建。(使用链式存储结构)

/*利用先序遍历的结构进行递归*/
linktree tree_creart(){
	data_s ch;
	linktree T;
	scanf("%c",&ch);
	if(ch == '#'){  //输入‘#’代表当前节点为空,即说明某个分支没有
		return NULL;
	}

	T = (linktree)malloc(sizeof(tree));

	if(T == NULL){  //递归终止条件,最后一个节点没有分支
		printf("T malloc is failed\n");
		return NULL;
	}
/*根节点,左子树,右子树的顺序递归输入*/
	T->data = ch;
	T->lchild = tree_creart();
	T->rchild = tree_creart();

	return T;
}

二叉树的遍历方法

遍历利用递归的方法,遍历二叉树;

先序遍历

先访问根节点,再访问左子树,后访问右子树;
示例二叉树遍历结果应为:A B C D E F G H K L;

int tree_show_first(linktree T){
		if(T == NULL){
		return ;
	}
	printf("%c",T->data);
	tree_show_first(T->lchild);
	tree_show_first(T->rchild);
}

中序遍历

先访问左子树,再访问根节点,后访问右子树;
示例二叉树遍历结果应为:C B E D A F K H L G;

int tree_show_middle(linktree T){
	if(T == NULL){
		return;
	}
	tree_show_middle(T->lchild);
	printf("%c",T->data);
	tree_show_middle(T->rchild);

}

后序遍历

先访问左子树,再访问右子树,后访问根节点;
示例二叉树遍历结果应为:C E D B K L H G F A;

int tree_show_back(linktree T){

	if(T == NULL){
		return;
	}
	tree_show_back(T->lchild);
	tree_show_back(T->rchild);
	printf("%c",T->data);
}

层遍历

依次遍历每层的兄弟节点;
示例二叉树遍历结果应为:A B F C D G E H K L;
将每层节点存入队列中,然后在进行出队的方式遍历每层;


int tree_show_layer(linktree T){ 

	linkqueue Q;
	Q = queue_creart();

	if(T == NULL){
		return;
	}

	printf("%c",T->data);
	puts("");
	queue_push(Q,T);
	while(Q->front != Q->reat){
		T = queue_pop(Q);

		if(T->lchild){
			printf("%c",T->lchild->data);
		
			queue_push(Q,T->lchild);
		}
		if(T->rchild){
			printf("%c",T->rchild->data);
			queue_push(Q,T->rchild);
		}
	puts("");
	}
	queue_free(Q);
}

实现代码展示

二叉树
tree.h

#ifndef _TREE_H_
#define _TREE_H_


typedef char data_s;

typedef struct node_t{
	data_s data;
	struct node_t *lchild, *rchild;
}tree,*linktree;

linktree tree_creart();
//int tree_insert(linktree T,data_t data);
int tree_show_first(linktree T);
int tree_show_middle(linktree T);
int tree_show_back(linktree T);
int tree_show_layer(linktree T);
linktree tree_free(linktree T);

#endif

tree.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"linkqueue.h"

linktree tree_creart(){ //二叉树创建
	data_s ch;
	linktree T;
	scanf("%c",&ch);
	if(ch == '#'){
		return NULL;
	}

	T = (linktree)malloc(sizeof(tree));

	if(T == NULL){
		printf("T malloc is failed\n");
		return NULL;
	}

	T->data = ch;
	T->lchild = tree_creart();
	T->rchild = tree_creart();

	return T;
}
int tree_show_first(linktree T){ //二叉树先序遍历
		if(T == NULL){
		return ;
	}
	printf("%c",T->data);
	tree_show_first(T->lchild);
	tree_show_first(T->rchild);
}
int tree_show_middle(linktree T){ //二叉树中序遍历
	if(T == NULL){
		return;
	}
	tree_show_middle(T->lchild);
	printf("%c",T->data);
	tree_show_middle(T->rchild);

}
int tree_show_back(linktree T){  //二叉树后序遍历

	if(T == NULL){
		return;
	}
	tree_show_back(T->lchild);
	tree_show_back(T->rchild);
	printf("%c",T->data);
}

int tree_show_layer(linktree T){ //二叉树层遍历

	linkqueue Q;
	Q = queue_creart();

	if(T == NULL){
		return;
	}

	printf("%c",T->data);
	puts("");
	queue_push(Q,T);
	while(Q->front != Q->reat){
		T = queue_pop(Q);

		if(T->lchild){
			printf("%c",T->lchild->data);
		
			queue_push(Q,T->lchild);
		}
		if(T->rchild){
			printf("%c",T->rchild->data);
			queue_push(Q,T->rchild);
		}
	puts("");
	}
	queue_free(Q);
}

linktree tree_free(linktree T){ //二叉树内存释放

linktree p,q;
p = T;
	linkqueue Q;
	Q = queue_creart();

	if(T == NULL){
		return;
	}

	printf("%c",T->data);
	puts("");
	queue_push(Q,T);
	while(Q->front != Q->reat){
		T = queue_pop(Q);

		if(T->lchild){
		//	printf("%c",T->lchild->data);
		
			queue_push(Q,T->lchild);
		}
		if(T->rchild){
		//	printf("%c",T->rchild->data);
			queue_push(Q,T->rchild);
		}
	printf("%c is free",T->data);
	free(T);
	puts("");
	}

	return NULL;
}

队列
lqueue.h

#include"tree.h"

typedef linktree data_t;

typedef struct node{
data_t data;
struct node *next;
}node,*linknode;

typedef struct{
linknode front;
linknode reat;
}queue,*linkqueue;

linkqueue queue_creart();
int queue_push(linkqueue Q,data_t data);
data_t queue_pop(linkqueue Q);
linkqueue queue_free(linkqueue Q);

lqueue.c

#include"linkqueue.h"
#include"tree.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

linkqueue queue_creart(){
	linknode H;
	linkqueue Q;
	H = (linknode)malloc(sizeof(node));
	if(H == NULL){
		printf("H malloc is filed\n");
		return NULL;
	}
	Q = (linkqueue)malloc(sizeof(queue));
	if(Q == NULL){
		printf("Q malloc is filed\n");
		return NULL;
	}
	H->data = 0;
	H->next = NULL;
	Q->front = Q->reat = H;
	return Q;
}
int queue_push(linkqueue Q,data_t data){
	linknode H;
	H = (linknode)malloc(sizeof(node));
	if(H == NULL){
		printf("H malloc is failed\n");
		return 0;
	}
	H->data = data;
	H->next = Q->reat->next;
	Q->reat->next = H;
	Q->reat = H;
	return 0;
}
data_t queue_pop(linkqueue Q){
	linknode p;
	if(Q->front == Q->reat){
		printf("queue is NULL\n");
		return NULL;
	}
	p = Q->front;
	Q->front = Q->front->next;
	free(p);
	return (Q->front->data);
}
linkqueue queue_free(linkqueue Q){
	linknode p;

	while(Q->front){
		p = Q->front;
//		printf("free %d\n",p->data);
		Q->front = Q->front->next;
		free(p);
		p = NULL;
	}
	free(Q);
	Q = NULL;
	return NULL;
}

主函数
main.c

#include<stdio.h>
#include"tree.h"

int main(){
linktree T;

printf("input data:i\n");
T = tree_creart();

tree_show_first(T);
puts("");
tree_show_middle(T);
puts("");
tree_show_back(T);
puts("");
tree_show_layer(T);
puts("");
T = tree_free(T);
tree_show_first(T);
puts("");
return 0;
}

执行结果

input data:
ABC##DE###F#GHK##L###
ABCDEFGHKL
CBEDAFKHLG
CEDBKLHGFA
ABFCDGEHKL

代码有诸多不足之处,请各位大神指教;

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树是一种常用的数据结构,它由节点组成,每个节点最多有两个子节点,分别称为左子节点和右子节点。创建二叉树方法有多种,最常用的是递归非递归方法递归创建二叉树的过程如下:首先定义一个二叉树节点的结构,包含节点值、左孩子和右孩子三个属性。然后利用递归函数,根据给定的数列或输入,依次创建节点并建立它们之间的关系。具体步骤如下:若输入为空,则返回空节点;否则,创建一个新节点作为当前节点,将当前节点的值设为输入的第一个值,将输入的余下部分分为左右两个部分,第一个部分为左子树的输入,第二个部分为右子树的输入。然后递归调用该函数,分别构建左右子树,并将返回的左右子树作为当前节点的左右孩子。 二叉树遍历有三种方式:前序遍历、中序遍历和后序遍历。前序遍历先访问根节点,然后遍历左子树,最后遍历右子树;中序遍历遍历左子树,然后访问根节点,最后遍历右子树;后序遍历遍历左子树,然后遍历右子树,最后访问根节点。对于每个节点,遍历方法是一样的,先访问当前节点后,再递归遍历左右子树。 在编程竞赛平台pta上,可以使用C++或其他编程语言来二叉树创建遍历。注意在创建二叉树时,需要读入并解析输入,构建二叉树并返回根节点。而在遍历二叉树时,可以定义一个递归函数,在访问每个节点时,先输出节点值,然后递归遍历左右子树。可以根据具体问题的要求,选择不同的遍历方式来现对二叉树节点的访问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值