二叉樹c語(yǔ)言主函數(shù) c語(yǔ)言實(shí)現(xiàn)二叉樹

C語(yǔ)言:建立二叉樹,在main方法里寫代碼調(diào)試?

#includestdlib.h

網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)的開發(fā),更需要了解用戶,從用戶角度來(lái)建設(shè)網(wǎng)站,獲得較好的用戶體驗(yàn)。成都創(chuàng)新互聯(lián)公司多年互聯(lián)網(wǎng)經(jīng)驗(yàn),見(jiàn)的多,溝通容易、能幫助客戶提出的運(yùn)營(yíng)建議。作為成都一家網(wǎng)絡(luò)公司,打造的就是網(wǎng)站建設(shè)產(chǎn)品直銷的概念。選擇成都創(chuàng)新互聯(lián)公司,不只是建站,我們把建站作為產(chǎn)品,不斷的更新、完善,讓每位來(lái)訪用戶感受到浩方產(chǎn)品的價(jià)值服務(wù)。

typedef?struct?node/*二叉鏈表結(jié)構(gòu)聲明*/

{

struct?node?*lchild;

char?data;

struct?node?*rchild;

}bitnode,*bitree;/*bitnode、bitree為該結(jié)構(gòu)體類型*/

bitree?CreatTree()/*創(chuàng)建二叉鏈表*/

{

char?a;

bitree?new;

scanf("%c",a);

if(a=='#')

return?NULL;

else

{

new=(bitree)malloc(sizeof(bitnode));

new-data=a;

new-lchild=CreatTree();/*遞歸創(chuàng)建左子樹*/

new-rchild=CreatTree();/*遞歸創(chuàng)建右子樹*/

}

return?new;

}

int?btreedepth(bitree?bt)/*自定義函數(shù)btreedepth()求二叉樹的深度*/

{

int?ldepth,rdepth;

if(bt==NULL)

return?0;

else

{

ldepth=btreedepth(bt-lchild);

rdepth=btreedepth(bt-rchild);

return?(ldepthrdepth?ldepth+1:rdepth+1);

}

}

int?ncount(bitree?bt)/*自定義函數(shù)ncount求結(jié)點(diǎn)的個(gè)數(shù)*/

{

if(bt==NULL)

return?0;

else?return(ncount(bt-lchild)+ncount(bt-rchild)+1);

}

int?lcount(bitree?bt)/*自定義函數(shù)lcount求葉子結(jié)點(diǎn)的個(gè)數(shù)*/

{

if(bt==NULL)

return?0;

else?if(bt-lchild==NULLbt-rchild==NULL)

return?1;

else?return(lcount(bt-lchild)+lcount(bt-rchild));

}

void?print(bitree?bt)/*自定義函數(shù)print用中序遍歷的方式輸出二叉樹結(jié)點(diǎn)內(nèi)容*/

{

if(bt!=NULL)

{

print(bt-lchild);

printf("%c",bt-data);

print(bt-rchild);

}

}

void?main()

{

bitree?root;

root=CreatTree();/*調(diào)用函數(shù)創(chuàng)建二叉鏈表*/

printf("contents?of?binary?tree:\n");

print(root);/*調(diào)用函數(shù)輸出結(jié)點(diǎn)內(nèi)容*/

printf("\ndepth?of?binary?tree:%d\n",btreedepth(root));/*調(diào)用函數(shù)輸出樹的深度*/

printf("the?number?of?the?nodes:%d\n",ncount(root));/*調(diào)用函數(shù)輸出樹中結(jié)點(diǎn)個(gè)數(shù)*/

printf("the?number?of?the?leaf?nodes:%d\n",lcount(root));/*調(diào)用函數(shù)輸出樹中葉子結(jié)點(diǎn)個(gè)數(shù)*/

}

求教二叉樹的main函數(shù)怎么寫合適:

