(C语言)二叉树实验

感兴趣的小伙伴可以先看看我的这篇文章哦,打开看看,说不定能帮到你一些~~

金陵科技学院软件工程学院软件工程专业

1.(1):建立一棵二叉树。对此树进行前序遍历、中序遍历及后序遍历,输出遍历序列。

实现代码:

#include <stdio.h>
#include <stdlib.h> 
typedef struct BitNode{
    char data;
    struct BitNode *lchild;
    struct BitNode *rchild;
}BitNode, *BinTree;
void CreateBinTree(BinTree *bt){
    char ch;
    if ((ch = getchar()) == '#') {
		*bt = NULL;
    }else{
        *bt = (BitNode *)malloc(sizeof(BitNode));
        (*bt)->data = ch;
        CreateBinTree(&(*bt)->lchild);
        CreateBinTree(&(*bt)->rchild);
    }
}
void PreOrderTraversal(BinTree *bt){
   if (*bt != NULL){
       printf("%c", ((*bt)->data));
       PreOrderTraversal(&(*bt)->lchild);
       PreOrderTraversal(&(*bt)->rchild);
   }
}
void InOrderTraversal(BinTree *bt){
    if (*bt != NULL){
        InOrderTraversal(&(*bt)->lchild);
        printf("%c", (*bt)->data);
        InOrderTraversal(&(*bt)->rchild);
    }
}
void PostOrderTraversal(BinTree *bt){
    if (*bt != NULL){
        PostOrderTraversal(&(*bt)->lchild);
        PostOrderTraversal(&(*bt)->rchild);
        printf("%c", (*bt)->data);
    }
}
int main(){
    BinTree T;
	printf("创建一颗二叉树(以#为虚结点):\n");
    CreateBinTree(&T);
    printf("二叉树创建完成。\n");
	printf("前序遍历:\n");
    PreOrderTraversal(&T);
	printf("\n中序遍历:\n");
    InOrderTraversal(&T);
	printf("\n后序遍历:\n");
    PostOrderTraversal(&T);
    return 0;
}

运行代码:
在这里插入图片描述
分析:
该程序中用于对二叉树进行前序遍历、中序遍历、后序遍历的函数的时间复杂度均为指数阶O(2^n),空间复杂度均为线性阶O(n)。

(2):在第一题基础上,求二叉树中叶结点的个数。

实现代码:

#include <stdio.h>
#include <stdlib.h> 
typedef struct BitNode{
    char data;
    struct BitNode *lchild;
    struct BitNode *rchild;
}BitNode, *BinTree;
void CreateBinTree(BinTree *bt){
    char ch; 
    if ((ch = getchar()) == '#') {
		*bt = NULL;
    }else{
        *bt = (BitNode *)malloc(sizeof(BitNode));
        (*bt)->data = ch;
        CreateBinTree(&(*bt)->lchild);
        CreateBinTree(&(*bt)->rchild);
    }
}
int CountLeaf(BinTree *bt,int count){
    if((*bt) != NULL){
        if ((*bt)->lchild == NULL && (*bt)->rchild == NULL)
            count++;
        count = CountLeaf(&(*bt)->lchild,count);
        count = CountLeaf(&(*bt)->rchild,count);
    }
    return count;
}
int main(){
    int count = 0;
    BinTree T;
    printf("创建一颗二叉树(以#为虚结点):\n");
    CreateBinTree(&T);
    printf("二叉树创建完成。\n");
    count = CountLeaf(&T,count);
    printf("该二叉树中叶结点个数为:%d",count);
    return 0;
}

运行结果:
在这里插入图片描述
分析:
该程序中用于计算二叉树中叶结点个数的函数的时间复杂度为指数阶O(2^n),空间复杂度为线性阶O(n)。

(3):在第一题基础上,求二叉树中结点总数。

实现代码:

#include <stdio.h>
#include <stdlib.h> 
typedef struct BitNode{
    char data;
    struct BitNode *lchild;
    struct BitNode *rchild;
}BitNode, *BinTree;
void CreateBinTree(BinTree *bt){
    char ch;
    if ((ch = getchar()) == '#') {
		*bt = NULL;
    }else{
        *bt = (BitNode *)malloc(sizeof(BitNode));
        (*bt)->data = ch;
        CreateBinTree(&(*bt)->lchild);
        CreateBinTree(&(*bt)->rchild);
    }
}
int CountNode(BinTree T){
	if(T == NULL) return 0;
	else return CountNode(T->lchild) + CountNode(T->rchild) + 1;
}
int main(){
    BinTree T;
    printf("创建一颗二叉树(以#为虚结点):\n");
    CreateBinTree(&T);
    printf("二叉树创建完成。\n");
    printf("该二叉树中结点总数为:%d",CountNode(T));
	return 0;
}

