普通樹的深度遍歷c語言,樹的層次遍歷c語言

本文目錄一覽:

C語言用三種不同的方法遍歷二叉樹並用兩種方式排序

以下是我的數據結構實驗的作業:肯定好用,裏面還包括了統計樹的深度和葉子數!記住每次做完一個遍歷還要重新輸入你的樹哦!

#include “stdio.h”

#include “string.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{

if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))

printf(“Error!”);

T-data=ch;

T-lchild=Create(T-lchild);

T-rchild=Create(T-rchild);

}

return T;

}

void Preorder(BiTree T){

if(T){

printf(“%c”,T-data);

Preorder(T-lchild);

Preorder(T-rchild);

}

}

int Sumleaf(BiTree T){

int sum=0,m,n;

if(T){

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

sum++;

m=Sumleaf(T-lchild);

sum+=m;

n=Sumleaf(T-rchild);

sum+=n;

}

return sum;

}

void zhongxu(BiTree T){

if(T){

zhongxu(T-lchild);

printf(“%c”,T-data);

zhongxu(T-rchild);

}

}

void houxu(BiTree T){

if(T){

houxu(T-lchild);

houxu(T-rchild);

printf(“%c”,T-data);

}

}

int Depth(BiTree T){

int dep=0,depl,depr;

if(!T) dep=0;

else{

depl=Depth(T-lchild);

depr=Depth(T-rchild);

dep=1+(depldepr?depl:depr);

}

return dep;

}

main(){

BiTree T;

int sum,dep;

T=Create(T);

Preorder(T);

printf(“\n”);

zhongxu(T);

printf(“\n”);

houxu(T);

printf(“\n”);

sum=Sumleaf(T);

printf(“%d”,sum);

dep=Depth(T);

printf(“\n%d”,dep);

}

在Turbo C的環境下,先按Ctrl+F9運行程序,此時就是建立二叉樹的過程,例如輸入序列ABC##DE#G##F###(其中的「#」表示空,並且輸入過程中不要加回車,因為回車也有對應的ASCII碼,是要算字符的,但是輸入完之後可以按回車退出),然後再按ALT+F5顯示用戶界面,這時候就能夠看到結果了。

另外你必須會手動建立一棵二叉樹,保證你輸入的序列能構成一棵二叉樹,否則你怎麼輸入,按最後按多少回車程序也不會結束!

C語言創建二叉樹 遍歷 求深度 求解!!

測試結果:

At the first,we create a tree

Please input nodes of tree

a

b

c

#

#

d

e

#

g

#

#

f

#

#

#

abcdegf

cbegdfa

cgefdba

The count of leaves is: 3

The height of tree is 5

請按任意鍵繼續. . .

【樓主】 建樹的函數是一個遞歸的過程

我上面測試的這顆樹

a

/

b

/ \

c d

/ \

e f

\

g

仔細看我的建樹的時候的輸入!

為了測試上面的5個函數,我把case去掉了。你補上去!

【你的CreateBinTree()】是沒有參數的,所以你後面的申明不能帶參數

測試代碼:

#includestdio.h

#includestdlib.h

typedef struct bnode{

char data;

struct bnode *lchild;

struct bnode *rchild;

}*BTree,Bnode;

BTree CreateBinTree()

