#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]!='#') the printf ( " , " ); afterbetree (B, 2 * C + 2 ); the printf ( " ) " ); } } return ; } // output binary form generalized table void yezijiedian (betree & B, int C, int & D ) { IF (b.data [C] == ' # ' ) return ; IF (( 2 * C + . 1 )> . 6 ) { the printf ( " leaf node specifically: C% \ n- ", b.data [C]); D ++ ; return ; } yezijiedian (B, 2 * C + . 1 , D); yezijiedian (B, 2 * C + 2 , D); return ; } // output junction sequence leaves The number and specific values point 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 ' * ' ; } // input node at any point in the tree, the parent node returns the main () { betree B; BN = 0 ; int C = 0 ; int D = 0 ; char E, G; char A [] = { ' A ' , ' B ' , ' C ' , ' D ' , ' # ' , ' E ' , ' # ' }; creatbetree (B, A, C); the printf (" First order output tree; \ n- " ); printbetree (B, C); the printf ( " \ n- " ); afterbetree (B, C); the printf ( " \ n- " ); yezijiedian (B, C, D); the printf ( " the number of leaf nodes:% D \ n- " , D); the printf ( " Please input node: " ); Scanf ( " % C " , & G); char H = getpartents (B, C, G) ; IF (H <= ' Z ' && H> ='){' A the printf ( " this node is a parent node: C% \ n- ' , H); } the else the printf ( " absent parent node " ); }
#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 & lt); the printf ( " ) " ); } } } void print_rl (BT T *, char C) { IF (T-> Data == C) { the printf ( " left and right child nodes:% C " , T-> L); } the 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-); the printf ( " generalized table output tree is: \ n " ); PrintBinTree (Test); the printf ( " \ n- " ); the printf ( " Please input node: " ); Scanf ( " % C " , &c); print_rl(test,c); }