运行结果:
在这里插入图片描述
分析:
该程序中用于计算二叉树中结点总数的函数的时间复杂度为指数阶O(2^n),空间复杂度为线性阶O(n)。

(4):在第一题基础上,求二叉树的深度。

实现代码:

#include <stdio.h>
#include <stdlib.h> 
typedef struct BitNode{
    char data;
    struct BitNode *lchild;
    struct BitNode *rchild;
}BitNode, *BinTree;
void CreateBinTree(BinTree *bt){
    char ch;
    if ((ch = getchar()) == '#') {
		*bt = NULL;
    }else{
        *bt = (BitNode *)malloc(sizeof(BitNode));
        (*bt)->data = ch;
        CreateBinTree(&(*bt)->lchild);
        CreateBinTree(&(*bt)->rchild);
    }
}
int getBinTreeDepth(BinTree T){
	int leftHeight, rightHeight, maxHeight;
	if (T != NULL){
		leftHeight = getBinTreeDepth(T->lchild);
		rightHeight = getBinTreeDepth(T->rchild);
		maxHeight = leftHeight > rightHeight ? leftHeight : rightHeight;
		return maxHeight + 1;
	}else{
		return 0;
	}
}
int main(){
    BinTree T;
    printf("创建一颗二叉树(以#为虚结点):\n");
    CreateBinTree(&T);
    printf("二叉树创建完成。\n");
    printf("该二叉树的深度为:%d",getBinTreeDepth(T));
	return 0;
}

运行结果:
在这里插入图片描述
分析:
该程序中用于计算二叉树深度的函数的时间复杂度为指数阶O(2^n),空间复杂度为线性阶O(n)。

2.已知一棵完全二叉树存于顺序表sa中,sa.elem[1…sa.last]存储结点的值。试编写算法由此顺序存储结构建立该二叉树的二叉链表。
解题思路:根据完全二叉树顺序存储的性质来确定二叉树的父子关系即“还原”了二叉树,之后再按照二叉树二叉链表的构造方法进行建立。完全二叉树顺序存储的一个重要性质为,第i个结点的左孩子是编号为2i的结点,第i个结点的右孩子是编号为2i+1的结点。

实现代码:

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct{
	char elem[MAXSIZE];
	int last;
}sequenlist;
typedef struct BitNode{
    char data;
    struct BitNode *lchild;
    struct BitNode *rchild;
}BitNode, *BinTree;
void CreateBinTree(sequenlist sa, BinTree *bt, int i){
	(*bt) = (BitNode *)malloc(sizeof(BitNode));
	(*bt)->data = sa.elem[i];
	if(sa.last >= 2 * i) 
		CreateBinTree(sa, &(*bt)->lchild, 2 * i);
	else{
		(*bt)->lchild = NULL;
	}
	if(sa.last >= 2 * i + 1)
		CreateBinTree(sa, &(*bt)->rchild, 2 * i + 1);
	else{
		(*bt)->rchild = NULL;
	}
}
void PreOrderTraversal(BinTree *bt){
   if (*bt != NULL){
       printf("%c", ((*bt)->data));
       PreOrderTraversal(&(*bt)->lchild);
       PreOrderTraversal(&(*bt)->rchild);
   }
}
int main(){
    BinTree T;
    // 随便创建的一个顺序表,用于后面由顺序存储结构建立二叉树的二叉链表
    sequenlist sa = {{'0','A','B','C','D','E','F','G','H','I'},9}; 
    printf("建立一颗完全二叉树。\n");
    CreateBinTree(sa,&T,1);
    printf("完全二叉树创建完成。\n");
    printf("对此完全二叉树进行前序遍历,遍历序列为:\n");
    PreOrderTraversal(&T); 
    return 0;
}

运行结果:
在这里插入图片描述
分析:
该程序中用于由顺序存储结构建立二叉树的二叉链表的函数的时间复杂度为指数阶O(2^n),空间复杂度为线性阶O(n)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Spiderman_94

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值