(C语言)二叉树实验

212 阅读4分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

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)。