美文网首页简友广场想法散文
数据结构第七周作业(二叉树、霍夫曼树、度、层次)

数据结构第七周作业(二叉树、霍夫曼树、度、层次)

作者: Cache_wood | 来源:发表于2021-04-22 17:14 被阅读0次
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.

(1)
(2)

叶子节点:N,K,M,H,I,J,E,F,P,C
分支节点:L,G,D,A,B

(3)

各节点的度:
A:3, B:3, C:0, D:4, E:0, F:0, P:0, G:3, \\H:0, I:0, J:0, L:1, K:0, M:0, N:0\\
树的度:4

(4)

各节点的层次
A:1, B:1, D:1, C:1, G:2, H:2, I:2, J:2, \\E:2, F:2, P:2, L:3, K:3, M:3, N:4

树的高度:5

(5)

4^k-1 = 4^5-1 = 1023

2.

(A,(D,(G,(L,(N)),K,M),H,I,J),(B,(E,F,P)),C)

3.

#include <stdio.h>
#include <stdlib.h>

struct Treenode{   
    int Data;
    struct Treenode *Lchild, *Rchild;
};
void Setuptree(struct Treenode **T, int b, struct Treenode *p, int tag){    
    printf("%d ",b);
    struct Treenode *x;
    x=(struct Treenode *) malloc(sizeof(struct Treenode));
    x->Data = b; x->Lchild = 0;  x->Rchild = 0;
    if ((*T)==0) (*T)=x;
    else if(p != 0){  
        if (tag==0) { x->Lchild = p->Lchild;  p->Lchild = x; } 
        else {x->Rchild = p->Rchild; p->Rchild = x;}
    }else if(tag==0){  
        p = (*T);
        while (p->Lchild !=0) p=p->Lchild;
        p->Lchild = x;
    }else{ 
        p = (*T); 
        while (p->Rchild !=0) p=p->Rchild;
        p->Rchild = x;
    }
}
/*递归方法
//统计叶子节点个数
int Leaf(struct Treenode *T){
   int count;
   if(T==NULL){
      return 0;
   }
   if(T->Lchild==NULL && T->Rchild==NULL){
      count++;
   }
   LeafCount(T->Lchild);
   LeafCount(T->Rchild);
   return count;
}
//统计度数为1的节点个数
int Degree1(struct Treenode *T){
   int count;
   if(T==NULL){
      return 0;
   }
   if((T->Lchild==NULL && T->Rchild!=NULL)||(T->Lchild!=NULL && T->Rchild==NULL)){
      count++;
   }
   Nodenumber1(T->Lchild);
   Nodenumber1(T->Rchild);
   return count;
}
//统计度数为2节点个数
int Degree2(struct Treenode *T){
   int count;
   if(T==NULL){
      return 0;
   }
   if(T->Lchild!=NULL && T->Rchild!=NULL){
      count++;
   }
   Nodenumber2(T->Lchild);
   Nodenumber2(T->Rchild);
   return count;
}*/
//非递归方法
int Leaf(struct Treenode *T){
    int count=0;
    if(T!=NULL)
    {
        Leaf(T->Lchild);
        Leaf(T->Rchild);
        if(T->Lchild==NULL && T->Rchild==NULL)
        {
            count++;   //统计叶子结点数目
        }
    }
    return count;
} 
int Degree1(struct Treenode *T){
    int count=0;
    if(T!=NULL){
        Degree1(T->Lchild);
        Degree1(T->Rchild);
        if((T->Lchild==NULL && T->Rchild!=NULL)||(T->Lchild!=NULL && T->Rchild==NULL)){
            count++;   //统计度为1的结点数目
        }
    }
    return count;
} 
int Degree2(struct Treenode *T){
    int count=0;
    if(T!=NULL){
        Degree2(T->Lchild);
        Degree2(T->Rchild);
        if(T->Lchild!=NULL && T->Rchild!=NULL){
            count++;   //统计度为2的结点数目
        }
    }
    return count;
} 
int main(){
    struct Treenode *T=0,*p, *(Q[20]);
    int A[]={45, 23, 76, 32, 66, 78, 52, 18, 90, 75};  
    int i=0;
    int n=sizeof(A)/sizeof(A[0]);
    Setuptree(&T, A[i++], p, 0);    // 二叉树根结点  //
    while (i<n){
        int Front=0; int Rear=0; Q[++Rear] = T;
        while (Front<Rear){ 
            p = Q[++Front];
            if (p->Lchild!=0) Q[++Rear] = p->Lchild;        // 左子树 //
            else {if (i<n) Setuptree(&T, A[i++], p, 0); }
            if (p->Rchild!=0) Q[++Rear] = p->Rchild;       // 右子树 //
            else {if (i<n) Setuptree(&T, A[i++], p, 1); }
        }
    }
    printf("度为0的结点数:%d\n",Leaf(T));
    printf("度为1的结点数:%d\n",Degree1(T));
    printf("度为2的结点数:%d\n",Degree2(T));

    return 0;
}

4.

设叶子节点为n_0,则有总结点数 = 总度数+1

n_0+n_1+……n_m = 0·n_0+1·n_1+……+m·n_m+1

叶子节点n_0 = 0·n_1+1·n_2+……+(m-1)n_m+1

分支节点n_1+n_2+……+n_m

5.

6.

7.

#include <stdio.h>
#include <stdlib.h>

struct Treenode{   
    int Data;
    struct Treenode *Lchild, *Rchild;
};
void BuildTree(struct Treenode **T,int x){
    struct Treenode *r, *p, *q;
    r = (struct Treenode *)malloc(sizeof(struct Treenode));
    r->Data = x; r->Lchild = 0; r->Rchild = 0; // 申请新结点 
    if ((*T) == 0) (*T) = r;    // 新结点作为某子树的根结点 //
    else{
        p= *T;
        while (p != 0){
            q = p;
            if (x < p->Data) p=p->Lchild;  else p=p->Rchild;
        }  // 查找位置 //
        if (x < q->Data) q->Lchild=r; else q->Rchild=r;   // 添加结点 //
    }  
}
//建分类树
void SortTree(struct Treenode **T, int A[], int n){     
    int i;  *T = 0;
    for (i=0; i<n; i++) BuildTree(&(*T), A[i]);
}
//搜索
struct Treenode *Nodesearch(struct Treenode *T, int key){
    struct Treenode *p=T;
    while(p!=0){
        if (p->Data==key) 
            return p;
        else if (p->Data>key)
            p=p->Lchild; 
        else 
            p=p->Rchild;
    }
    return 0;
}
int main(){
    struct Treenode *T=0,*p;
    int A[]={45, 23, 76, 32, 66, 78, 52, 18, 90, 75};  
    int n = sizeof(A)/sizeof(A[0]);
    int key;
    printf("enter the key:");
    scanf("%d",&key);

    SortTree(&T,A,n);
    printf("%d",Nodesearch(T,key));
    return 0;
}
在这里插入图片描述

相关文章

网友评论

    本文标题:数据结构第七周作业(二叉树、霍夫曼树、度、层次)

    本文链接:https://www.haomeiwen.com/subject/zwdfrltx.html