日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

树与二叉树的深度优先与广度优先算法(递归与非递归)

發布時間:2025/3/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 树与二叉树的深度优先与广度优先算法(递归与非递归) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本博客前面文章已對樹與二叉樹有過簡單的介紹,本文主要是重點介紹有關二叉樹的一些具體操作與應用

閱讀本文前,可以先參考本博客 各種基本算法實現小結(三)—— 樹與二叉樹 ? 和? 各種基本算法實現小結(二)—— 堆 棧

?

二叉樹

深度層數、葉子數、節點數和廣度優先算法

以及樹的先序、中序、后序的遞歸與非遞歸(深度優先)


測試環境:VS2008(C)

#include "stdafx.h" #include <stdlib.h> #include <malloc.h> #define DataType char int d_tree=0; /* tree's depth */ int l_tree=0; /* tree's leaf */ int n_tree=0; /* tree's node */ /**************************************/ /******** 樹的結構定義 ********/ /**************************************/ struct _tree { DataType data; struct _tree *lchild; struct _tree *rchild; }; typedef struct _tree tree, *ptree; /**************************************/ /******** 棧的結構定義 ********/ /**************************************/ struct _node { ptree pt; struct _node *next; }; typedef struct _node node, *pnode; struct _stack { int size; pnode ptop; }; typedef struct _stack stack, *pstack; /**************************************/ /******** 堆的結構定義 ********/ /**************************************/ struct _queue { pnode front; pnode rear; }; typedef struct _queue queue, *pqueue; /**************************************/ /******** 棧的數據操作 ********/ /**************************************/ pstack init_stack() { pnode pn=NULL; pstack ps=NULL; pn=(pnode)malloc(sizeof(node)); ps=(pstack)malloc(sizeof(stack)); pn->pt=NULL; pn->next=NULL; ps->ptop=pn; return ps; } int empty_stack(pstack ps) { if(ps->ptop->next==NULL) return 1; else return 0; } void push_stack(pstack ps, ptree pt) /* flag for post tree: 0 for lchild; 1 for rchild */ { pnode pn=NULL; pn=(pnode)malloc(sizeof(node)); pn->pt=pt; pn->next=ps->ptop; ps->ptop=pn; } ptree pop_stack(pstack ps) { ptree pt=NULL; pnode pn=NULL; if(!empty_stack(ps)) { pn=ps->ptop; ps->ptop=ps->ptop->next; pt=pn->pt; free(pn); } return pt; } ptree gettop_stack(pstack ps) { if(!empty_stack(ps)) return ps->ptop->pt; } /**************************************/ /******** 堆的數據操作 ********/ /**************************************/ queue init_queue() { pnode pn=NULL; queue qu; pn=(pnode)malloc(sizeof(node)); pn->pt=NULL; pn->next=NULL; qu.front=qu.rear=pn; /* init: pn is header */ return qu; } int empty_queue(queue &qu) { if(qu.front==qu.rear) return 1; else return 0; } void en_queue(queue &qu, ptree pt) { pnode pn=NULL; pn=(pnode)malloc(sizeof(node)); pn->pt=pt; pn->next=qu.rear->next; qu.rear->next=pn; qu.rear=qu.rear->next; } ptree de_queue(queue &qu) { ptree pt=NULL; pnode pn=NULL; if(!empty_queue(qu)) { pn=qu.front->next; if(pn==qu.rear) /* qu is null: qu.front==qu.rear */ qu.rear=qu.front; else qu.front->next=qu.front->next->next; pt=pn->pt; free(pn); } return pt; } /**************************************/ /******** 樹的數據操作 ********/ /**************************************/ ptree init_tree() { ptree pt=NULL; pt=(ptree)malloc(sizeof(tree)); pt->data='0'; pt->lchild=NULL; pt->rchild=NULL; return pt; } ptree create_tree() { char ch; ptree pt=NULL; scanf("%c", &ch); getchar(); if(ch==' ') return NULL; else { pt=(ptree)malloc(sizeof(tree)); pt->data=ch; pt->lchild=create_tree(); pt->rchild=create_tree(); } return pt; } int depth_tree(ptree pt) { int l_len, r_len; ptree p=pt; if(p==NULL) return 0; else { l_len=depth_tree(p->lchild); r_len=depth_tree(p->rchild); return l_len>r_len?(l_len+1):(r_len+1); } } void leaf_tree(ptree pt) { ptree p=pt; if(p->lchild==NULL && p->rchild==NULL) l_tree++; else { leaf_tree(p->lchild); leaf_tree(p->rchild); } } void node_tree(ptree pt) { ptree p=pt; if(p!=NULL) { n_tree++; node_tree(p->lchild); node_tree(p->rchild); } } void print_pretree(ptree pt) { if(pt!=NULL) { printf("%3c", pt->data); print_pretree(pt->lchild); print_pretree(pt->rchild); } } void print_pretree2(ptree pt) { pstack ps=NULL; ptree p=NULL; ps=init_stack(); p=pt; while(p!=NULL || !empty_stack(ps)) { while(p!=NULL) { printf("%3c", p->data); push_stack(ps, p); p=p->lchild; } if(!empty_stack(ps)) { p=pop_stack(ps); p=p->rchild; } } } void print_midtree(ptree pt) { if(pt!=NULL) { print_midtree(pt->lchild); printf("%3c", pt->data); print_midtree(pt->rchild); } } void print_midtree2(ptree pt) { pstack ps=NULL; ptree p=NULL; ps=init_stack(); p=pt; while (p!=NULL || !empty_stack(ps)) { while(p!=NULL) { push_stack(ps, p); p=p->lchild; } if(!empty_stack(ps)) { p=pop_stack(ps); printf("%3c", p->data); p=p->rchild; } } } void print_posttree(ptree pt) { if(pt!=NULL) { print_posttree(pt->lchild); print_posttree(pt->rchild); printf("%3c", pt->data); } } void print_posttree2(ptree pt) { pstack ps=NULL; ptree p=NULL; ptree p2=NULL; ptree lastvisit=NULL; ps=init_stack(); p=pt; while (p!=NULL || !empty_stack(ps)) { while(p!=NULL) { push_stack(ps, p); p=p->lchild; } p2=gettop_stack(ps); /* top: rchild==null or sub_root */ if(p2->rchild==NULL || p2->rchild==lastvisit) { printf("%3c", p2->data); lastvisit=pop_stack(ps); /* pop */ } else p=p2->rchild; } } void bfs_tree(ptree pt) { ptree p=NULL; queue qu; qu=init_queue(); p=pt; if(p!=NULL) { en_queue(qu, p); while (!empty_queue(qu)) { p=de_queue(qu); printf("%3c", p->data); if(p->lchild!=NULL) en_queue(qu, p->lchild); if(p->rchild!=NULL) en_queue(qu, p->rchild); } } } int _tmain(int argc, _TCHAR* argv[]) { ptree pt=NULL; /*pt=init_tree();*/ printf("Create tree:/n"); pt=create_tree(); /************ recursion ************/ printf("/n/nrecursion..."); printf("/npre tree.../n"); print_pretree(pt); printf("/nmid tree.../n"); print_midtree(pt); printf("/npost tree.../n"); print_posttree(pt); /************ stack ************/ printf("/n/nstack, non recursion:"); printf("/npre tree.../n"); print_pretree2(pt); printf("/nmid tree.../n"); print_midtree2(pt); printf("/npost tree.../n"); print_posttree2(pt); /********** bfs tree **********/ printf("/n/nbfs_tree:/n"); bfs_tree(pt); /********* tree operation ********/ printf("/n/ntree operation:"); d_tree=depth_tree(pt); leaf_tree(pt); node_tree(pt); printf("/ntree's depth: %d", d_tree); printf("/ntree's leaf: %d", l_tree); printf("/ntree's node: %d", n_tree); printf("/n"); return 0; }

運行結果:

???

======================================

???

===================================================================

轉載于:https://www.cnblogs.com/springside4/archive/2010/06/22/2481808.html

總結

以上是生活随笔為你收集整理的树与二叉树的深度优先与广度优先算法(递归与非递归)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。