數據結構與算法實驗部分課後題程序答案

題目
5.27 設- -棵二叉樹以二叉鏈表表示, 試以成員函數形式編寫有關二叉樹的遞歸算法:
(1)統計二叉樹中度爲1的結點個數。
(2)統計二叉樹中度爲2的結點個數。
(3)統計二叉樹中度爲0(葉結點)的結點個數。
(4)統計二叉樹的深度。.
(5)統計二叉樹的寬度,即在二叉樹的各層上,具有結點數最多的那一層上結點總數。
(6)從二叉樹中刪去所有葉結點。
(7)計算二叉樹中指定結點"p所在層次。
(8)計算二叉樹中各結點中的最大元素的值.
(9)以前序次序輸出一棵二叉樹所有結點的數據值及結點所在層次。

5.29 試編寫一個算法,將用二叉鏈表表示的完全二叉樹轉換爲二叉樹的順序(數組)
表示。

5. 34二叉樹的雙序 遍歷(Double order traversal)是指: 對於二叉樹的每一個結點來
說,先訪同這個結點,再按雙序遍歷它的左子樹,然後再-次訪問這個結點,接下來按雙序遍
歷它的右子樹。試寫出執行這種雙序遍歷的算法。

5.37 可以用縮格(或移行)的文本形式( Indented Text)來表示一棵樹的結點數據。
例如,下面圖5. 62(a)所示的樹的縮格文本形式如圖5.62(b)所示。試設計一個算法,將用
左子女右兄弟鏈表表示的樹用縮格文本形式輸出
在這裏插入圖片描述

#include <iostream>
using namespace std;

typedef char type;

struct node
{
    type x;
    node *lson,*rson;
};

void creat(node *&root)
{
    type x;
    cin>>x;
    if(x=='@') root=NULL;
    else
    {
        root=new node;
        root->x=x;
        creat(root->lson);
        creat(root->rson);
    }
}

///5.29 完全二叉樹存入數組
///測試用例:abc@@d@@e@@ (完全前序先建樹)
///輸出:a[1]=a a[2]=b a[3]=e a[4]=c a[5]=d
type a[1<<10];
int cnt=0;///總節點數
void change(node *root,int index=1)
{
    if(root==NULL) return;
    cnt=max(cnt,index);
    a[index]=root->x;
    change(root->lson,index*2);
    change(root->rson,index*2+1);
}

///5.34 雙序遍歷
///測試用例:abc@@d@@e@@ (完全前序先建樹)
///輸出:a b c c b d d a e e
void doubleprint(node *root)
{
    if(root==NULL) return;
    cout<<root->x<<' ';
    doubleprint(root->lson);
    cout<<root->x<<' ';
    doubleprint(root->rson);
}

///5.37 縮格文本形式輸出
///測試用例:abc@@d@@e@@ (完全前序先建樹)
///輸出:
/*
a
  b
    c
    d
  e
*/
void Sprint(node *root,int layer=0)
{
    if(root==NULL) return;
    for(int i=0;i<layer*2;i++) cout<<' ';
    cout<<root->x<<endl;
    Sprint(root->lson,layer+1);
    Sprint(root->rson,layer+1);
}


///5.27.1/2/3
///測試用例:abc@@d@@e@@ (完全前序先建樹)
///輸出:
///度數爲0的節點個數爲 3
///度數爲1的節點個數爲 0
///度數爲2的節點個數爲 2
int tot=0;
void Calculate_nodes(node *root,int num)///計算度數爲num的節點個數
{
    if(root==NULL) return;
    int cnt=0;
    Calculate_nodes(root->lson,num);
    Calculate_nodes(root->rson,num);
    if(root->lson) cnt++;
    if(root->rson) cnt++;
    if(cnt==num) tot++;
}
///5.27.4
///測試用例:abc@@d@@e@@ (完全前序先建樹)
///輸出: 樹的深度爲3
int layer=0;
void getheight(node *root,int l=1)///計算樹的深度
{
    if(root==NULL) return;
    getheight(root->lson,l+1);
    getheight(root->rson,l+1);
    if(!root->lson&&!root->rson) layer=max(layer,l);
}

