码迷,mamicode.com
首页 > 其他好文 > 详细

6.4数据结构

时间:2019-06-11 13:12:20      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:nts   lib   style   scanf   printf   顺序   clu   print   creat   

#include<stdio.h>
#define max 30
typedef struct{
    char data[max];
    int n;
}betree; 

void creatbetree(betree &b,char a[],int c){
    if(c<7){
        b.data[c]=a[c];
        if(a[c]!=#){
            b.n++;
        }
    }
    else return;
    creatbetree(b,a,2*c+1);
    creatbetree(b,a,2*c+2);
    return;
} 
 
void printbetree(betree &b,int c){
    if(c<7){
        printf("%c",b.data[c]);
    }
    else return;
    printbetree(b,2*c+1);
    printbetree(b,2*c+2);
    return;
}

void afterbetree(betree &b,int c){
    if(b.data[c]==;) return; 
    if(b.data[c]!=#){
        printf("%c",b.data[c]);
        if(2*c+2<7){
            printf("(");
            afterbetree(b,2*c+1);
            if(b.data[2*c+1]!=#&&b.data[2*c+2]!=#)
            printf(",");
            afterbetree(b,2*c+2);
            printf(")");
        }
    }
    return;
}//以广义表的形式输出二叉树

void yezijiedian(betree &b,int c,int &d){
    if(b.data[c]==#) return;
    if((2*c+1)>6){
        printf("叶子节点具体为:%c\n",b.data[c]);
        d++;
        return;
    }
    yezijiedian(b,2*c+1,d);
    yezijiedian(b,2*c+2,d);
    return;
}//输出顺序树叶子结点个数及具体值

char getpartents(betree &b,int c,int f){
    if(b.data[c]==#||2*c+1>6) return *;
    if(b.data[2*c+1]==f||b.data[2*c+2]==f) return b.data[c];
    char a=getpartents(b,2*c+1,f);
    char d=getpartents(b,2*c+2,f);
    if(a<=z&&a>=a) return a;
    else if(d<=z&&d>=a) return d;
    else return *;
}//输入树中任意点结,返回双亲节点

main(){
    betree b;
    b.n=0;
    int c=0;
    int d=0;
    char e,g;
    char a[]={a,b,c,d,#,e,#};
    creatbetree(b,a,c);
    printf("先序输出树;\n") ; 
    printbetree(b,c);
    printf("\n");
    afterbetree(b,c);
    printf("\n");
    yezijiedian(b,c,d);
    printf("叶子结点个数:%d\n",d);
    printf("请输入节点:");
    scanf("%c",&g);
    char h=getpartents(b,c,g);
    if(h<=z&&h>=a){
        printf("该节点双亲节点为:%c\n",h);
    }
    else printf("不存在双亲节点");
}
#include<stdio.h>
#include<stdlib.h> 
typedef struct A {
char data;
struct A *l,*r;
}bt;

void creatbt(bt *&T,char pre[],int &n)
{   char ch=pre[n++];  
    if(ch==;) return; 
    if(ch!=#) { 
    T=(bt *)malloc(sizeof(bt));
    T->data=ch;
    creatbt(T->l,pre,n);       
    creatbt(T->r,pre,n);   
    }
    else T=NULL;          
} 
        
void PrintBinTree(bt *t)
{   if(t!=NULL){ 
        printf("%c",t->data);
        if(t->l!=NULL||t->r!=NULL){
            printf("(");
            PrintBinTree(t->l);
            printf(",");
            PrintBinTree(t->r);
            printf(")");
            }
        } 
} 

void print_rl(bt *t,char c) {
    if(t->data==c){
        printf("左右孩子节点为:%c",t->l);
    }
    else{
        print_rl(t->l,c);
    }
    //bt *s;
    
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                

main(){
    bt *test;char c;  
    char a[]={a,b,#,#,c,#,#,;}; 
    int n=0;           
    creatbt(test,a,n);
    printf("广义表输出树为:\n");
    PrintBinTree(test); 
    printf("\n");
    printf("请输入节点:");
    scanf("%c",&c);
    print_rl(test,c);
}

 

6.4数据结构

标签:nts   lib   style   scanf   printf   顺序   clu   print   creat   

原文地址:https://www.cnblogs.com/huangjiaxin/p/10972748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!