我的能力也有限,學(xué)數(shù)據(jù)結(jié)構(gòu)過(guò)的時(shí)間有點(diǎn)久了,而且這個(gè)程序我讀的很吃力,沒(méi)用過(guò)這樣子的語(yǔ)言來(lái)寫呢,剛剛寫了個(gè)主類可是運(yùn)行還是有錯(cuò)誤,我又不會(huì)改,不好意思。。。我想要的主類大體是這樣寫的,你可以參考一下:

void main()

{

BiTree Tr;//這里定義的東西在這個(gè)程序里也不行,本來(lái)是想讓那個(gè)BinTree是個(gè)指針的,可是這個(gè)程序俺也不大會(huì)弄

printf("按前序次序輸入,以#表示為空:\n");

CreateBinTree(Tr,T,i);//這個(gè)括號(hào)里面的內(nèi)容我也不知該怎么寫,程序大體讀了讀 貌似不大會(huì)

printf("\n前序遍歷結(jié)果為:\n");

PreOrder(Tr);//反正括號(hào)里的內(nèi)容就是你前面寫的那個(gè)函數(shù)括號(hào)里相應(yīng)的

printf("\n中序遍歷結(jié)果為:\n");

InOrder(Tr);

printf("\n后序遍歷結(jié)果為:\n");

PostOder(Tr);

printf("\n層序遍歷結(jié)果為:\n");

LevelOrder(Tr);

printf("\n該二叉樹的深度為:\n%d",countHighOfBiTree(Tr));

printf("\n該二叉樹的葉子節(jié)點(diǎn)個(gè)數(shù)為:\n");

countNumOfLeaf(Tr);

printf("\n該二叉樹的所有結(jié)點(diǎn)數(shù)為:\n");

Count(Tr);

printf("\n");

}

這里是實(shí)驗(yàn)課上老師布置給我們的,然后自己寫的,語(yǔ)言和你的不大一樣 但思路差不多,你可以看看這個(gè)的,畢竟我還是學(xué)的時(shí)候思路比較清晰啦,嘿嘿,貌似~是按前序序列來(lái)創(chuàng)建的二叉樹,你輸入的前序序列一定要是正確的哦~我的這個(gè)程序還很低級(jí),錯(cuò)誤的它不會(huì)提示,不好意思哈,學(xué)習(xí)不大好,只能幫到這里了

#include "stdio.h"

#include "conio.h"

#include "stdlib.h"

#includemalloc.h

#includestdlib.h

//#includestdio.h

#includestring.h

#define NULL 0

typedef char Elemtype;

typedef struct BinNode

{

Elemtype data;

struct BinNode *lchild,*rchild;//左右孩子指針

}BinTNode,*BinTree;

//按前序構(gòu)造二叉樹鏈表表示的二叉樹序列

BinTree CreateBinTree(BinTree T)

{

char ch;

scanf("%c,\n",ch);

if(ch=='#') T=NULL;

else

{

T=(BinNode *)malloc(sizeof(BinNode));

T-data=ch;//生成根結(jié)點(diǎn)

CreateBinTree(T-lchild);//生成左子樹

CreateBinTree(T-rchild);//生成右子樹

}//if

return T;

}//CreateBinTree

void Visit(char dataa)

{

printf("%c",dataa);

}

//前序遍歷二叉樹

void PreOrderTraverse(BinTree T)

{

//前序遍歷二叉樹T的遞歸算法,Visit是訪問(wèn)數(shù)據(jù)元素的函數(shù)

if(T)//二叉樹非空

{

Visit(T-data);//訪問(wèn)根結(jié)點(diǎn)

PreOrderTraverse(T-lchild);//前序遍歷左子樹

PreOrderTraverse(T-rchild);//前序遍歷右子樹

}//if

}//PreOrderTraverse

//中序遍歷二叉樹

void InOrderTraverse(BinTree T)

{

//中序遍歷二叉樹T的遞歸算法,Visit是訪問(wèn)數(shù)據(jù)元素的函數(shù)

if(T)//二叉樹非空

{

InOrderTraverse(T-lchild);//中序遍歷左子樹

Visit(T-data);//訪問(wèn)根結(jié)點(diǎn)

InOrderTraverse(T-rchild);//中序遍歷右子樹

}//if

}//InOrderTraverse

