数据结构与算法实验部分课后题程序答案

题目
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

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