关闭

二叉树的相关操作:创建、查找、求高度和深度、各种遍历(前、中、后、层序)等等

280人阅读 评论(0) 收藏 举报
分类:
#include <stdio.h>
#include <stdlib.h>
typedef struct BTree
{
    struct BTree *lchild,*rchild;
    char item;
}BTree,*Tree; //BTree内存:两个指针(各占4字节),一个char类型参数(1字节)
typedef struct Queue
{
    int Front,rear;
    BTree *bt[10];
}Queue;
Tree BTcreat();    //二叉树的创建
int  BThigh(Tree );//二叉树的高度=深度+1
int  BTnode(Tree );  //统计二叉树中全部结点个数
int  BTleafnode(Tree );  //统计二叉树中叶结点个数
int  BTsearch(Tree ,char ); //1:有元素 0:树中无此元素
Tree  BTsearch1(Tree ,char ); //返回此元素结点
int  shortestpath(Tree );//输出最短路径长度(根结点到叶结点)
void traverse(Tree ,int );//int 取0(前序)1(中序)2(后序)递归程序
void laytraverse(Tree );//用队列(头部删除,尾部插入属于先进先出)结构实现
int  BTequal(Tree ,Tree );//判断两棵树是否相等(1:相等,0:不等)
void exchange(Tree );//将二叉树的左右子节点交换
int check(Tree ,Tree );//判断两个节点是否相等
int symmetry(Tree );//判断二叉树是否对称(与其“镜像”一样)

int main()
{
    printf("Creat a new Binary Tree:\n");
    BTree *bt = BTcreat();
    printf("The pre-traverse:\n");
    traverse(bt,0);
    printf("\n");
    printf("The indix-traverse:\n");
    traverse(bt,1);
    printf("\n");
    printf("The post-traverse:\n");
    traverse(bt,2);
    printf("\n");
    int h = BThigh(bt);
    int dp = h-1;
    printf("The high of the Binary Tree is %d.\n",h);
    printf("The depth of the Binary Tree is %d.\n",dp);
    int cnt_node = BTnode(bt);
    printf("The node number of the Binary Tree is %d.\n",cnt_node);
    printf("Input the char to be searched: ");
    char ch;
    scanf("%c",&ch);
    int k = BTsearch(bt,ch);
    if(k)
        printf("%c is in the Binary Tree.\n",ch);
    else
        printf("No %c!\n",ch);
    printf("The lay-traverse:\n");
    laytraverse(bt);
    printf("\n");
    BTree *st = bt;
    if(BTequal(bt,st))
        printf("bt and st are equal!\n");
    else
        printf("bt and st are NOT equal!\n");
    int len = shortestpath(bt);
    printf("The shortest path length is:%d\n",len);
    if(symmetry(bt))
        printf("The Binary Tree is symmetric!\n");
    else
        printf("NO symmetric!\n");
    exchange(st);
    traverse(st,0);
    return 0;
}

Tree BTcreat()
{
    BTree *bt;   
    char ch;
    scanf(" %c",&ch);
    if(ch == '#')
        return  NULL;
    else
    {
        bt = malloc(sizeof(*bt));
        bt->item = ch;
        printf("Input the left child:\n");
        bt->lchild = BTcreat();
        printf("Input the right child:\n");
        bt->rchild = BTcreat();
    }
    return bt;
}

void traverse(Tree bt,int k)
{
    switch(k)
    {
case 0:
    if(bt == NULL)
        return;
    else
    {
        printf("%c ",bt->item);
        if(bt->lchild != NULL)
            traverse(bt->lchild,0);
        if(bt->rchild != NULL)
            traverse(bt->rchild,0);
    }
    break;
case 1:
    if(bt == NULL)
        return;
    else
    {
        if(bt->lchild != NULL)
            traverse(bt->lchild,1);
        printf("%c ",bt->item);
        if(bt->rchild != NULL)
            traverse(bt->rchild,1);
    }
    break;
case 2:
    if(bt == NULL)
        return;
    else
    {
        if(bt->lchild != NULL)
            traverse(bt->lchild,2);
        if(bt->rchild != NULL)
            traverse(bt->rchild,2);
        printf("%c ",bt->item);
    }
    break;

    }
}

