[数据结构]第四次作业:二叉数的各种算法

/* ==============  Program Description  ============= */ 
/*               Freshare’s 4th of dswork             */ 
/* ================================================== */ 

#include "stdlib.h" 
#include "stdio.h" 
#define  DataType char 
#define  Status  int 
#define  ERROR  -1 
#define  OK  1 
#define  MAX  255 

typedef struct BiTNode   //定义类型 

    DataType data; 
    struct BiTNode *lchild,*rchild;  
}BiTNode,*BiTree; 
BiTree que[MAX],Tree,t; 
int front=0, rear=0;    //队列变量 

BiTree CreateBiTree(BiTree T) //CreatBiTree 

    char ch; 
    ch=getchar(); 
    if(ch==’ ’) T=NULL; 
    else{ 
        if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) 
            exit(ERROR); 
        T->data=ch; 
        T->lchild=CreateBiTree(T->lchild); 
        T->rchild=CreateBiTree(T->rchild); 
    } 
    return T; 


void preorder(BiTree root) 

    if (root!=NULL) 
    { 
        printf("%c",root->data); 
        preorder(root->lchild); 
        preorder(root->rchild); 
    } 


void inorder(BiTree root) 

    if (root!=NULL) 
    { 
        inorder(root->lchild); 
        printf("%c",root->data); 
        inorder(root->rchild); 
    } 


void postorder(BiTree root) 

    if (root!=NULL) 
    { 
        postorder(root->lchild); 
        postorder(root->rchild); 
        printf("%c",root->data); 
    } 


void enqueue(BiTree t)        //进队 
{ if(front!=(rear+1) % MAX) 
    { rear = (rear+1) % MAX; 
      que[rear]=t; 
     } 


BiTNode *delqueue() 
{ if (front==rear) return NULL; 
front=(front+1) % MAX; 
return (que[front]); 


void levelorder(BiTree t) 
{ BiTree p; 
 if(t!=NULL) 
 { 
  enqueue(t); 
    while(front!=rear) 
    { 
      p=delqueue( ); 
      printf("%c", p->data); 
      if(p->lchild!=NULL) enqueue(p->lchild); 
      if(p->rchild!=NULL) enqueue(p->rchild); 
     } 
  } 


BiTree findnode(BiTree t ,DataType x) 

    BiTree p; 
    if(!t) p=NULL; 
    else if(t->data==x) p=t; 
    else  
    { 
        p=findnode(t->lchild,x); 
        if(!p) p=findnode(t->rchild,x); 
    } 
   return (p); 


BiTree findparent (BiTree root ,BiTree q) 

    BiTree p; 
    if(!root) return (NULL); 
    else if (root->lchild==q||root->rchild==q) 
        return (root); 
    else 
    { 
        p=findparent(root->lchild,q); 
            if(!p)p=findparent(root->rchild,q); 
        return (p); 
    } 


int counter(BiTree root) 

    int s; 
    if(root==NULL) return (0); 
    else s=counter(root->lchild)+counter(root->rchild)+1; 
    return (s); 


int depth(BiTree t) 

    int dep1,dep2; 
    if (t==NULL) return (0); 
    else 
    { 
        dep1=depth(t->lchild); 
        dep2=depth(t->rchild); 
        if (dep1>dep2) return (dep1+1); 
        else return (dep2+1); 
    } 


void main() 

    char treen; 
    BiTree tmp; 
    Tree=CreateBiTree(Tree); 
    printf("该二叉树总结点数为:%d,高度为:%d/n前序遍历:",counter(Tree),depth(Tree)); 
    preorder(Tree); 
    printf("/n中序遍历:"); 
    inorder(Tree); 
    printf("/n后序遍历:"); 
    postorder(Tree); 
    printf("/n层序遍历:"); 
    levelorder(Tree); 
    printf("/n请输入一个节点:"); 
    getchar();getchar(); 
    treen=getchar(); 
    tmp=findnode(Tree,treen); 
    printf("它的父结点是:%c/n/n",findparent(Tree,tmp)->data); 
}

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值