///5.27.5
///測試用例:abc@@d@@ef@@@ (完全前序先建樹)
///輸出: 樹的寬度爲3
int d[100],weight=0;
void getweight(node *root,int l=1)///計算樹的深度
{
    if(root==NULL) return;
    getweight(root->lson,l+1);
    getweight(root->rson,l+1);
    d[l]++;///每層的節點數
    weight=max(weight,d[l]);
}

///5.27.6
///測試用例:abc@@d@@ef@@@ (完全前序先建樹)
///輸出: a b e (先序序列)
void deleteleaves(node *root)///刪除葉子節點
{
    if(root==NULL) return;
    if(root->lson)
    {
        node* temp=root->lson;
        if(!temp->lson&&!temp->rson)
        {
            delete temp;
            root->lson=NULL;
        }
    }
    if(root->rson)
    {
        node* temp=root->rson;
        if(!temp->lson&&!temp->rson)
        {
            delete temp;
            root->rson=NULL;
        }
    }
    deleteleaves(root->lson);
    deleteleaves(root->rson);
}
void first(node *root)
{
    if(root==NULL) return;
    cout<<root->x<<' ';
    first(root->lson);
    first(root->rson);
}
///5.27.7
///測試用例:abc@@d@@ef@@@ (完全前序先建樹) 查找地址root->rson->lson ,查找的值e
///輸出: 按地址查找的節點在第3層   按值查找的節點在第2層
int ans=-1;///-1爲查找不到
void find_point1(node *root,node *target,int layer=1)
{
    if(root==NULL) return;
    find_point1(root->lson,target,layer+1);
    find_point1(root->rson,target,layer+1);
    if(root==target)
    {
        ans=layer;
        return;
    }
}
void find_point2(node *root,type target,int layer=1)
{
    if(root==NULL) return;
    find_point2(root->lson,target,layer+1);
    find_point2(root->rson,target,layer+1);
    if(root->x==target)
    {
        ans=layer;
        return;
    }
}

///5.27.8
///測試用例:abc@@d@@ef@@@ (完全前序先建樹)
///輸出:  f
type maxn='\0';
void find_max(node *root)
{
    if(root==NULL) return;
    find_max(root->lson);
    find_max(root->rson);
    maxn=max(maxn,root->x);
}
///5.27.9
///測試用例:abc@@d@@ef@@@ (完全前序先建樹)
///輸出:
///節點值爲a 層次爲1
///節點值爲b 層次爲2
///節點值爲c 層次爲3
///節點值爲d 層次爲3
///節點值爲e 層次爲2
///節點值爲f 層次爲3
void Print_layer(node *root,int layer=1)
{
    if(root==NULL) return;
    cout<<"節點值爲"<<root->x<<" 層次爲"<<layer<<endl;
    Print_layer(root->lson,layer+1);
    Print_layer(root->rson,layer+1);
}

int main()
{
    node *root;
    creat(root);
    ///5.27.1/2/3
    /*for(int i=0;i<=2;i++)
    {
        tot=0;
        Calculate_nodes(root,i);
        cout<<"度數爲"<<i<<"的節點個數爲 "<<tot<<endl;
    }*/
    ///5.27.4
    //getheight(root);
    //cout<<"樹的深度爲"<<layer<<endl;
    ///5.27.5
    //getweight(root);
    //cout<<"樹的寬度爲"<<weight<<endl;
    ///5.27.6
    //deleteleaves(root);
    //first(root);
    ///5.27.7
    //find_point1(root,root->rson->lson);
    //cout<<"按地址查找的節點在第"<<ans<<"層"<<endl;
    //ans=-1;
    //find_point2(root,'e');
    //cout<<"按值查找的節點在第"<<ans<<"層"<<endl;
    ///5.27.8
    //find_max(root);
    //cout<<maxn<<endl;
    ///5.27.9
    //Print_layer(root);
    ///5.29
    //change(root);
    //for(int i=1;i<=cnt;i++)cout<<"a["<<i<<"]="<<a[i]<<' ';cout<<endl;
    ///5.34
    //doubleprint(root);
    ///5.37
    //Sprint(root);
    return 0;
}

da

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章