int BThigh(Tree st)
{
    int hl,hr;
    if(st == NULL)
        return 0;
    else
    {
            hl = BThigh(st->lchild)+1;
            hr = BThigh(st->rchild)+1;
    if(hl > hr)
        return hl;
    else
        return hr;
    }
}

int  BTnode(Tree st)
{
    int cnt;
    if(st == NULL)
        return 0;
    else
    {
       cnt = BTnode(st->lchild)+BTnode(st->rchild)+1;
       return cnt;
    }
}

int  BTleafnode(Tree st)
{
    int cnt;
    if(st == NULL)
        return 0;
    else
    {
        if(st->lchild == NULL && st->rchild == NULL)
            cnt = BTleafnode(st->lchild)+BTleafnode(st->rchild)+1;
        return cnt;
    }
}

int  BTsearch(Tree st,char ch)
{
   if(st == NULL)
     return 0;
   else
   {
       if(st->item == ch)
        return 1;
       else
        {
            int k = BTsearch(st->lchild,ch);
            if(k)
                return k;
            else
              return BTsearch(st->rchild,ch);
        }
   }
}

Tree BTsearch1(Tree st,char ch)  //返回值为BTree型的search
{
    if(st == NULL)
     return NULL;
   else
   {
       if(st->item == ch)
        return st;
       else
        {
            if(BTsearch(st->lchild,ch) == NULL)
              return BTsearch(st->rchild,ch);
        }
   }
}


int  shortestpath(Tree st)
{
    int hl,hr;
    if(st->lchild == NULL && st->rchild == NULL)
        return 0;
    else
    {
        if(st->lchild != NULL)
        hl = shortestpath(st->lchild)+1;
        if(st->rchild != NULL)
        hr = shortestpath(st->rchild)+1;
        if(hl < hr)
            return hl;
        else
            return hr;
    }
}

void laytraverse(Tree st)
{

    Queue q;
    q.Front = 0;
    q.rear = 0;
    if(st != NULL)
        printf("%c ",st->item);
    q.bt[q.Front] = st;
    q.rear = q.rear+1;
    while(q.Front < q.rear)
    {
      st = q.bt[q.Front];
      q.Front = q.Front+1;    //每次移动一位,然后取这位节点的左右子节点,队列中相邻节点属于同一层!
      if(st->lchild != NULL)
      {
          q.bt[q.rear] = st->lchild;
          q.rear = q.rear+1;
          printf("%c ",st->lchild->item);
      }
      if(st->rchild != NULL)
      {
          q.bt[q.rear] = st->rchild;
          q.rear = q.rear+1;
          printf("%c ",st->rchild->item);
      }

    }
}

int  BTequal(Tree st1,Tree st2)
{
    if(st1 == NULL ^ st2 == NULL) //^是异或符号,之前的判断语句是:if(st1->item != st2->item)
        return 0;                   //此判断语句在st1 和st2 为NULL时“调试遇挫”
    else if(st1 == NULL && st2 == NULL)
        return 1;
    else if(st1->item == st2->item)
    {
        if(1 == BTequal(st1->lchild,st2->lchild))
           return BTequal(st1->rchild,st2->rchild);
        else
            return 0;
    }
}

void exchange(Tree st)
{
    if(st == NULL)
        return;
    else
        {
            BTree *tmp;
            tmp = st->lchild;
            st->lchild = st->rchild;
            st->rchild = tmp;
            exchange(st->lchild);
            exchange(st->rchild);
        }
}
int check(Tree p,Tree q)
{
    if(p != NULL && q == NULL)
        return 0;
    else if(p == NULL && q != NULL)
        return 0;
    else if(p != NULL && q != NULL)
    {
        if(p->item != q->item)
          return 0;
        else
        {
            if(check(p->lchild,q->rchild) == 1)
                return check(p->rchild,q->lchild);
        }
    }
    else 
    return 1;
}

int symmetry(Tree st)
{
   if(st == NULL)
        return 1;
   else
     return check(st->lchild,st->rchild);
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4728次
    • 积分:228
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条