c語言深度優先二叉樹遍歷,深度優先遍歷類似於二叉樹的層次遍歷

本文目錄一覽:

急急急!求C語言的數據結構二叉樹遞歸遍歷程序!

/******************************************************/

/* 二叉樹的建立深度優先遍歷求葉子個數求深度 */

/******************************************************/

#include “stdio.h”

#include “string.h”

#include “stdlib.h”

#define NULL 0

typedef struct bitnode{

int 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));

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);

/*printf(“%c”,t-data); 中序*/

preorder(t-rchild);

/*printf(“%c”,t-data); 後序*/

}

}

/*求深度*/

int depth(bitree t){

int depthval,depl,depr;

if(!t)

depthval=0;

else{

depl=depth(t-lchild);

depr=depth(t-rchild);

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

}

return depthval;

}

/*求葉子數*/

int countleaf(bitree t){

int count=0;

if(!t)

count=0;

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

count++;

else

count=countleaf(t-lchild)+countleaf(t-rchild);

return count;

}

/*主函數*/

main(){

bitree t=NULL;

printf(“\nplease input a tree:”);

t=create(t);

preorder(t);

printf(“\ndepth:%d\nleave:%d\n”,depth(t),countleaf(t));

system(“pause”);

}

程序以調試通過!!!!!

C語言二叉樹的遍歷。

二叉樹的前中後遍歷(遞歸與非遞歸)

#includestdio.h

#includestdlib.h

typedef struct NODE

{

char value;

struct NODE *LChild;

struct NODE *RChild;

}BiTNode,*BiTree; //二叉樹數據結構

BiTree root;

typedef struct node

{

BiTNode *pointer;

struct node *link;

}LinkStackNode,*LinkStack; //鏈棧數據結構

LinkStack S;

int count = 0;

//BiTNode * InitTree(BiTree Tree);

BiTNode *CreateTree(BiTree Tree); //創建二叉樹

void PreOrder(BiTree Tree); //遞歸前序遍歷二叉樹

void MidOrder(BiTree Tree); //遞歸中序遍歷二叉樹

void PostOrder(BiTree Tree); //遞歸後序遍歷二叉樹

void NPreOrder(BiTree Tree); //非遞歸前序遍歷二叉樹

void NMidOrder(BiTree Tree); //非遞歸中序遍歷二叉樹

void NPostOrder(BiTree Tree); //非遞歸後序遍歷二叉樹

//—————————————————

LinkStackNode *InitLinkStack(LinkStack top); //初始化鏈棧

void Push(LinkStack top,BiTNode *p); //進棧操作

BiTNode * Pop(LinkStack top); //出棧操作

//int IsEmpty(LinkStack S); //判斷棧是否為空

void main()

{

//BiTree tree;

//root = InitTree(tree);

root = CreateTree(root);

PreOrder(root);

printf(“\n”);

MidOrder(root);

printf(“\n”);

PostOrder(root);

printf(“\n”);

NPreOrder(root);

printf(“\n”);

NMidOrder(root);

printf(“\n”);

NPostOrder(root);

printf(“\n”);

}

/*BiTNode * InitTree(BiTree Tree)

{

//BiTNode *root;

//root = Tree;

Tree = (BiTNode *)malloc(sizeof(BiTNode));

Tree = NULL;

//Tree-LChild = NULL;

//Tree-RChild = NULL;

return Tree;

}*/

//二叉樹的擴展先序遍歷的創建

BiTNode * CreateTree(BiTree Tree)

{

char ch;

ch = getchar();

if(ch == ‘.’)

Tree = NULL;

else

{

Tree = (BiTNode *)malloc(sizeof(BiTNode));

if(Tree)

{

Tree-value = ch;

Tree-LChild = CreateTree(Tree-LChild);

Tree-RChild = CreateTree(Tree-RChild);

}

}

return Tree;

}

//遞歸前序遍歷二叉樹

void PreOrder(BiTree Tree)

{

if(Tree)

{

printf(“%c”,Tree-value);

PreOrder(Tree-LChild);

PreOrder(Tree-RChild);

}

}

//遞歸中序遍歷二叉樹

void MidOrder(BiTree Tree)

{

if(Tree)

{

MidOrder(Tree-LChild);

printf(“%c”,Tree-value);

MidOrder(Tree-RChild);

}

}

//遞歸後序遍歷二叉樹

void PostOrder(BiTree Tree)

{

if(Tree)

{

PostOrder(Tree-LChild);

PostOrder(Tree-RChild);

printf(“%c”,Tree-value);

}

}

//非遞歸前序遍歷二叉樹

void NPreOrder(BiTree Tree)

{

BiTNode *p;

S = InitLinkStack(S);

p = Tree;

while(p || count != 0)

{

if(p)

{

if(p-RChild)

Push(S,p-RChild);

printf(“%c”,p-value);

p = p-LChild;

}

else

p = Pop(S);

}

}

//非遞歸中序遍歷二叉樹

void NMidOrder(BiTree Tree)

{

//char ch;

BiTNode *p;

S = InitLinkStack(S);

p = Tree;

while(p || count != 0)

{

if(p)

{

Push(S,p);

p = p-LChild;

}

else

{

p = Pop(S);

printf(“%c”,p-value);

p = p-RChild;

}

}

}

//非遞歸後序遍歷二叉樹

void NPostOrder(BiTree Tree)