void PostOrderTraverse(BinTree T)

{

//后序遍歷二叉樹T的遞歸算法,visit是訪問(wèn)數(shù)據(jù)元素的函數(shù)

if(T)//二叉樹非空

{

PostOrderTraverse(T-lchild);//后序遍歷左子樹

PostOrderTraverse(T-rchild);//后序遍歷右子樹

Visit(T-data);//訪問(wèn)根結(jié)點(diǎn)

}//if

}//PostOrderTraverse

//求二叉樹的深度

int Depth(BinTree T)

{

int DepthLeft,DepthRight,depthval;

if(!T)

return 0;

else

{

DepthLeft=Depth(T-lchild);

DepthRight=Depth(T-rchild);

depthval=1+(DepthLeftDepthRight?DepthLeft:DepthRight);

return depthval;

}//if

}//Depth

void CountLeaf(BinTree T,int count0,int count2)

{

//統(tǒng)計(jì)二叉樹中的葉子節(jié)點(diǎn)個(gè)數(shù)

if(T)

{

if((!T-lchild)(!T-rchild))

count0++;

CountLeaf(T-lchild,count0,count2);

CountLeaf(T-rchild,count0,count2);

}

count2=count0-1;

}

void Countduone(BinTree T,int count1)

{

//統(tǒng)計(jì)二叉樹中度為1的結(jié)點(diǎn)個(gè)數(shù)

if(T)

{

if(((!T-lchild)(T-rchild))||((T-lchild)(!T-rchild)))

count1++;

Countduone(T-lchild,count1);

Countduone(T-rchild,count1);

}

}

int ZongNode(int a,int b,int c)

{

return (a+b+c);

}

void main()

{

BinTree Tr;

int count0,count1,count2;

int jie;

count0=0;

count1=0;

count2=0;

printf("按前序次序輸入,以#表示為空:\n");

CreateBinTree(Tr);

printf("\n前序遍歷結(jié)果為:\n");

PreOrderTraverse(Tr);

printf("\n中序遍歷結(jié)果為:\n");

InOrderTraverse(Tr);

printf("\n后序遍歷結(jié)果為:\n");

PostOrderTraverse(Tr);

printf("\n該二叉樹的深度為:\n%d",Depth(Tr));

printf("\n該二叉樹的葉子節(jié)點(diǎn)個(gè)數(shù)為:\n");

CountLeaf(Tr,count0,count2);

printf("%d",count0);

printf("\n該二叉樹的所有結(jié)點(diǎn)數(shù)為:\n");

//CountLeaf(Tr,count0,count2);

Countduone(Tr,count1);

jie=ZongNode(count1,count2,count0);

printf("%d",jie);

printf("\n");

}

C語(yǔ)言二叉樹遞歸算法怎么做?

#include?stdio.h

#include?string.h

struct?treenode{

int?value;

treenode*?left;

treenode*?right;

};

typedef?treenode*?BiTree;

void?visit(treenode*?node)

{

printf("%2d?",?node-value);

}

//????結(jié)點(diǎn)總數(shù)?

int?node(BiTree?T)

{

if(?!T?){

return?0;

}

return?node(T-left)?+?node(T-right)?+?1;

}

//????前序?

void?preOrder(BiTree?T)

{

if(?T?){

visit(T);

preOrder(T-left);

preOrder(T-right);????

}

}

//????中序

void?inOrder(BiTree?T)

{

if(?T?){

inOrder(T-left);

visit(T);

inOrder(T-right);????

}????

}?

//????后序

void?postOrder(BiTree?T)

{

if(?T?){

postOrder(T-left);

postOrder(T-right);????

visit(T);

}????

}?

//????葉子節(jié)點(diǎn)數(shù)

int?leafnode(BiTree?T)

{

if(?T?){

if(?!T-left??!T-right?)

return?1;

else

leafnode(T-left)?+?leafnode(T-right);????

}else{

return?0;

}

}?

