用C语言建立一棵含有n个结点的二叉树,采用二叉链表存储,然后分别实现前序,中序,后序遍历该二叉树
\u6570\u636e\u7ed3\u6784 c\u8bed\u8a00\u7248\u4e8c\u53c9\u6811(1) \u5efa\u7acb\u4e00\u68f5\u542b\u6709n\u4e2a\u7ed3\u70b9\u7684\u4e8c\u53c9\u6811\uff0c\u91c7\u7528\u4e8c\u53c9\u94fe\u8868\u5b58\u50a8\uff1b#include
#include
typedef struct node *tree_pointer;
struct node{
char ch;
tree_pointer left_child,right_child;
};
tree_pointer root=NULL;
tree_pointer create(tree_pointer ptr)
{
char ch;
scanf("%c",&ch);
if(ch==' ')
ptr=NULL;
else{
ptr=(tree_pointer)malloc(sizeof(node));
ptr->ch=ch;
ptr->left_child=create(ptr->left_child);
ptr->right_child=create(ptr->right_child);
}
return ptr;
}
void preorder(tree_pointer ptr)
{
if(ptr){
printf("%c",ptr->ch);
preorder(ptr->left_child);
preorder(ptr->right_child);
}
}
void inorder(tree_pointer ptr)
{
if(ptr){
inorder(ptr->left_child);
printf("%c",ptr->ch);
inorder(ptr->right_child);
}
}
void postorder(tree_pointer ptr)
{
if(ptr){
postorder(ptr->left_child);
postorder(ptr->right_child);
printf("%c",ptr->ch);
}
}
void main()
{
printf("\u6784\u5efa\u4e00\u4e2a\u4e8c\u53c9\u6811\uff08\u7ed3\u70b9\u6570\u4e3an)\uff1a\n");
root=create(root);
printf("\u524d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\uff1a\n");
preorder(root);
printf("\n");
printf("\u4e2d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\uff1a\n");
inorder(root);
printf("\n");
printf("\u540e\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\uff1a\n");
postorder(root);
printf("\n");
}
#include
#include
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *lchild,*rchild;
}LNode,*TLNode;
void create(TLNode * Tree){ //\u521b\u5efa
ElemType e;
scanf("%d",&e);
if(e==0)
*Tree=NULL;
else{
(*Tree)=(TLNode)malloc(sizeof(LNode));
(*Tree)->data=e;
printf("input %d lchild: ",e);
create(&(*Tree)->lchild);
printf("input %d rchild: ",e);
create(&(*Tree)->rchild);
}
}
void print1(TLNode Tree){ //\u5148\u5e8f\u904d\u5386
if(Tree!=NULL){
printf("%d-",Tree->data);
print1(Tree->lchild);
print1(Tree->rchild);
}
}
void print2(TLNode Tree){ //\u4e2d\u5e8f\u904d\u5386
if(Tree!=NULL){
print2(Tree->lchild);
printf("%d-",Tree->data);
print2(Tree->rchild);
}
}
void print3(TLNode Tree){ //\u540e\u5e8f\u904d\u5386
if(Tree!=NULL){
print3(Tree->lchild);
print3(Tree->rchild);
printf("%d-",Tree->data);
}
}
int leaf=0; //\u6c42\u53f6\u5b50\u8282\u70b9\u6570
int depth(TLNode Tree){ //\u6df1\u5ea6
int s1,s2;
if(Tree==NULL)
return 0;
else{
s1=depth(Tree->lchild);
s2=depth(Tree->rchild);
if(s1==0 && s2==0) leaf++;
return (s1>s2?s1:s2)+1;
}
}
int Cnode(TLNode Tree){ //\u603b\u7ed3\u70b9
int s1,s2;
if(Tree==NULL)
return 0;
else{
s1=Cnode(Tree->lchild);
s2=Cnode(Tree->rchild);
return s1+s2+1;
}
}
void main(){
TLNode Tree;
printf("input \u6839\u8282\u70b9: ");
create(&Tree);
printf("\u5148\u5e8f\u904d\u5386:");
print1(Tree);
printf("\u4e2d\u5e8f\u904d\u5386");
print2(Tree);
printf("\u540e\u5e8f\u904d\u5386");
print3(Tree);
printf("\n\u6df1 \u5ea6:%d \n",depth(Tree));
printf("\u603b\u7ed3\u70b9\u6570:%d \n",Cnode(Tree));
printf("\u53f6\u5b50\u7ed3\u70b9\u6570:%d\n",leaf);
}
#include <stdlib.h>
#define max 100
typedef struct node{ //二叉树结构
char data;
struct node *lc,*rc; //左右子树
}bt,*list;
/*
二叉树
A
/ \
B C
/ \ \
D E F
/ / \
K G H
input
ABDK000E00C0FG00H00
ouput
ABDKECFGH
KDBEACGFH
KDEBGHFCA
*/
int creat(list*root){ //创建一棵二叉树,root使用的是二维指针
char n;
scanf(" %c",&n); //注%C前面加空格是为了起间隔作用 scanf不读入空格
if (n=='0') //0为间隔
{
*root=NULL; return 0; //输入结束
}
*root=(list)malloc(sizeof(bt));
if (!*root) return 0;
(*root)->data=n;
creat(&(*root)->lc);
creat(&(*root)->rc);
return 1;
}
int pre(list root){ //先序遍历
if (!root) return 0;
printf("%c",root->data);
pre(root->lc);
pre(root->rc);
return 1;
}
int mid(list root){ //中序遍历
if (!root) return 0;
mid(root->lc);
printf("%c",root->data);
mid(root->rc);
return 1;
}
int bh(list root){ //后序遍历
if (!root) return 0;
bh(root->lc);
bh(root->rc);
printf("%c",root->data);
return 1;
}
int sum(list root,int *cnt){ //求结点的个数sum
if(root){
(*cnt)++;
sum(root->lc,cnt);
sum(root->rc,cnt);
return 1;
}
return 0;
}
int sumleaf(list root,int *cnt){ //求叶子节点的个数
if (root)
{
if ((!root->lc)&&(!root->rc))
{ (*cnt)++; }
sumleaf(root->lc,cnt);
sumleaf(root->rc,cnt);
return 1;
}
return 0;
}
int deep(list root,int *cnt){ //求深度
if (!root)
{
*cnt=0; return 0;
}
int m,n;
n=m=*cnt;
deep(root->lc,&m);
deep(root->rc,&n);
*cnt=m+1;
if(m<n) *cnt=n+1;
return 1;
}
int floor(list root){ //层次遍历
if(root)
{
list s[max]; int front,rear; //用队进行存储
front=-1; rear=0; s[rear]=root; //初始化
while (rear!=front) //当队为空时结束
{
printf("%c",s[++front]->data);
if (s[rear]->lc)
{s[1+rear]=s[rear]->lc; rear++; } //将左子树存入队列中
if (s[rear]->rc)
{s[1+rear]=s[rear]->rc; rear++; } //将右子书存入队列中
}
return 1;
}
return 0;
}
int scop(list *r,list *p){ //树的复制
if(*r){
*p=(list)malloc(sizeof(bt));
if(*p){
(*p)->data=(*r)->data;
scop(&(*r)->lc,&(*p)->lc);
scop(&(*r)->rc,&(*p)->rc);
return 1;
}
}
*p=NULL;
return 0;
}
int sepect(list root,list *p,char e){ //查找节点返回指针*p p为二级指针
if(root){
if (root->data==e)
{ *p=root; return 1;
}
sepect(root->lc,p,e);
sepect(root->rc,p,e);
}
return 0;
}
void main(){
list b,s,m;
int n,t=1;
char ch='\0';
printf("***********Bitree************\n");
while(t){ //循环操作
printf("input a tree(int):\n");
s=m=b=NULL; //二叉树的初始化
creat(&b);
//按三种遍历输出二叉树
printf("\npre "); pre(b);
printf("\nmid "); mid(b);
printf("\nbh "); bh(b); printf("\n");
//求节点数目,叶子节点的个数,深度
n=0; sum(b,&n); printf("sumdata: %d\n",n);
n=0; sumleaf(b,&n); printf("sumleaf: %d\n",n);
n=0; deep(b,&n); printf("deep: %d\n",n);
//二叉树的复制
scop(&b,&s);
printf("\nscopy tree:\npre ");
pre(s); printf("\nmid ");
mid(s); printf("\nbh ");
bh(s); printf("\n");
//查找节点
printf("sepect a data:\n");
scanf(" %c",&ch); //注%C前面加空格是为了起间隔作用 scanf不读入空格
sepect(b,&m,ch);
if(m)
printf("sepect : %c \n",m->data);
else
printf("Error,no this data: %c\n",ch);
//继续则输入 1,退出输入 0
printf("continue input 1,break input 0:\n");
scanf("%d",&t);
}
}
绛旓細int creat(list*root){ //鍒涘缓涓妫浜屽弶鏍戯紝root浣跨敤鐨鏄簩缁存寚閽 char n;scanf(" %c",&n); //娉%C鍓嶉潰鍔犵┖鏍兼槸涓轰簡璧烽棿闅斾綔鐢 scanf涓嶈鍏ョ┖鏍 if (n=='0') //0涓洪棿闅 { root=NULL; return 0; //杈撳叆缁撴潫 } root=(list)malloc(sizeof(bt));if (!*root) return 0;(*root...
绛旓細postorder(ptr->right_child);printf("%c",ptr->ch);} } void main(){ printf("鏋勫缓涓涓簩鍙夋爲锛缁撶偣鏁颁负n)锛歕n");root=create(root);printf("鍓嶅簭閬嶅巻浜屽弶鏍戯細\n");preorder(root);printf("\n");printf("涓簭閬嶅巻浜屽弶鏍戯細\n");inorder(root);printf("\n");printf("鍚庡簭閬嶅巻浜...
绛旓細void InitList(ListNode* head)/*灏嗗崟閾捐〃鍒濆鍖栦负绌恒傚姩鎬佺敓鎴愪竴涓ご缁撶偣锛屽苟灏嗗ご缁撶偣鐨鎸囬拡鍩熺疆涓虹┖銆*/ { head=malloc(sizeof(ListNode));/*涓哄ご缁撶偣鍒嗛厤涓涓瀛樺偍绌洪棿*/ if(head==NULL){ return;} head->next = NULL; /*灏嗗崟閾捐〃鐨勫ご缁撶偣鎸囬拡鍩熺疆涓虹┖*/ } int ListEmpty(ListNode* ...
绛旓細瀹屽叏浜屽弶鏍戞槸涓绉嶇壒娈婄殑浜屽弶鏍戙傚畾涔夛細濡傛灉涓妫鍏锋湁n涓粨鐐圭殑娣卞害涓簁鐨勪簩鍙夋爲锛屽畠鐨勬瘡涓涓粨鐐归兘涓庢繁搴︿负k鐨勬弧浜屽弶鏍戜腑缂栧彿涓1~n鐨缁撶偣涓涓瀵瑰簲锛岃繖妫典簩鍙夋爲绉颁负瀹屽叏浜屽弶鏍戙備緥锛氱壒鐐癸細鍙跺瓙缁撶偣鍙彲鑳藉湪鏈澶х殑涓ゅ眰涓婂嚭鐜,瀵逛换鎰忕粨鐐癸紝鑻ュ叾鍙冲垎鏀笅鐨勫瓙瀛欐渶澶у眰娆′负L锛屽垯鍏跺乏鍒嗘敮涓嬬殑瀛愬瓩鐨勬渶澶...
绛旓細鍝堝か鏇肩紪鐮佹楠わ細涓銆佸缁欏畾鐨n涓鏉冨納W1,W2,W3,...,Wi,...,Wn}鏋勬垚n妫浜屽弶鏍戠殑鍒濆闆嗗悎F= {T1,T2,T3,...,Ti,...,Tn}锛屽叾涓瘡妫典簩鍙夋爲Ti涓彧鏈涓涓潈鍊间负Wi鐨勬牴缁撶偣锛屽畠鐨勫乏鍙冲瓙鏍戝潎涓虹┖銆傦紙涓烘柟渚垮湪璁$畻鏈轰笂瀹炵幇绠 娉曪紝涓鑸繕瑕佹眰浠i鐨勬潈鍊糤i鐨勫崌搴忔帓鍒椼傦級浜屻佸湪F涓夊彇涓ゆ5鏍...
绛旓細head ==NULL)锛岃繖閲岀殑鎰忔濇槸鍒ゆ柇鎸囬拡head鏈夋病鏈夋寚鍚戞煇涓妭鐐瑰湴鍧锛屼竴鑸敤浜庡垽鏂褰曢鑺傜偣銆俬ead锛漬ewnode锛宧ead鏄妭鐐规寚閽堬紝newnode鑷劧涔熸槸鑺傜偣鎸囬拡銆傚師鐮佷及璁℃槸杩唬涓嶆柇鐢ㄦ寚閽坣ewnode鎸囧悜鏂板缓鐨鑺傜偣绌洪棿锛屾瘮濡俷ewnode=(node *)malloc(sizeof(node));鍐嶆妸绗涓涓垱寤虹殑鑺傜偣鍦板潃浼犵粰head淇濆瓨銆
绛旓細include "string.h"include "stdlib.h"define NULL 0 typedef struct bitnode{ char data;struct bitnode *lchild,*rchild;}bitnode,*bitree;/*鍒涘缓涓涓浜屾潏鏍戜互#鍙风粨鏉*/ bitree create(bitree t){ char ch;ch=getchar();if(ch=='#')t=NULL;else{ t=(bitree)malloc(sizeof(bitnode));...
绛旓細浠庝竴涓叿鏈n涓粨鐐圭殑鍗曢摼琛ㄤ腑鏌ユ壘鍏跺肩瓑浜巟鐨勭粨鐐规椂锛屽湪鏌ユ壘鎴愬姛鐨勬儏鍐典笅锛岄渶骞冲潎姣旇緝锛圖銆侊紙n-1锛/2锛変釜鍏冪礌缁撶偣銆備竴绉嶉摼寮忓瓨鍙栫殑鏁版嵁缁撴瀯锛鐢ㄤ竴缁勫湴鍧浠绘剰鐨勫瓨鍌ㄥ崟鍏冨瓨鏀剧嚎鎬ц〃涓殑鏁版嵁鍏冪礌銆傞摼琛ㄤ腑鐨勬暟鎹槸浠ョ粨鐐规潵琛ㄧず鐨勩傛瘡涓粨鐐圭殑鏋勬垚锛氬厓绱狅紙鏁版嵁鍏冪礌鐨勬槧璞★級 +鎸囬拡锛堟寚绀哄悗缁у厓绱犲瓨鍌ㄤ綅缃級...
绛旓細杈撳叆涓涓湁n涓鍙缁撶偣鐨鏉冨兼瀯閫涓妫鍝堝か鏇兼爲 澶浖鏍戣鍥俱傜敤word闅忎究鐢荤殑锛屾瘮杈冮毦鐪嬨傚甫鏉冭矾寰勯暱搴 锛2 3锛*3 (5 7 9)*2 12*1=15 42 12=69 鍏跺疄浣犲彲浠ユ牴鎹笅闈㈢殑鐩存帴姹傘傚搱澶浖鏍戠殑鏋勯 鍋囪鏈塶涓潈鍊硷紝鍒欐瀯閫犲嚭鐨勫搱澶浖鏍戞湁n涓彾瀛愮粨鐐广 n涓潈鍊煎垎鍒涓 w1銆亀2銆佲︺亀n锛屽垯鍝堝か鏇...
绛旓細杩欎釜鍑芥暟瑕佽涓嬮噷銆傚悓鏃惰寮cstdlib澶存枃浠 u->next=NULL;}int main(){head=newnode();//鍒涘缓涓涓鏂扮殑鎸囬拡銆 tail=head;for (int i=1;i<=5;i++){tail->next=newnode();tail=tail->next;//浣犲彲浠ヨ緭鍏ユ暟鎹劧鍚庡瓨鍏ユ寚閽堜腑銆傛瘮濡俿canf("%d",&tail->age);鐒跺悗缁檛ail->num浠涔堢殑璧嬪笺 }...