{

char ch;

BTree t;

ch=getchar();

getchar();

if(ch==’#’)

t=NULL;

else

{

t=(BTree)malloc(sizeof(Bnode));

t-data=ch;

t-lchild=CreateBinTree();

t-rchild=CreateBinTree();

}

return t;

}

void PreOrder(BTree t)

{

if(t)

{

printf(“%c”,t-data);

PreOrder(t-lchild);

PreOrder(t-rchild);

}

}

void InOrder(BTree t)

{

if(t)

{

InOrder(t-lchild);

printf(“%c”,t-data);

InOrder(t-rchild);

}

}

void PostOrder(BTree t)

{

if(t)

{

PostOrder(t-lchild);

PostOrder(t-rchild);

printf(“%c”,t-data);

}

}

int Height(BTree t)

{

int h1,h2;

if(t==NULL)

return 0;

else

{

h1=Height(t-lchild);

h2=Height(t-rchild);

if(h1h2)

return h1+1;

else

return h2+1;

}

}

int LeafCount(BTree t)

{

int count1,count2;

if(t==NULL)

return 0;

else

{

if(t-lchild==NULLt-rchild==NULL)

return 1;

else

{

count1=LeafCount(t-lchild);

count2=LeafCount(t-rchild);

return count1+count2;

}

}

}

main()

{ BTree root;/*定義指向樹的指針*/

int t=1;

printf(“At the first,we create a tree\n”);

printf(“Please input nodes of tree\n”);

root=CreateBinTree();//調用函數創建樹

if (root==NULL) printf(“It’s an empty tree!\n”);

else

{

PreOrder(root); printf(“\n”);

InOrder(root); printf(“\n”);

PostOrder(root); printf(“\n”);

printf(“\nThe count of leaves is: %d \n”,LeafCount(root));

printf(“\nThe height of tree is %d \n”,Height(root));

system(“pause”);

}

}

C語言二叉樹的創建和遍歷

我寫了一個二叉樹 你給看看 一定能行的 我自己用了

#include “stdio.h”

#include “malloc.h”

#include “string.h”

#include “stdlib.h”

#define Max 20 //結點的最大個數

typedef struct BinTNode{

char data;

struct BinTNode *lchild,*rchild;

}BinTNode,*BinTree; //自定義二叉樹的結點類型

//定義二叉樹的指針

int NodeNum,leaf; //NodeNum為結點數,leaf為葉子數

//==========以廣義表顯示二叉樹==============

void DisTree(BinTree T)

{

if(T)

{

printf(“%c”,T-data);

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

{

if(T-lchild)

{

printf(“%c”,'(‘);

DisTree(T-lchild);

}

if(T-rchild)

{

printf(“%c”,’,’);

DisTree(T-rchild);

printf(“%c”,’)’);

}

}

}

}

//==========基於先序遍歷算法創建二叉樹==============

//=====要求輸入先序序列,其中加入虛結點”#”以示空指針的位置==========

BinTree CreatBinTree(BinTree T)

{

char ch;

ch=getchar();

if(ch==’#’)

T=NULL;

else

{

if(!(T=(BinTNode *)malloc(sizeof(BinTNode))))

printf(“Error!”);

T-data=ch;

T-lchild=CreatBinTree(T-lchild);

T-rchild=CreatBinTree(T-rchild);

}

return T;

}

//========NLR 先序遍歷=============

void Preorder(BinTree T)

{

if(T)

{

printf(“%c”,T-data);

Preorder(T-lchild);

Preorder(T-rchild);

}

}

//========LNR 中序遍歷===============

void Inorder(BinTree T)

{

if(T){

Inorder(T-lchild);

printf(“%c”,T-data);

Inorder(T-rchild);

}

}

//==========LRN 後序遍歷============

void Postorder(BinTree T)

{

if(T){

Postorder(T-lchild);

Postorder(T-rchild);

printf(“%c”,T-data);

}

}

//=====採用後序遍歷求二叉樹的深度、結點數及葉子數的遞歸算法========

int TreeDepth(BinTree T)

{

int hl,hr,max;

if(T){

hl=TreeDepth(T-lchild); //求左深度

hr=TreeDepth(T-rchild); //求右深度

max=hlhr? hl:hr; //取左右深度的最大值

NodeNum=NodeNum+1; //求結點數

if(hl==0hr==0)

leaf=leaf+1; //若左右深度為0,即為葉子。

return(max+1);

}

else return(0);

}

//====利用”先進先出”(FIFO)隊列,按層次遍歷二叉樹==========

void Levelorder(BinTree T)

{

int front=0,rear=1;

BinTNode *cq[Max],*p; //定義結點的指針數組cq

cq[1]=T; //根入隊

while(front!=rear)

{

front=(front+1)%NodeNum;

p=cq[front]; //出隊

printf(“%c”,p-data); //出隊,輸出結點的值

if(p-lchild!=NULL){

rear=(rear+1)%NodeNum;

cq[rear]=p-lchild; //左子樹入隊

}

if(p-rchild!=NULL){

rear=(rear+1)%NodeNum;

cq[rear]=p-rchild; //右子樹入隊

}

}

}

//==========主函數=================

void main()

{

BinTree T,root;

int i,depth;

printf(“\n”);

printf(“輸入完全二叉樹的先序序列:”); //輸入完全二叉樹的先序序列,

// 用#代表虛結點,如ABD###CE##F##

root=CreatBinTree(T); //創建二叉樹,返回根結點

DisTree(root);

printf(“\n”);

do //從菜單中選擇遍歷方式,輸入序號。

{

printf(“\t********** 菜單 ************\n”);

printf(“\n”);

printf(“\t1: 先序遍歷\n”);

printf(“\t2: 中序遍歷\n”);

printf(“\t3: 後序遍歷\n”);

printf(“\t4: 該樹的深度,結點數,葉子數\n”);

printf(“\t5: 層次遍歷\n”); //按層次遍歷之前,先選擇4,求出該樹的結點數。

printf(“\t0: 退出\n”);

printf(“\t*******************************\n”);

scanf(“%d”,i);

//輸入菜單序號(0-5)

switch(i)

{

case 1: {printf(“Print Bin_tree Preorder: “);

Preorder(root); //先序遍歷

}break;

case 2: {printf(“Print Bin_Tree Inorder: “);

Inorder(root); //中序遍歷

}break;

case 3: {printf(“Print Bin_Tree Postorder: “);

Postorder(root); //後序遍歷

}break;

case 4: {depth=TreeDepth(root); //求樹的深度及葉子數

printf(“樹深=%d 樹總結點數=%d”,depth,NodeNum);

printf(” 樹葉子數=%d”,leaf);

}break;

case 5: {printf(“LevePrint Bin_Tree: “);

Levelorder(root); //按層次遍歷

}break;

default: exit(1);

}

}while(i=0i6);

}

兄弟你看看 不懂再往下留言 記得給我的勞動成果一點點獎勵哦!!

C語言二叉樹遍歷程序

先看下creat這個函數:

status creat(bitnode *t)/*先序建立二叉樹*/

{

char ch;

ch=getch();putch(ch);

if(ch==’0′) t=NULL;

else

{

t=(bitnode *)malloc(sizeof(bitnode));

if(!t)

exit(OVERFLOW);

t-data=ch;

creat(t-lchild);

creat(t-rchild);

}

return OK;

}

其中有句代碼是t=(bitnode *)malloc(sizeof(bitnode));

這是給t賦值,由於t是參數,這樣做是不能返回的。

我知道你的意思是想通過指針返回,但是那樣的用法應該是對t所指向的變量賦值,也就是對*t賦值。

如果你還沒理解的話看下函數里的遞歸調用:creat(t-lchild);調用函數後,本意是要給t-lchild賦值的,但是是做不到的,因為要改變一個變量的值的話,應該傳的是它的地址。

可能你覺得有點亂了,我舉個函數中用指針做參數來返回的例子:

假如要用指針返回一個整型的變量,那麼指針應該是指向整型變量的,即int*

這裡應該是要返回一個struct bitnode *類型的,也就是返回的值就是個指針,那麼參數就應該是一個指向這種指針的指針,即struct bitnode **

可以這麼修改:

status creat(bitnode **t) //多了個*

{

char ch;

ch=getch();putch(ch);

if(ch==’0′) *t=NULL; //多了個*

else

{

*t=(bitnode *)malloc(sizeof(bitnode)); //多了個*

if(!*t) //多了個*

exit(OVERFLOW);

(*t)-data=ch;

creat((*t)-lchild); //注意不同

creat((*t)-rchild);

}

return OK;

}

主函數這麼改

status main()

{

bitnode* t1; //多了個*

creat(t1);

pre(t1,print); //少了個

getch();

return 0;

}

另外一個編譯錯誤就是

int pre(bitnode *t,status (*visit)())

指針函數後面應該帶參數,改為

int pre(bitnode *t,status (*visit)(bitnode *))

C語言 樹的生成和遍歷

#include //頭文件

#include

typedef struct BiTNode

{

char data;

struct BiTNode *lchild,*rchild;

}

BiTNode,*BiTree;//定義結點類型

BiTree CreateBiTree()//創建樹

{

char p;BiTree T;

scanf(“%c”,p);

if(p==’ ‘)

T=NULL;

else

{

T=(BiTNode *)malloc(sizeof(BiTNode));//為結點開闢空間

T-data=p;

T-lchild=CreateBiTree();

T-rchild=CreateBiTree();

}

return (T);

}

void PreOrder(BiTree T)//先序

{

if(T!=NULL)

{

printf(“%c”,T-data);

PreOrder(T-lchild);

PreOrder(T-rchild);

}

}

void InOrder(BiTree T)//中序

{

if(T!=NULL)

{

InOrder(T-lchild);

printf(“%c”,T-data);

InOrder(T-rchild);

}

}

void PostOrder(BiTree T)//後序

{

if(T!=NULL)

{

PostOrder(T-lchild);

PostOrder(T-rchild);

printf(“%c”,T-data);

}

}

void main()//主函數

{

BiTree Ta;

Ta=CreateBiTree();

printf(“先序遍歷:”);

printf(“\n”);

PreOrder(Ta);

printf(“\n”);

printf(“中序遍歷:”);

printf(“\n”);

InOrder(Ta);

printf(“\n”);

printf(“後序遍歷:”);

printf(“\n”);

PostOrder(Ta);

}

給你個簡單的例子:

AB***

其中*代表空格

複雜點的例子

ABC**DE*f**g***

其中*代表空格

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/300769.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-29 12:52
下一篇 2024-12-29 12:52

相關推薦

  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python遍歷集合中的元素

    本文將從多個方面詳細闡述Python遍歷集合中的元素方法。 一、for循環遍歷集合 Python中,使用for循環可以遍歷集合中的每個元素,代碼如下: my_set = {1, 2…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • Python如何遍歷字典中的key和value

    本文將詳細講解Python中如何遍歷字典中的key和value,包括多種遍歷方式以及在遍歷過程中的一些應用場景。 一、遍歷字典中的key和value 在Python中,字典是一種無…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • 深度查詢宴會的文化起源

    深度查詢宴會,是指通過對一種文化或主題的深度挖掘和探究,為參與者提供一次全方位的、深度體驗式的文化品嘗和交流活動。本文將從多個方面探討深度查詢宴會的文化起源。 一、宴會文化的起源 …

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28

發表回復

登錄後才能評論