int?height(BiTree?T)

{

if(?T?){

int?lh?=?height(T-left);

int?rh?=?height(T-right);

return?(lhrh???lh:rh)?+?1;

}else{

return?0;

}

}

int?main()

{

return?0;

}

二叉樹的基本操作 C語(yǔ)言版的

#include iostream.h

typedef struct BiTNode

{

char data;

int bit;

struct BiTNode *lchild,*rchild,*parent;

}BiTNode;

void InitBT(BiTNode *t)//1、初始化,不帶頭結(jié)點(diǎn)

{

t=NULL;

}

/*void InitBT(BiTNode *t)//初始化,帶頭結(jié)點(diǎn)

{

t=new BiTNode;

t-lchild=t-rchild=t-parent=NULL;

}*/

int EmptyBT(BiTNode *t)//判斷隊(duì)空

{

if(t==0)

return 1;

else

return 0;

}

BiTNode *creatBT(BiTNode *t,int b)//2、創(chuàng)建二叉樹

{

BiTNode *p;

char ch;

cinch;

if(ch=='#')return 0;

else

{

p=new BiTNode;

p-data=ch;

p-parent=t;

p-bit=b;

t=p;

t-lchild=creatBT(t,0);

t-rchild=creatBT(t,1);

}

return t;

}

void preorder(BiTNode *t)//3、先序遍歷

{

if(!EmptyBT(t))

{

coutt-data;

preorder(t-lchild);

preorder(t-rchild);

}

}

void inorder(BiTNode *t)//中序遍歷

{

if(!EmptyBT(t))

{

inorder(t-lchild);

coutt-data;

inorder(t-rchild);

}

}

void postorder(BiTNode *t)//后序遍歷

{

if(!EmptyBT(t))

{

postorder(t-lchild);

postorder(t-rchild);

coutt-data;

}

}

void coutBT(BiTNode *t,int m,int n,int i)//4、計(jì)算二叉樹中葉子結(jié)點(diǎn)、度為2的結(jié)點(diǎn)和度為1的結(jié)點(diǎn)的個(gè)數(shù)

{

if(!EmptyBT(t))

{

if((t-lchild==0) (t-rchild==0))

m++;//葉子結(jié)點(diǎn)

else if((t-lchild!=0) (t-rchild!=0))

i++;//度為2的結(jié)點(diǎn)

else

n++;//度為1的結(jié)點(diǎn)

coutBT(t-lchild,m,n,i);

coutBT(t-rchild,m,n,i);

}

}

void coutNode(BiTNode *t,int k)//5、求二叉樹中結(jié)點(diǎn)個(gè)數(shù)

{

if(!EmptyBT(t))

{

k++;

coutNode(t-lchild,k);

coutNode(t-rchild,k);

}

}

int BTdepth(BiTNode *t)//6、求二叉樹的深度

{

int i,j;

if(EmptyBT(t))

return 0;

else

{

i=BTdepth(t-lchild);

j=BTdepth(t-rchild);

return (ij?i:j)+1;

}

}

int Xdepth(BiTNode *t,char x)//7、查找x的層數(shù)

{

int num1,num2,n;

if(t==NULL)

return 0;

else{

if(t-data==x)

return 1;

num1=Xdepth(t-lchild,x);

num2=Xdepth(t-rchild,x);

n=num1+num2;

if(num1!=0||num2!=0)

n++;

return n;

}

}

static int flag;

void SearchChild(BiTNode *t,int k)//8、查找第k個(gè)結(jié)點(diǎn)的左右孩子

{

if(!EmptyBT(t))

{

if(k==0)

{

cout"位置不能為0!"endl;

return;

}

else

{

flag++;

if(flag==k)

{

if(t-lchild==0)

cout"無(wú)左孩子! ";

else

cout"左孩子為:"(t-lchild-data)" ";

if(t-rchild==0)

cout"無(wú)右孩子!"endl;

else

cout"右孩子為:"(t-rchild-data)endl;

}

else

{

SearchChild(t-lchild,k);

SearchChild(t-rchild,k);

}

}

}

}