{

BiTNode *p,*q = NULL;

S = InitLinkStack(S);

p = Tree;

while(p || count != 0)

{

if(p)

{

Push(S,p);

p = p-LChild;

}

else

{

p = S-link-pointer;

if(p-RChild == NULL || p-RChild == q)

{

p = Pop(S);

printf(“%c”,p-value);

q = p;

p = NULL;

}

else

{

//p = Pop(S);

p = p-RChild;

}

}

}

}

//初始化鏈棧

LinkStackNode *InitLinkStack(LinkStack top)

{

top = (LinkStackNode *)malloc(sizeof(LinkStackNode));

return top;

}

//進棧操作

void Push(LinkStack top,BiTNode *p)

{

LinkStackNode *temp;

temp = (LinkStackNode *)malloc(sizeof(LinkStackNode));

if(temp)

{

temp-pointer = p;

temp-link = top-link;

top-link = temp;

count++;

}

}

//出棧操作

BiTNode * Pop(LinkStack top)

{

//char ch;

BiTNode *p;

LinkStackNode *temp;

p = (BiTNode *)malloc(sizeof(BiTNode));

temp = top-link;

if(temp)

{

top-link = temp-link;

p = temp-pointer;

free(temp);

count–;

}

return p;

}

C語言數據結構「遍歷二叉樹」

[答案]:

//////////////////////////////////////////////////

使用方法:

輸入樹的節點,輸入0結束

1 2 3 4 5 6 7 8 9 0

中序列印

1-2-3-4-5-6-7-8-9-

後序列印

9-8-7-6-5-4-3-2-1-

前序列印

1-2-3-4-5-6-7-8-9-

程序原碼:

//////////////////////////////////////////////////

#includestdlib.h

#includestdio.h

typedef struct tree

{

struct tree *left;

int date;

struct tree *right;

}treenode,*b_tree;

///////按順序插入節點/////////////////////

b_tree insert(b_tree root,int node)

{

b_tree newnode;

b_tree currentnode;

b_tree parentnode;

newnode=(b_tree)malloc(sizeof(treenode));

newnode-date=node;

newnode-right=NULL;

newnode-left=NULL;

if(root==NULL)

return newnode;

else

{

currentnode=root;

while(currentnode!=NULL)

{

parentnode=currentnode;

if(currentnode-datenode)

currentnode=currentnode-left;

else

currentnode=currentnode-right;

}

if(parentnode-datenode)

parentnode-left=newnode;

else

parentnode-right=newnode;

}

return root;

}

//////建立樹///////////////////

b_tree creat(int *date,int len)

{

b_tree root=NULL;

int i;

for(i=0;ilen;i++)

root=insert(root,date[i]);

return root;

}

//////中序列印////////////////

void print1(b_tree root)

{if(root!=NULL)

{

print1(root-left);

printf(“%d-“,root-date);

print1(root-right);

}

}

//////後序列印////////////////

void print2(b_tree root)

{if(root!=NULL)

{

print2(root-left);

print2(root-right);

printf(“%d-“,root-date);

}

}

//////前序列印////////////////

void print3(b_tree root)

{if(root!=NULL)

{ printf(“%d-“,root-date);

print3(root-left);

print3(root-right);

}

}

///////測試函數//////////////////

void main()

{

b_tree root=NULL;

int i,index;

int value;

int nodelist[20];

printf(“輸入樹的節點,輸入0結束\n”);

index=0;

scanf(“%d”,value);

while(value!=0)

{

nodelist[index]=value;

index=index+1;

scanf(“%d”,value);

}

root=creat(nodelist,index);

printf(“\n中序列印\n”);

print1(root);

printf(“\n後序列印\n”);

print2(root);

printf(“\n前序列印\n”);

print3(root);

}

悉雨辰寂

二叉樹的創建和遍歷

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

#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);

}

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

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

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

相關推薦

  • Python遍歷集合中的元素

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

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

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

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

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

    編程 2025-04-29
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • Python下載深度解析

    Python作為一種強大的編程語言,在各種應用場景中都得到了廣泛的應用。Python的安裝和下載是使用Python的第一步,對這個過程的深入了解和掌握能夠為使用Python提供更加…

    編程 2025-04-28
  • 二叉樹非遞歸先序遍歷c語言

    本文將為您詳細介紹二叉樹的非遞歸先序遍歷演算法,同時提供完整的C語言代碼示例。通過本文,您將了解到二叉樹的先序遍歷演算法,以及非遞歸實現的方式。 一、二叉樹的先序遍歷演算法介紹 在介紹二…

    編程 2025-04-28
  • Python如何遍歷列表

    在Python編程中,列表是一種常用的數據類型,它允許我們存儲多個值。但是,我們如何遍歷列表並對其中的每個值進行操作呢? 一、for循環遍歷列表 fruits = [‘apple’…

    編程 2025-04-28
  • Python遍歷字典刪除元素

    本文主要介紹Python中如何遍歷字典並刪除元素。在實際應用中,遍歷字典並刪除元素是一種非常常見的操作,但需要注意的是,直接在字典中刪除元素可能會改變字典中其他元素的索引順序,因此…

    編程 2025-04-28
  • Python列表構建二叉樹

    本文將從以下幾個方面詳細闡述如何使用Python列表構建二叉樹: 一、二叉樹的基本概念 二叉樹是一種重要的數據結構,其每個節點最多有兩個子節點,左子節點和右子節點。左子節點始終比右…

    編程 2025-04-27
  • Python遞歸深度用法介紹

    Python中的遞歸函數是一個函數調用自身的過程。在進行遞歸調用時,程序需要為每個函數調用開闢一定的內存空間,這就是遞歸深度的概念。本文將從多個方面對Python遞歸深度進行詳細闡…

    編程 2025-04-27

發表回復

登錄後才能評論