• C语言数据结构之二叉树的实现


    本篇博文是博主在学习C语言算法与数据结构的一些应用代码实例,给出了以二叉链表的形式实现二叉树的相关操作。如创建,遍历(先序,中序后序遍历),求树的深度,树的叶子节点数,左右兄弟,父节点。

    代码清单如下:

      1 #pragma once
      2 #include<stdio.h>
      3 #include"stdlib.h"
      4 typedef char TElemType;
      5 typedef struct BiTNode
      6 {
      7     TElemType data;
      8     struct BiTNode *lchild, *rchild;
      9 
     10 }BiTNode, *BiTree;
     11 void InitBiTree(BiTree&T)
     12 {
     13     T = NULL;
     14 }
     15 
     16 //创建二叉树
     17 int CreateBiTree(BiTree&T)
     18 {
     19     printf("请输入树的节点:");
     20     TElemType ch;
     21     getchar();
     22     scanf("%c", &ch);
     23     if (ch == ' ')
     24     {
     25         T = NULL;
     26 
     27     }
     28     else
     29     {
     30         T = (BiTNode*)malloc(sizeof(BiTNode));
     31         if (!T)
     32         {
     33             exit(EOVERFLOW);
     34         }
     35         T->data = ch;
     36         CreateBiTree(T->lchild);
     37         CreateBiTree(T->rchild);
     38     }
     39     return true;
     40 }
     41 
     42 //判断二叉树是否为空
     43 int BiTreeEmpty(BiTree&T)
     44 {
     45     if (T)
     46     {
     47         return false;
     48     }
     49     else
     50     {
     51         return true;
     52     }
     53 }
     54 
     55 //求树的深度
     56 int BiTreeDepth(BiTree T)
     57 {
     58     int i, j;
     59     if (!T)
     60     {
     61         return 0;
     62     }
     63     if (T->lchild)
     64     {
     65         i = BiTreeDepth(T->lchild);
     66     }
     67     else
     68     {
     69         i = 0;
     70     }
     71     if (T->rchild)
     72     {
     73         j = BiTreeDepth(T->rchild);
     74     }
     75     else
     76     {
     77         j = 0;
     78     }
     79     return i > j ? i + 1 : j + 1;
     80 }
     81 
     82 //打印节点
     83 void Visit(TElemType e)
     84 {
     85     printf("%c", e);
     86 }
     87 
     88 //以递归形式先序遍历二叉树 
     89 void PreOrderTraverse(BiTree T, void(*Visit)(TElemType))
     90 {
     91     if (T)
     92     {
     93         Visit(T->data);
     94         PreOrderTraverse(T->lchild, Visit);
     95         PreOrderTraverse(T->rchild, Visit);
     96     }
     97 }
     98 //以递归形式中序遍历二叉树 
     99 void InOrderTraverse(BiTree T, void(*Visit)(TElemType))
    100 {
    101     if (T)
    102     {
    103         InOrderTraverse(T->lchild, Visit);
    104         Visit(T->data);
    105         InOrderTraverse(T->rchild, Visit);
    106     }
    107 }
    108 
    109 //以递归形式后序遍历二叉树 
    110 void PostOrderTraverse(BiTree T, void(*Visit)(TElemType))
    111 {
    112     if (T)
    113     {
    114         PostOrderTraverse(T->lchild, Visit);
    115         PostOrderTraverse(T->rchild, Visit);
    116         Visit(T->data);
    117     }
    118 }
    119 
    120 //计算叶子节点数
    121 int CountLeaf(BiTree &T, int &count)
    122 {
    123     if (T)
    124     {
    125 
    126         if ((T->lchild == NULL) && (T->rchild == NULL))
    127         {
    128 
    129             count++;
    130         }
    131         CountLeaf(T->lchild, count);
    132         CountLeaf(T->rchild, count);
    133     }
    134     return count;    //返回叶子节点数
    135 }
    136 int max(int a, int b)
    137 {
    138     return a > b ? a : b;
    139 }
    140 
    141 //求父节点的值 注意返回值为char类型
    142 char Parent(BiTree T, ElemType e) //二叉树存在,e是T中某个节点,若e为非根结点,则返回它的双亲,否则返回空
    143 {
    144 
    145     if (T != NULL)
    146     {
    147         if ((T->lchild && (T->lchild->data == e)) || (T->rchild && (T->rchild->data == e)))
    148             return(T->data);
    149         else
    150         {
    151             Parent(T->lchild, e);
    152             Parent(T->rchild, e);
    153         }
    154     }
    155     return ' ';
    156 
    157 }
    158 
    159 //寻找某个节点 
    160 BiTree FindNode(BiTree T, char e)  //返回指向这个节点的指针
    161 {
    162     if (T == NULL)
    163     {
    164         return NULL;
    165     }
    166     if (T->data == e)
    167     {
    168         return T;
    169     }
    170     BiTree node;
    171     if ((node = FindNode(T->lchild, e)) != NULL)
    172         return node;
    173     else
    174         return FindNode(T->rchild, e);
    175 }
    176 
    177 //输出某个节点的左兄弟
    178 void LeftSibling(BiTree &T, char e)
    179 {
    180     char ch = Parent(T, e);
    181     BiTree tmp = FindNode(T, ch);
    182     if (tmp->lchild != NULL)
    183     {
    184         if (tmp->lchild->data != e)
    185         {
    186             printf("此节点的左兄弟为:%c ", tmp->lchild->data);
    187         }
    188         else
    189         {
    190             printf("此节点没有左兄弟 ");
    191         }
    192         //return tmp->lchild->data;
    193     }
    194     else
    195     {
    196         printf("此节点没有左兄弟 ");
    197 
    198     }
    199 }
    200 
    201 //输出某个节点的左兄弟
    202 void RightSibling(BiTree &T, char e)
    203 {
    204     char ch = Parent(T, e);
    205     BiTree tmp = FindNode(T, ch);
    206     if (tmp->rchild != NULL)
    207     {
    208         if (tmp->rchild->data != e)
    209             printf("此节点的右兄弟为:%c 
    ", tmp->rchild->data);
    210         //return tmp->rchild->data;
    211         else
    212         {
    213             printf("此节点没有右兄弟 ");
    214         }
    215     }
    216     else
    217     {
    218         printf("此节点没有右兄弟 ");
    219 
    220     }
    221 }


  • 相关阅读:
    【HIDS】关于HIDS的一些看法
    图片在容器内水平垂直居中显示
    C++ 实现Cholesky分解
    Minikube 安装和简单使用
    关于.net的一些记录
    C#将窗体Form嵌入主窗体Panel中的一些问题
    Pod持久化
    Maven打包包含jar包
    Adam
    [漏洞]DNS Server Spoofed Request Amplification DDoS
  • 原文地址:https://www.cnblogs.com/doctorXiong/p/9186502.html
Copyright © 2020-2023  润新知