int Xancestor(BiTNode *t,char x)//9、查找x結(jié)點(diǎn)祖先

{

int n,num1,num2;

if(t==NULL)

return 0;

else

{

if(t-data==x)

return 1;

num1=Xancestor(t-lchild,x);

num2=Xancestor(t-rchild,x);

n=num1+num2;

if(n!=0)

{

n++;

coutt-data" "endl;

}

}

}

void BTNodePath(BiTNode *t)//10、輸出所有葉子結(jié)點(diǎn)路徑

{

if(!EmptyBT(t))

{

if((t-lchild==0) (t-rchild==0))

{

coutt-data"的路徑為:";

for(BiTNode *p=t;p!=0;p=p-parent)

coutp-data;

coutendl;

}

else

{

BTNodePath(t-lchild);

BTNodePath(t-rchild);

}

}

}

void BTNodebit(BiTNode *t)//11、輸出所有葉子結(jié)點(diǎn)編碼

{

if(!EmptyBT(t))

{

if((t-lchild==0) (t-rchild==0))

{

coutt-data"的編碼為:";

for(BiTNode *p=t;p-parent!=0;p=p-parent)

coutp-bit;

coutendl;

}

else

{

BTNodebit(t-lchild);

BTNodebit(t-rchild);

}

}

}

void main()

{

BiTNode *t;

int m,n,i,d,q,k;

char x;

cout"1、初始化..."endl;

InitBT(t);

cout"2、創(chuàng)建二叉樹..."endl;

t=creatBT(t,0);

cout"3.1、先序遍歷..."endl;

preorder(t);

coutendl;

cout"3.2、中序遍歷..."endl;

inorder(t);

coutendl;

cout"3.3、后序遍歷..."endl;

postorder(t);

coutendl;

m=n=i=0;

cout"4、計(jì)算葉子結(jié)點(diǎn),度為1的結(jié)點(diǎn)和度為2的結(jié)點(diǎn)的個(gè)數(shù)..."endl;

coutBT(t,m,n,i);

cout"葉子結(jié)點(diǎn)個(gè)數(shù)為:"mendl;

cout"度為1的結(jié)點(diǎn)個(gè)數(shù)為:"nendl;

cout"度為2的結(jié)點(diǎn)個(gè)數(shù)為:"iendl;

q=0;

cout"5、計(jì)算結(jié)點(diǎn)個(gè)數(shù)..."endl;

coutNode(t,q);

cout"結(jié)點(diǎn)個(gè)數(shù)為:"qendl;

d=0;

cout"6、計(jì)算深度..."endl;

d=BTdepth(t);

cout"深度為:"dendl;

cout"7、求x的層數(shù)..."endl;

cout"輸入x:";

cinx;

if(Xdepth(t,x)==0)

cout"x不存在!"endl;

else

coutXdepth(t,x)endl;

cout"8、輸入要查找孩子的結(jié)點(diǎn)在先序遍歷中的位置k(不等于0):";

cink;

SearchChild(t,k);

if(kflag)

cout"位置超出長(zhǎng)度!"endl;

cout"9、查詢結(jié)點(diǎn)的所有祖先,請(qǐng)輸入結(jié)點(diǎn)x:";

cinx;

int num;

num=Xancestor(t,x);

if(num==0)

cout"結(jié)點(diǎn)不存在!"endl;

if(num==1)

cout"根結(jié)點(diǎn)無(wú)祖先!"endl;

cout"10、輸出所有葉子結(jié)點(diǎn)路徑(葉→根):"endl;

BTNodePath(t);

cout"11、輸出所有葉子結(jié)點(diǎn)編碼(葉→根):"endl;

BTNodebit(t);

}

二叉樹建立中函數(shù)定義與運(yùn)行(C語(yǔ)言)

大多數(shù)問(wèn)題是函數(shù)名字寫錯(cuò) 了。

