當前位置:編程學習大全網 - 編程語言 - 關於數據結構C語言二叉樹的程序,請人幫忙看看~謝謝

關於數據結構C語言二叉樹的程序,請人幫忙看看~謝謝

給妳完全調好了,壹切正常運行:

#include "stdio.h"

#include "stdlib.h"

typedef int status; //C中沒有status類型,所以想使用這個類型妳必須定義它

#define OK 0

#define ERROR -1

#define OVERFLOW -2 //OK、OVERLFLOW、ERROR這些宏的定義頭文件中是沒有的,所以妳必須自己定義它們

typedef struct BiTNode{

char data;

struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;

typedef struct QNode{

BiTNode *data;

struct QNode *next; //馬虎。少個字符Q

}QNode,*QueuePtr;

typedef struct{

QueuePtr front;

QueuePtr rear;

}LinkQueue;

void CreateBiTree(BiTree &T)

{

char ch;

/*printf("Input the char\n"); //妳把輸出語句放到遞歸的函數裏它會輸出N多遍,所以,還是放到主函數裏吧 */

scanf("%c",&ch); //妳忘了取地址符了

/*if(ch == '#')T==NULL;*/ if(ch == '#')T=NULL;//是將T指針賦值為空,而不是T==NULL;

else{

if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))exit(OVERFLOW);

/**T.data=ch;*/T->data=ch; //樓主要仔細研究壹下指向運算符"->"和結構體成員運算符"."的區別,此程序中N多錯誤都是因為沒有區分它們引起的

CreateBiTree(T->lchild);

CreateBiTree(T->rchild);

}

}

status DLR(BiTree root) //void類型是不能返回值的,所以妳可以把函數改成status類型;函數參數不用引用。因為沒有改變參數值,只是使用

{

if(root!=NULL)

{

printf("%c",root->data);

DLR(root->lchild);

DLR(root->rchild); //這壹點屬於嚴重錯誤,說明妳沒有弄清遞歸遍歷的過程。是先根,再左,再右。下面還有三個同樣的錯誤

}

return OK;

}

status LDR(BiTree root) //函數參數不用引用。因為沒有改變參數值,只是使用

{

if(root!=NULL)

{

LDR(root->lchild);

printf("%c",root->data);

LDR(root->rchild); //同上

}

return OK;

}

status LRD(BiTree root) //函數參數不用引用。因為沒有改變參數值,只是使用

{

if(root!=NULL)

{

LRD(root->lchild);

LRD(root->rchild); //同上

printf("%c",root->data);

}

return OK;

}

int i=0;

int Found_SUM(BiTree root)

{

if(root!=NULL)

{

i++;

Found_SUM(root->lchild);

Found_SUM(root->rchild);//遞歸求結點數,和遍歷函數有什麽關系?

}

return i;

}

int j=0;

int FOUND_LEAVES(BiTree root)

{

if(root!=NULL)

{

if(!root->lchild&&!root->rchild)j++;

FOUND_LEAVES(root->lchild);

FOUND_LEAVES(root->rchild);

}

return j;

}

status InitQueue(LinkQueue &Q) //這兒應該是初始化隊列,而不是銷毀隊列吧。。。汗。。。妳原來的步驟是用來銷毀隊列Q的啊

{

Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));

if(!Q.front)exit(OVERFLOW);

Q.front->next=NULL;

return OK;

}

status EnQueue(LinkQueue &Q,BiTree e)

{

QueuePtr p=NULL;//p的定義。。

p=(QueuePtr)malloc(sizeof(QNode));//馬虎,少個右括號

if(!p) exit(OVERFLOW);

p->data=e;p->next=NULL;//馬虎,兩個語句要用";"分開

Q.rear->next=p;

Q.rear=p;

return OK;

}

status DeQueue(LinkQueue &Q,BiTree &e) //此處添加壹個引用參數e較為方便,妳原來的方法太復雜了

{

QueuePtr p;

if(Q.front==Q.rear)return ERROR;

p=Q.front->next;

e=p->data;

Q.front->next=p->next;

if(Q.rear==p)Q.rear=Q.front;

free(p);

return OK;

}

status LSCAN(BiTree root)

{

LinkQueue Q;

BiTree e=NULL;

InitQueue(Q);

EnQueue(Q,root);

while(Q.front<Q.rear) //如果隊列非空

{

DeQueue(Q,root);

printf("%c",root->data);

if(root->lchild)EnQueue(Q,root->lchild);

if(root->rchild)EnQueue(Q,root->rchild);

}

return OK;

}

void main()

{

BiTree T=NULL;//未定義T

T=(BiTree)malloc(sizeof(BiTNode));

int a,b;

printf("Input the char\n");

CreateBiTree(T);

printf("\n");

printf("先序遍歷二叉樹:\n");

DLR(T);

printf("\n");

printf("中序遍歷二叉樹:\n");

LDR(T);

printf("\n");

printf("後序遍歷二叉樹:\n");

LRD(T);

printf("\n");

printf("層序遍歷二叉樹:\n");

LSCAN(T);

/*LSCAN(T); // 不知妳為啥要用兩遍這個函數,給妳註釋掉了壹個*/

a=Found_SUM(T);

b=FOUND_LEAVES(T);

printf("葉子結點數目%d",b);printf("\n");

printf("結點總數%d",a);

printf("\n");

}

  • 上一篇:91熊貓桌面的更新說明
  • 下一篇:女生適合學網絡工程專業嗎 好就業嗎
  • copyright 2024編程學習大全網