首页 > 编程知识 正文

【数据结构】--二叉链表

时间:2023-05-06 12:55:42 阅读:159131 作者:4056

cab c # # de # g # # f # # hln 123 fapcreatedsuccess! height=5. leaf=3. nodes=7. preorder is : abcde gf.in order is 3360 cbegdfa.postorder is 3360 cgefdba.thecountofais160 ABA //节点声明typedef struct node ///变量,然后单击{ char data; struct node *lchild,*rchild; } bnode,*btree; voidcreatetree(btreet ) { char ch; if((ch=getchar ) )=='# ' ) { T=NULL; }else{t=(bnode* ) malloc ) sizeof ) bnode ); T-data=ch; 创建树(t-lchild ); 创建树(t-rchild ); }voidpret(btreet ) ) if ) t!=null(printf('%c ',T-data ); pret(t-lchild ); pret(t-rchild ); (} //else printf (' ); } order (btreet ) if ) t!=null}{inorder(t-lchild ); printf('%c ',T-data ); inorder(t-rchild ); (} //else printf (' ); }voidpostorder(btreet ) ) if ) t!=null}{postorder(t-lchild ); postorder(t-rchild ); printf('%c ',T-data ); (} //else printf (' ); ({ int depth ) btreet ) ) intd; if(t==null ) d=0; }else{intdl=Depth(t-lchild ); intdr=Depth(t-rchild ); d=1(dldr? dl:dr; } return d; }intleaf(btreet ) if ) t==null ) return 0; //kongshuelseif (t-lchild==nullt-rchild==null ) return 1; 返回级别(t-rchild )级别(t-rchild ); }intnodes(btreet ) if ) t==null ) return 0; if(t-lchild )==null (t-rchild==null ) )返回1; return1nodes(t-lchild ) nodes ) t-rchild; }intfind(btreet,char ch ) if ) t==null ) { return 0; }elseif(t-data==ch ) return1find ) t-lchild,ch ) find ) t-rchild,ch ); } return 0; }voidoutput(btreet,int l ) ) if ) t!=null(for ) intI=1; il; I ) printf (' ); //if (t==空) return; //if(t-data=='# ' ) printf (' ); printf(%c(n ),T-data ); output(t-lchild,l 1 ); output(t-rchild,l 1 ); }}int main () ) { char ch; 结构节点*树; while(~scanf('%c ',ch ) ) if ) ch=='c ' ) { getchar; //getchar (; 创建树(tree; cout'Created success! ' endl; }elseif(ch=='h ' ) inth=depth(tree ); cout'Height='h'.'endl; }elseif(ch=='l ' ) cout'leaf='leaf(tree ).' endl; //coutleaf(tree ); }elseif(ch=='n ' ) cout'nodes='nodes(tree ); cout'.'endl; (elseif ) ch=='1' ) )//前置遍历) { cout'Preorder is: ); pret (树; cout'.'endl; }elseif(ch=='2' ) { cout'Inorder is: ); 序列(tree ); cout'.'endl; }elseif(ch=='3' ) { cout'Postorder is: ); postorder (树); cout'.'endl; }elseif(ch=='f ' ) { string s; cins; //cout'kk'kkendl; cout ' the count of ' s[0] ' is ' find (tree,s [0] ).' endl; }elseif(ch=='p ' ) { cout'The tree is:'endl; 输出(tree,1 ); } } return 0; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。