l4-d11 树及实现(下)

一、二叉树的三种遍历

常用命令

创建二叉树

bitree * tree_create() {
	data_t ch;
	bitree *r;

	scanf("%c", &ch);
	if (ch == '#')
		return NULL;

	if ((r = (bitree *)malloc(sizeof(bitree))) == NULL) {
		printf("malloc failed\n");
		return NULL;
	}
	r->data = ch;//封装节点
	r->left  = tree_create(); //左子树
	r->right = tree_create(); //右子树
	return r;
}

先序遍历

//先序遍历
void preorder(bitree * r) {
	if (r == NULL) {
		return;
	}
	printf("%c", r->data);//根
	preorder(r->left);//左
	preorder(r->right);//右
}

中序遍历

//中序遍历
void inorder(bitree * r) {
	if (r == NULL) {
		return;
	}
	inorder(r->left);
	printf("%c", r->data);
	inorder(r->right);
}

后序遍历

//后序遍历
void postorder(bitree * r) {
	if (r == NULL) {
		return;
	}
	postorder(r->left);
	postorder(r->right);
	printf("%c", r->data);
}

层次遍历

void layerorder(bitree * r) {
	linkqueue * lq;

	if ((lq = queue_create()) == NULL) 
		return;

	if (r == NULL) 
		return;

	printf("%c", r->data);//访问
	enqueue(lq, r);//入队
    
	while (!queue_empty(lq)) {//队列不为空
		r = dequeue(lq);//出队
		if (r->left) {//左子树存在
			printf("%c", r->left->data);//访问
			enqueue(lq, r->left);//入队
		}
		if (r->right) {//右子树存在
			printf("%c", r->right->data);//访问
			enqueue(lq, r->right);//入队
		}
	}		
	puts("");
}

二、二叉树的层次遍历

void layerorder(bitree * r) {
	linkqueue * lq;

	if ((lq = queue_create()) == NULL) 
		return;

	if (r == NULL) 
		return;

	printf("%c", r->data);//访问
	enqueue(lq, r);//入队
    
	while (!queue_empty(lq)) {//队列不为空
		r = dequeue(lq);//出队
		if (r->left) {//左子树存在
			printf("%c", r->left->data);//访问
			enqueue(lq, r->left);//入队
		}
		if (r->right) {//右子树存在
			printf("%c", r->right->data);//访问
			enqueue(lq, r->right);//入队
		}
	}		
	puts("");
}

三、实现

头文件

tree.h

#ifndef _TREE_H_
#define _TREE_H_

typedef char data_t;

typedef struct node_t {
	data_t data;//数据部分
	struct node_t * left;//左孩子
	struct node_t * right;//右孩子
}bitree;

bitree * tree_create();
void preorder(bitree * r);//先序遍历
void inorder(bitree * r);//中序
void postorder(bitree * r);//后序
void layerorder(bitree * r);

#endif

linkqueue.h

#include "tree.h"
typedef bitree * datatype;

typedef struct node {
	datatype data;
	struct node *next;
}listnode , *linklist;

typedef struct {
	linklist front;
	linklist rear;
}linkqueue;

linkqueue * queue_create();
int enqueue(linkqueue *lq, datatype x);
datatype dequeue(linkqueue *lq);
int queue_empty(linkqueue *lq);
int queue_clear(linkqueue *lq);
linkqueue * queue_free(linkqueue *lq);

源程序

tree.c

#include <stdio.h>
#include <stdlib.h>
//#include "tree.h" //"linkqueue.h"中包含"tree.h"
#include "linkqueue.h"

bitree * tree_create() {
	data_t ch;
	bitree *r;

	scanf("%c", &ch);
	if (ch == '#')
		return NULL;

	if ((r = (bitree *)malloc(sizeof(bitree))) == NULL) {
		printf("malloc failed\n");
		return NULL;
	}
	r->data = ch;//封装节点
	r->left  = tree_create(); //左子树
	r->right = tree_create(); //右子树
	return r;
}

//先序遍历
void preorder(bitree * r) {
	if (r == NULL) {
		return;
	}
	printf("%c", r->data);//根
	preorder(r->left);//左
	preorder(r->right);//右
}

//中序遍历
void inorder(bitree * r) {
	if (r == NULL) {
		return;
	}
	inorder(r->left);
	printf("%c", r->data);
	inorder(r->right);
}

//后序遍历
void postorder(bitree * r) {
	if (r == NULL) {
		return;
	}
	postorder(r->left);
	postorder(r->right);
	printf("%c", r->data);
}

void layerorder(bitree * r) {
	linkqueue * lq;

	if ((lq = queue_create()) == NULL) 
		return;

	if (r == NULL) 
		return;

	printf("%c", r->data);//访问
	enqueue(lq, r);//入队
    
	while (!queue_empty(lq)) {//队列不为空
		r = dequeue(lq);//出队
		if (r->left) {//左子树存在
			printf("%c", r->left->data);//访问
			enqueue(lq, r->left);//入队
		}
		if (r->right) {//右子树存在
			printf("%c", r->right->data);//访问
			enqueue(lq, r->right);//入队
		}
	}		
	puts("");
}

