樹的簡單操作集合——基於C實現

來源:互聯網
上載者:User

標籤:樹的建立   樹的遍曆   

很多資料結構的書上講解資料機構時都是採用虛擬碼實現,其實感覺蠻不直觀的,所以對於所有的資料結構操作我都將其用C實現一遍。
樹是學習二叉樹的基礎,也是後面理解B樹,B+樹的等樹的基礎,下面就給出樹的幾個簡單操作,方便理解。

資料結構
//-------資料結構----------------------------------------#define m 3  //定義度為3的樹typedef char datatype;typedef struct node{    datatype data;    struct node* child[m];}treenode;//------------------------------------------------------
基本操作
//--------操作------------------------------------------void preorder(treenode* t){    int i;    if (t)    {        printf("%c",t->data);        for(i=0;i<m;i++)        {            preorder(t->child[i]);        }    }}void postorer(treenode* t){    int i;    if (t)    {        for(i =0;i<m;i++)        {            postorer(t->child[i]);        }        printf("%c",t->data);    }}//必須用指標的指標,如果實參是一個指標,則形參只是一個實參指標的拷貝,所以實參指標的值是不會改變的。void createtree(treenode** p) {    //輸入的時候直接輸AB###C###D###斷行符號    int i;char ch;    scanf("%c",&ch);    if (ch ==‘#‘)    {        *p = NULL;    }    else    {        *p = (treenode*)malloc(sizeof(node));        (*p)->data = ch;        for (i =0;i<m;i++)        {            createtree(&((*p)->child[i]));        }    }}//------------------------------------------------------

對於樹的操作不需要做太多,大多數都是針對二叉樹的操作,所以就給出了簡單的幾個操作,足矣。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

樹的簡單操作集合——基於C實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.