#includestdio.h

#includestdlib.h

#define OK 1

#define ERROR 0

#define OVERFLOW -1

typedef int Status;

typedef char TElemType;

typedef struct BiTNode{

TElemType data;

struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;

Status CreateBitree(BiTree T);

Status PreOrder(BiTree T);

Status InOrder(BiTree T);

Status CreateBitree(BiTree T)

{

char ch;

scanf("%c",ch);

if(ch=='#')

T=NULL;

else{

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

if(!T);

exit(OVERFLOW);

T-data=ch;

CreateBitree(T-lchild);

CreateBitree(T-rchild);

}

return OK;

}

Status PreOrder(BiTree T)

{

if(T){

printf("%c",T-data);

PreOrder(T-lchild);

PreOrder(T-rchild);

}

return OK;

}

Status InOrder(BiTree T)

{

if(T){

InOrder(T-lchild);

printf("%c",T-data);

InOrder(T-rchild);

}

return OK;

}

Status PostOder(BiTree T)

{

if(T){

PostOder(T-lchild);

PostOder(T-rchild);

printf("%c",T-data);

}

return OK;

}

int main()

{

BiTree T={'\0'};

printf("先序建樹:依次輸入二叉樹結(jié)點(diǎn)號(hào),孩子為空時(shí)輸入空格\n");

CreateBitree(T);

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

PreOrder(T);

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

InOrder(T);

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

PostOder(T);

return 0;

}

二叉樹前、中、后遍歷后要用括號(hào)表示法輸出;主函數(shù)怎么寫啊。

#include iostream

using std::cin;

using std::cout;

using std::endl;

//using namespace std;

typedef struct BiTNode {

char data;

struct BiTNode *Lchild, *Rchild; // 左、右孩子指針

} *BiTree;

void CreateBiTree(BiTree T){

以B為根節(jié)點(diǎn)的左子樹 A根節(jié)點(diǎn) 以C為根節(jié)點(diǎn)的右子樹

以D為根節(jié)點(diǎn)的左子樹 B根節(jié)點(diǎn) 以E為根節(jié)點(diǎn)的右子樹

以G為根節(jié)點(diǎn)的左子樹 D根節(jié)點(diǎn) 以H為根節(jié)點(diǎn)的右子樹

以K為根節(jié)點(diǎn)的左子樹 C根節(jié)點(diǎn) 以F為根節(jié)點(diǎn)的右子樹

以I為根節(jié)點(diǎn)的左子樹 F根節(jié)點(diǎn) 右子樹為空

左子樹為空 I根節(jié)點(diǎn) 以J為根節(jié)點(diǎn)的右子樹

擴(kuò)展資料:

主函數(shù)的兩個(gè)形參形式中的形參,允許從執(zhí)行環(huán)境中傳遞任意的多字節(jié)字符串(它們通常被稱為命令行參數(shù)),各個(gè)指針 argv[1] .. argv[argc-1] 指向每個(gè)這些字符串的第一個(gè)字符。argv[0] 是指向一個(gè)表示用于執(zhí)行該程序自身的名字的空結(jié)尾多字節(jié)字符串(或者當(dāng)執(zhí)行環(huán)境不支持時(shí),為空字符串 "")的開頭字符的指針。

這些字符串是可以改動(dòng)的,雖然對(duì)它們的改動(dòng)并不會(huì)被傳回給執(zhí)行環(huán)境:比如可以用 std::strtok 來(lái)使用它們。由 argv 所指向的數(shù)組的大小至少為 argc+1,其最后一個(gè)元素 argv[argc] 保證為一個(gè)空指針。

參考資料來(lái)源:百度百科-main函數(shù)

網(wǎng)站欄目:二叉樹c語(yǔ)言主函數(shù) c語(yǔ)言實(shí)現(xiàn)二叉樹
本文地址:http://muchs.cn/article14/hgejge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化電子商務(wù)、網(wǎng)站策劃、外貿(mào)建站、網(wǎng)站營(yíng)銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)