linkqueue.c

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

linkqueue * queue_create() {
	linkqueue *lq;

	if ((lq = (linkqueue *)malloc(sizeof(linkqueue))) == NULL) {
		printf("malloc linkqueue failed\n");
		return NULL;
	}

	lq->front = lq->rear = (linklist)malloc(sizeof(listnode));
	if (lq->front == NULL) {
		printf("malloc node failed\n");
		return NULL;
	}
	lq->front->data = 0;
	lq->front->next = NULL;

	return lq;
}

int enqueue(linkqueue *lq, datatype x) {
	linklist p;

	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}

	if ((p = (linklist)malloc(sizeof(listnode))) == NULL) {
		printf("malloc node failed\n");
		return -1;
	}
	p->data = x;
	p->next = NULL;

	lq->rear->next = p;
	lq->rear = p;

	return 0;
}

datatype dequeue(linkqueue *lq) {
	linklist p;

	if (lq == NULL) {
		printf("lq is NULL\n");
		return NULL;
	}

	p = lq->front;
	lq->front = p->next;
	free(p);
	p = NULL;

	return (lq->front->data);
}

int queue_empty(linkqueue *lq) {
	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}

	return (lq->front == lq->rear ? 1 : 0);
}

int queue_clear(linkqueue *lq) {
	linklist p;

	if (lq == NULL) {
		printf("lq is NULL\n");
		return -1;
	}

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

linkqueue * queue_free(linkqueue *lq) {
	linklist p;

	if (lq == NULL) {
		printf("lq is NULL\n");
		return NULL;
	}

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

	free(lq);
	lq = NULL;

	return NULL;
}

测试文件test.c

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

int main(int argc, const char *argv[])
{
	bitree * r;

	if ((r = tree_create()) == NULL)
		return -1;

	preorder(r);
	puts("");

	inorder(r);
	puts("");

	postorder(r);
	puts("");

	layerorder(r);

	return 0;
}

四、作业 

已知遍历结果如下,试画出对应的二叉树
前序: A B C E H F I J D G K
中序: A H E C I F J B D K G
(利用遍历规则的特点,还原二叉树,提交还原的二叉树,或者写出后序遍历结果) 

 

后序遍历结果:HEIJFCKGDBA

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: DevExpress VCL是一个功能强大的控件库,提供许多可自定义的控件和组件,适用于Delphi和C++Builder IDE。版本19.1.2完整版可以与Delphi 7至Delphi 11版本进行兼容。提供了各种各样的控件,包括网格控件、图表控件、导航控件、表单控件、编辑控件、布局控件、菜单控件等。它还提供了两个主题:Light和Dark,以满足不同用户的需求。除了控件和组件外,DevExpress VCL还提供了很多工具和服务,包括打印和报表工具,代码生成器,文档管理工具等。此外,它还支持Unicode字符集和跨平台开发。总之,DevExpress VCL是一个功能强大的控件库,可以提高开发人员的开发效率和应用程序性能。 ### 回答2: DevExpress VCL是一款Windows应用程序开发框架,该框架提供了大量的控件、工具和资源,能够简化Windows应用程序的开发过程。 VCL_19.1.2full source d7-d11 Alexandria是该框架的一个版本,其中包含了完整的源代码和对Delphi 7到Delphi 11 Alexandria版本的支持。 使用DevExpress VCL可以快速创建强大的Windows应用程序,包括各种类型的控件,如文本编辑器、表格、图表和图形,可以进行丰富的数据可视化,大大提高了用户界面的质量。 此版本还加入了实用的开发工具,如预览控制、代码生成器和调试工具等,使开发人员能够更加高效地开发高质量的Windows应用程序。 总的来说,DevExpress VCL_19.1.2full source d7-d11 Alexandria是一个非常实用的Windows应用程序开发框架,具有强大的功能和大量的功能,使开发人员能够更快地创建高质量的Windows应用程序。 ### 回答3: DevExpress VCL是一款功能强大的开发工具,可以帮助开发人员加快应用程序的开发过程,同时还提供了一系列质量优异的控件和组件,能够满足各种开发场景的需要。其中VCL 19.1.2 Full Source是DevExpress VCL最新版本,在Delphi 7到Delphi 11 Alexandria的开发环境下良好运行。它包含了一系列优质的控件和组件,如日历控件、表格控件、图表控件等,这些功能强大的控件可以帮助开发人员快速构建出高效、美观的应用程序。同时,它还提供了全面的源代码,让开发人员可以更加灵活地修改和定制控件,以适应自己的开发需求。将DevExpress VCL 19.1.2 Full Source应用于开发过程中,不仅可以提升开发效率,还可以提高应用程序的质量和稳定性,从而给用户带来更好的使用体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值