19版考研数据结构王道课后习题代码-树 下【未完】

本文详细解析了2021年考研数据结构教材中关于树部分的王道课后习题,涵盖二叉树、线索二叉树、树的遍历等重要概念及其实现代码。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stack>
using namespace std;
#define maxSize 101
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stack>
using namespace std;
#define maxSize 101


/*
 //后序遍历二叉树的非递归算法  P123.3
typedef struct Tree
{
    int data;
    Tree *lchild,*rchild;
};
void PostTraval(Tree *t)
{
    Tree *stack[maxSize];
    int top=-1;
    Tree *r=NULL;
    while(top!=-1||t)
    {
        while(t)
        {
            stack[++top]=t;
            t=t->lchild;
        }
        t=stack[top];
        if(t->rchild&&t->rchild!=r)
        {
            t=t->rchild;
        }
        else
        {
            t=stack[top--];
            cout<<t->data<<" ";
            r=t;
            t=NULL;
        }
    }
}
Tree *Create(Tree *&t,int ch)
{
    if(t==NULL)
    {
        t=(Tree*)malloc(sizeof(Tree));
        t->data=ch;
        t->lchild=NULL;
        t->rchild=NULL;
    }
    else if(ch<=t->data)
        t->lchild=Create(t->lchild,ch);
    else if(ch>t->data)
        t->rchild=Create(t->rchild,ch);
    return t;
}
int main()
{
    Tree *t=NULL;
    int n,a[maxSize];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        t=Create(t,a[i]);
    }
    PostTraval(t);
}
 
5
3 4 2 1 3
1 3 2 4 3
*/

/*
 //给出自下而上从右到左的层次遍历算法 P123 4  先把从上到下从左到右的层次遍历结果存到栈中,再从栈中输出
 typedef struct Tree{
     int data;
     Tree *lchild,*rchild;
 }Tree;
int s[maxSize];
int top=-1;
void LayerTraval(Tree *t)
{
    Tree *queue[maxSize];
    int front=1;
    int rear=1;
    queue[rear++]=t;
    while(front!=rear)
    {
        Tree *q=queue[front];
        s[++top]=q->data;
        front++;
        if(q->lchild)
        {
            queue[rear++]=q->lchild;
        }
        if(q->rchild)
        {
            queue[rear++]=q->rchild;
        }
    }
}
Tree *insert(Tree *&t,int ch)
{
    if(t==NULL)
    {
        t=(Tree*)malloc(sizeof(Tree));
        t->data=ch;
        t->lchild=NULL;
        t->rchild=NULL;
    }
    if(ch<t->data)
    {
        t->lchild=insert(t->lchild,ch);
    }
    else if(ch>t->data)
    {
        t->rchild=insert(t->rchild,ch);
    }
    return t;
}
int main()
{
    Tree *t=NULL;
    int n,a[maxSize];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        t=insert(t,a[i]);
    }
    LayerTraval(t);
    while(top!=-1)
    {
        cout<<s[top--]<<" ";
    }
}

 //6
 //5 3 6 1 4 8
 //8 4 1 6 3 5
 */


/*
 //非递归法求二叉树的高度 P123 5
typedef struct Tree{
    int data;
    Tree *lchild,*rchild;
}Tree;
Tree *insert(Tree *&t,int ch)
{
    if(t==NULL)
    {
        t=(Tree*)malloc(sizeof(Tree));
        t->data=ch;
        t->lchild=t->rchild=NULL;
    }
    else if(ch<t->data)
    {
        insert(t->lchild,ch);
    }
    else if(ch>t->data)
    {
        insert(t->rchild,ch);
    }
    return t;
}
void DFS(Tree *t,int &max)
{
    Tree *stack[maxSize];
    int top=-1;
    int h=0;
    while(t||top!=-1)
    {
        while(t)
        {
            stack[++top]=t;
            t=t->lchild;
            h++;
            if(h>max)
                max=h;
        }
        if(top!=-1)
        {
            t=stack[top--];
            h--;
            t=t->rchild;
        }
    }
}
int main()
{
    Tree *t=NULL;
    int n;
    int a[maxSize];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        t=insert(t,a[i]);
    }
    int max=0;
    DFS(t,max);
    cout<<max<<endl;
}
//
// 6
// 5 3 6 2 1 8
// 4
//
*/


/*
 //由先序遍历和中序遍历建树 P123 6
typedef struct Tree{
    int data;
    Tree *lchild,*rchild;
}Tree;
Tree *CreatTree(int pre[],int in[],int preL,int preR,int inL,int inR)
{
    if(preL<=preR)
    {
        Tree *p=(Tree*)malloc(sizeof(Tree));
        p->lchild=NULL;
        p->rchild=NULL;
        p->data=pre[preL];
        int k;
        for(int i=inL;i<=inR;i++)
        {
            if(in[i]==pre[preL])
            {
                k=i;
                break;
            }
        }
        p->lchild=CreatTree(pre,in,preL+1,preL+(k-inL),inL,k-1);
        p->rchild=CreatTree(pre,in,preL+(k-inL)+1,preR,k+1,inR);
        return p;
    }
    return NULL;
}
void postTraval(Tree *t)
{
    if(t)
    {
        postTraval(t->lchild);
        postTraval(t->rchild);
        cout<<t->data<<" ";
    }
}
int main()
{
    int pre[maxSize],in[maxSize];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>pre[i];
    }
    for(int i=0;i<n;i++)
    {
        cin>>in[i];
    }
    Tree *t=NULL;
    t=CreatTree(pre,in,0,n-1,0,n-1);
    postTraval(t);
}

//6
//5 3 2 1 6 8
//1 2 3 5 6 8
//1 2 3 8 6 5
*/


/*
 //链表形式,判断二叉树是否是完全二叉树 P123 7  //检查每一层的节点数
typedef struct Tree{
    int data,lev;
    Tree *lchild,*rchild;
}Tree;
Tree *insert(Tree *&t,int ch)
{
    if(t==NULL)
    {
        t=(Tree*)malloc(sizeof(Tree));
        t->data=ch;
        t->lchild=t->rchild=NULL;
    }
    else if(ch<t->data)
    {
        insert(t->lchild,ch);
    }
    else if(ch>t->data)
    {
        insert(t->rchild,ch);
    }
    return t;
}
int BFS (Tree *t)
{
    Tree *queue[maxSize];
    int front=0;
    int rear=0;
    int l;
    queue[rear]=t;
    queue[rear]->lev=1;
    rear++;
    while(front!=rear)
    {
        Tree *p=queue[front];
        l=queue[front]->lev;
        front++;
        if(p->lchild)
        {
            queue[rear]=p->lchild;
            queue[rear]->lev=l+1;
            rear++;
        }
        if(p->rchild)
        {
            queue[rear]=p->rchild;
            queue[rear]->lev=l+1;
            rear++;
        }
    }
    for(int i=1;i<l;i++)
    {
        int cnt=0;
        for(int j=0;j<rear;j++)
        {
            if(queue[j]->lev==i)
            {
                cnt++;
            }
        }
        if(cnt!=pow(2,i-1))
            return 0;
    }
    return 1;
}
int main()
{
    Tree *t=NULL;
    int n;
    int a[maxSize];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        t=insert(t,a[i]);
    }
    int d=BFS(t);
    cout<<d<<endl;
}
*/


/*

 //计算链表中所有双分支节点的个数 P123 8
typedef struct Tree
{
    int data;
    struct Tree *lchild,*rchild;
}Tree;
void PreTraval(Tree *t,int &n)
{
    if(t)
    {
        if(t->lchild&&t->rchild)
        {
            n++;
        }
        PreTraval(t->lchild,n);
        PreTraval(t->rchild,n);
    }
}
void *insert(Tree *&t,int x)
{
    if(t==NULL)
    {
        t=(Tree*)malloc(sizeof(Tree));
        t->data=x;
        t->lchild=NULL;
        t->rchild=NULL;
    }
    else if(x<t->data)
    {
        insert(t->lchild,x);
    }
    else if(x>=t->data)
    {
        insert(t->rchild,x);
    }
    return NULL;
}
int main()
{
    Tree *t;
    int n,c;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>c;
        insert(t,c);
    }
    int k=0;
    PreTraval(t,k);
    cout<<k<<endl;
}
 
// 7
// 5 3 1 4 9 7 12
// 3
*/


/*
 //交换左右子树 P123 9
typedef struct Tree
{
    int data;
    struct Tree *lchild,*rchild;
}Tree;
Tree *PreTraval(Tree  *t)
{
    if(t)
    {
        t->lchild=PreTraval(t->lchild);
        t->rchild=PreTraval(t->rchild);
        Tree *u;
        u=t->lchild;
        t->lchild=t->rchild;
        t->rchild=u;
        return t;
    }
    return NULL;
}
void *insert(Tree *&t,int x)
{
    if(t==NULL)
    {
        t=(Tree*)malloc(sizeof(Tree));
        t->data=x;
        t->lchild=NULL;
        t->rchild=NULL;
    }
    else if(x<t->data)
    {
        insert(t->lchild,x);
    }
    else if(x>=t->data)
    {
        insert(t->rchild,x);
    }
    return NULL;
}
void postTraval(Tree *t)
{
    if(t)
    {
        postTraval(t->lchild);
        postTraval(t->rchild);
        cout<<t->data<<" ";
    }
}
int main()
{
    Tree *t=NULL;
    int n;
    cin>>n;
    int c;
    for(int i=0;i<n;i++)
    {
        cin>>c;
        insert(t,c);
    }
    t=PreTraval(t);
    postTraval(t);
}
 
// 6
// 5 3 1 4 9 7
// 7 9 4 1 3 5
//
 */



 //求先序遍历序列中第k个节点的值 P123 10
//typedef struct Tree
//{
//    int data;
//    struct Tree *lchild,*rchild;
//}Tree;
//void *insert(Tree *&t,int x)
//{
//    if(t==NULL)
//    {
//        t=(Tree*)malloc(sizeof(Tree));
//        t->data=x;
//        t->lchild=NULL;
//        t->rchild=NULL;
//    }
//    else if(x<t->data)
//    {
//        insert(t->lchild,x);
//    }
//    else if(x>=t->data)
//    {
//        insert(t->rchild,x);
//    }
//    return NULL;
//}
//int d=0;
//void preTraval(Tree *t,int k)
//{
//    if(t)
//    {
//        d++;
//        //cout<<d<<endl;
//        if(d==k)
//        {
//            cout<<t->data;
//            return;
//        }
//        preTraval(t->lchild,k);
//        preTraval(t->rchild,k);
//    }
//}
//int main()
//{
//    Tree *t=NULL;
//    int n;
//    cin>>n;
//    int c,k;
//    for(int i=0;i<n;i++)
//    {
//        cin>>c;
//        insert(t,c);
//    }
//    cin>>k;
//    preTraval(t,k);
//}
//
// 6
// 5 3 1 4 9 7
// 5
// 9



 //删除元素值为x的元素为根的子树 P123 11
//typedef struct Tree
//{
//    int data;
//    struct Tree *lchild,*rchild;
//}Tree;
//void *insert(Tree *&t,int x)
//{
//    if(t==NULL)
//    {
//        t=(Tree*)malloc(sizeof(Tree));
//        t->data=x;
//        t->lchild=NULL;
//        t->rchild=NULL;
//    }
//    else if(x<t->data)
//    {
//        insert(t->lchild,x);
//    }
//    else if(x>=t->data)
//    {
//        insert(t->rchild,x);
//    }
//    return NULL;
//}
//void Delete(Tree *&t,int x)
//{
//    if(t)
//    {
//        if(t->data==x)
//        {
//            t->lchild=NULL;
//            t->rchild=NULL;
//            t=NULL;
//            return;
//        }
//        Delete(t->lchild,x);
//        Delete(t->rchild,x);
//    }
//    return;
//}
//void PostTravel(Tree *t)
//{
//    if(t)
//    {
//        PostTravel(t->lchild);
//        PostTravel(t->rchild);
//        cout<<t->data<<" ";
//    }
//}
//int main()
//{
//    Tree *t=NULL;
//    int n;
//    cin>>n;
//    int c,k;
//    for(int i=0;i<n;i++)
//    {
//        cin>>c;
//        insert(t,c);
//    }
//    int x;
//    cin>>x;
//    Delete(t,x);
//    PostTravel(t);
//}
//
//6
//5 3 1 4 9 7
//9
//1 4 3 5
//



找出值为x的节点的所有祖先 P123 12  用parent指针指向祖先节点!注意点:根节点的parent要赋值为NULL!
标准答案的方法:非递归后序遍历!栈中存储的是本节点所有的祖先节点!!
//typedef struct Tree
//{
//    int data;
//    struct Tree *lchild,*rchild,*parent;
//}Tree;
//void *insert(Tree *&t,int x)
//{
//    if(t==NULL)
//    {
//        t=(Tree*)malloc(sizeof(Tree));
//        t->data=x;
//        t->lchild=NULL;
//        t->rchild=NULL;
//    }
//    else if(x<t->data)
//    {
//        insert(t->lchild,x);
//    }
//    else if(x>=t->data)
//    {
//        insert(t->rchild,x);
//    }
//    return NULL;
//}
//void PreTraval(Tree *t)
//{
//    if(t)
//    {
//        if(t->lchild)
//        {
//            t->lchild->parent=t;
//        }
//        if(t->rchild)
//        {
//            t->rchild->parent=t;
//        }
//        PreTraval(t->lchild);
//        PreTraval(t->rchild);
//    }
//}
//void PostTravel(Tree *t,int x)
//{
//    if(t)
//    {
//        PostTravel(t->lchild,x);
//        PostTravel(t->rchild,x);
//        if(t->data==x)
//        {
//            while(t->parent!=NULL)
//            {
//                t=t->parent;
//                cout<<t->data<<endl;
//            }
//            return;
//        }
//
//    }
//}
//int main()
//{
//    Tree *t=NULL;
//    int n;
//    cin>>n;
//    int c;
//    for(int i=0;i<n;i++)
//    {
//        cin>>c;
//        insert(t,c);
//    }
//    t->parent=NULL;
//    int x;
//    cin>>x;
//    PreTraval(t);
//    PostTravel(t,x);
//}

/*
//找p,q最近公共祖先节点 P123 13
typedef struct Tree
{
    int data;
    struct Tree *lchild,*rchild,*parent;
    int lev;
}Tree;
void *insert(Tree *&t,int x)
{
    if(t==NULL)
    {
        t=(Tree*)malloc(sizeof(Tree));
        t->data=x;
        t->lchild=NULL;
        t->rchild=NULL;
    }
    else if(x<t->data)
    {
        insert(t->lchild,x);
    }
    else if(x>=t->data)
    {
        insert(t->rchild,x);
    }
    return NULL;
}
void PreOrder(Tree *t)
{
    Tree *stack[maxSize];
    t->parent=NULL;
    int top=-1;
    while(top!=-1||t)
    {
        while(t)
        {
            stack[++top]=t;
            if(t->lchild)
                t->lchild->parent=t;
            t=t->lchild;
        }
        t=stack[top--];
        if(t->rchild)
        {
            t->rchild->parent=t;
        }
        t=t->rchild;
    }
}

void Ancestor(Tree *t,Tree *p,Tree *q,Tree *&r)
{
    while(p)
    {
        Tree *s=q;
        while(s)
        {
            if(p==s)
            {
                r=p;
                return;
            }
            s=s->parent;
        }
        p=p->parent;
    }
}

int main()
{
    Tree *t=NULL;
    int n;
    cin>>n;
    int c;
    for(int i=0;i<n;i++)
    {
        cin>>c;
        insert(t,c);
    }
    PreOrder(t);
    Tree *p=t->rchild->rchild;
    Tree *q=t->rchild->lchild->lchild;
    Tree *r=NULL;
    Ancestor(t,p,q,r);
    cout<<r->data;
}
    
//7
//4 3 1 7 9 6 5
//7
*/


 //求二叉树的宽度 P123 14
//typedef struct Tree
//{
//    int data;
//    struct Tree *lchild,*rchild;
//    int lev;
//}Tree;
//void *insert(Tree *&t,int x)
//{
//    if(t==NULL)
//    {
//        t=(Tree*)malloc(sizeof(Tree));
//        t->data=x;
//        t->lchild=NULL;
//        t->rchild=NULL;
//    }
//    else if(x<t->data)
//    {
//        insert(t->lchild,x);
//    }
//    else if(x>=t->data)
//    {
//        insert(t->rchild,x);
//    }
//    return NULL;
//}
//int BFS(Tree *t)
//{
//    Tree *queue[maxSize];
//    int front=0;
//    int rear=0;
//    int l=0;
//    queue[rear]=t;
//    queue[rear]->lev=0;
//    rear++;
//    while(front!=rear)
//    {
//        t=queue[front++];
//        l=t->lev;
//        if(t->lchild)
//        {
//            queue[rear]=t->lchild;
//            queue[rear]->lev=l+1;
//            rear++;
//        }
//        if(t->rchild)
//        {
//            queue[rear]=t->rchild;
//            queue[rear]->lev=l+1;
//            rear++;
//        }
//    }
//    int max=0;
//    for(int i=0;i<=l;i++)
//    {
//        int cnt=0;
//        for(int j=0;j<rear;j++)
//        {
//            if(queue[j]->lev==i)
//            {
//                cnt++;
//            }
//        }
//        if(cnt>max)
//            max=cnt;
//    }
//    return max;
//}
//int main()
//{
//    Tree *t=NULL;
//    int n;
//    cin>>n;
//    int c;
//    for(int i=0;i<n;i++)
//    {
//        cin>>c;
//        insert(t,c);
//    }
//    int d=BFS(t);
//    cout<<d<<endl;
//}
//
//7
//7 5 11 4 6 8 15
//4


//
由满二叉树的先序写出其后序序列 P123 15
//void PreTraval(int pre[],int post[],int l1,int r1,int l2,int r2)
//{
//    if(l1<=r1)   //一定要记得写控制条件!!!
//    {
//        post[r2]=pre[l1];
//        int half;
//        half=(r1-l1)/2;
//        PreTraval(pre,post,l1+1,l1+half,l2,l2+half-1);
//        PreTraval(pre,post,l1+half+1,r1,l2+half,r2-1);
//    }
//}
//int main()
//{
//    int n;
//    int pre[maxSize],post[maxSize];
//    cin>>n;
//    for(int i=1;i<=n;i++)
//        cin>>pre[i];
//    PreTraval(pre,post,1,n,1,n);
//    for(int i=1;i<=n;i++)
//        cout<<post[i]<<" ";
//}
//
//


/*
//将二叉树的叶子节点从左到右连成一个单链表 P126 16
typedef struct Tree
{
    int data;
    struct Tree *lchild,*rchild;
}Tree;
void *insert(Tree *&t,int x)
{
    if(t==NULL)
    {
        t=(Tree*)malloc(sizeof(Tree));
        t->data=x;
        t->lchild=NULL;
        t->rchild=NULL;
    }
    else if(x<t->data)
    {
        insert(t->lchild,x);
    }
    else if(x>=t->data)
    {
        insert(t->rchild,x);
    }
    return NULL;
}
void PreOrder(Tree *t,Tree *p)
{
    if(t)
    {
        if(t->lchild==NULL&&t->rchild==NULL)
        {
            p->rchild=t;
            p=t;
            p->rchild=NULL;
        }
        PreOrder(t->lchild,p);
        PreOrder(t->rchild,p);
    }
}
void preTraval(Tree *t,Tree *&head)
{
    Tree *p=head;
    Tree *stack[maxSize];
    int top=-1;
    while(top!=-1||t)
    {
        while(t)
        {
            stack[++top]=t;
            if(t->lchild==NULL&&t->rchild==NULL)
            {
                p->rchild=t;
                p=t;
                p->rchild=NULL;
            }
            t=t->lchild;
        }
        t=stack[top--];
        t=t->rchild;
    }
}
int main()
{
    Tree *t=NULL;
    int n;
    cin>>n;
    int c;
    for(int i=0;i<n;i++)
    {
        cin>>c;
        insert(t,c);
    }
    Tree *head=(Tree*)malloc(sizeof(Tree));
    preTraval(t,head);
    while(head->rchild)
    {
        cout<<head->rchild->data<<" ";
        head=head->rchild;
    }
}
//
//7
//4 2 6 1 3 5 7
//1 3 5 7
*/

//
判断两棵二叉树是否相识 P124 17
//typedef struct Tree
//{
//    int data;
//    struct Tree *lchild,*rchild;
//}Tree;
//void *insert(Tree *&t,int x)
//{
//    if(t==NULL)
//    {
//        t=(Tree*)malloc(sizeof(Tree));
//        t->data=x;
//        t->lchild=NULL;
//        t->rchild=NULL;
//    }
//    else if(x<t->data)
//    {
//        insert(t->lchild,x);
//    }
//    else if(x>=t->data)
//    {
//        insert(t->rchild,x);
//    }
//    return NULL;
//}
//int Judge(Tree *t1,Tree *t2)
//{
//    if(t1&&t2)
//    {
//        if(t1->data!=t2->data)
//        {
//            return 0;
//        }
//        int l1=Judge(t1->lchild,t2->lchild);
//        int l2=Judge(t1->rchild,t2->rchild);
//        return l1&&l2;
//    }
//    else if(t1==NULL&&t2==NULL)
//    {
//        return 1;
//    }
//    return 0;
//}
//int main()
//{
//    Tree *t1=NULL;
//    int n;
//    cin>>n;
//    int c;
//    for(int i=0;i<n;i++)
//    {
//        cin>>c;
//        insert(t1,c);
//    }
//    Tree *t2=NULL;
//    for(int i=0;i<n;i++)
//    {
//        cin>>c;
//        insert(t2,c);
//    }
//    int d=Judge(t1,t2);
//    cout<<d<<endl;
//}
//
//6
//5 3 1 4 9 7
//5 3 4 1 9 7
//1



/*
写出中序线索二叉树里查找指定节点在后序的前驱节点 P124 18
typedef struct Tree
{
    int data;
    struct Tree *lchild,*rchild;
    int ltag,rtag;
}Tree;
void *insert(Tree *&t,int x)
{
    if(t==NULL)
    {
        t=(Tree*)malloc(sizeof(Tree));
        t->data=x;
        t->lchild=NULL;
        t->rchild=NULL;
    }
    else if(x<t->data)
    {
        insert(t->lchild,x);
    }
    else if(x>=t->data)
    {
        insert(t->rchild,x);
    }
    return NULL;
}
Tree *Find(Tree *t)
{
    if(t->rtag==0)
    {
        return t->rchild;
    }
    if(t->ltag==0)
    {
        return t->lchild;
    }
    return t->lchild->lchild;
}
void InThread(Tree *&t,Tree *&pre)
{
    if(t)
    {
        InThread(t->lchild,pre);
        if(t->lchild==NULL)
        {
            t->ltag=1;
            t->lchild=pre;
        }
        if(pre!=NULL&&pre->rchild==NULL)
        {
            pre->rtag=1;
            pre->rchild=t;
        }
        pre=t;   //易忽视的点,有pre的话一定记得推进pre的前进!!!
        InThread(t->rchild,pre);
    }
}
void CreateThread(Tree *&t)
{
    Tree *pre=NULL;
    if(t)
    {
        InThread(t,pre);
        pre->rchild=NULL;
        pre->rtag=1;
    }
}
int main()
{
    Tree *t=NULL;
    int n;
    cin>>n;
    int c;
    for(int i=0;i<n;i++)
    {
        cin>>c;
        insert(t,c);
    }
    CreateThread(t);
    Tree *s=Find(t->lchild->rchild);
    cout<<s->data;
}
*/


//
//
求二叉树的带权路径长度 P124 19
//typedef struct Tree
//{
//    int weight;
//    struct Tree *lchild,*rchild;
//}Tree;
//void *insert(Tree *&t,int x)
//{
//    if(t==NULL)
//    {
//        t=(Tree*)malloc(sizeof(Tree));
//        t->weight=x;
//        t->lchild=NULL;
//        t->rchild=NULL;
//    }
//    else if(x<t->weight)
//    {
//        insert(t->lchild,x);
//    }
//    else if(x>=t->weight)
//    {
//        insert(t->rchild,x);
//    }
//    return NULL;
//}
//int BFS(Tree *t)
//{
//    Tree *queue[maxSize];
//    int front=0;
//    int rear=0;
//    queue[rear++]=t;
//    int sum=0;
//    Tree *newNode=t;
//    Tree *lastNode=t;
//    int deep=0;
//    while(front!=rear)
//    {
//        t=queue[front++];
//        if(t->lchild==NULL&&t->rchild==NULL)
//        {
//            sum+=t->weight*deep;
//        }
//        if(t->lchild)
//        {
//            queue[rear++]=t->lchild;
//            newNode=t->lchild;
//        }
//        if(t->rchild)
//        {
//            queue[rear++]=t->rchild;
//            newNode=t->rchild;
//        }
//        if(t==lastNode)
//        {
//            deep++;
//            lastNode=newNode;
//        }
//    }
//    return sum;
//    return sum;
//}
//int main()
//{
//    Tree *t1=NULL;
//    int n;
//    cin>>n;
//    int c;
//    for(int i=0;i<n;i++)
//    {
//        cin>>c;
//        insert(t1,c);
//    }
//    int d=BFS(t1);
//    cout<<d;
//}
//
//
//6
//5 3 1 4 9 10
//30
//
//
//
将表达式树转化为中缀表达式  P124 20
//typedef struct Tree
//{
//    char data[maxSize];
//    struct Tree *lchild,*rchild;
//}Tree;
//int n=0;
//char seq[maxSize];
//void InOrder(Tree *t)
//{
//    if(t)
//    {
//        seq[n++]='(';
//        InOrder(t->lchild);
//        for(int i=0;t->data[i]!='0';i++)
//            seq[n++]=t->data[i];
//        seq[n++]=')';
//        InOrder(t->rchild);
//    }
//}
//int main()
//{
//    Tree *t1=(Tree*)malloc(sizeof(Tree));
//    strcpy(t1->data,"*");
//    Tree *t2=(Tree*)malloc(sizeof(Tree));
//    strcpy(t2->data,"+");
//    Tree *t3=(Tree*)malloc(sizeof(Tree));
//    strcpy(t3->data,"a");
//    t3->lchild=NULL;
//    t3->rchild=NULL;
//    Tree *t4=(Tree*)malloc(sizeof(Tree));
//    strcpy(t4->data,"b");
//    t4->lchild=NULL;
//    t4->rchild=NULL;
//    t1->lchild=t2;
//    t2->lchild=t3;
//    t2->rchild=t4;
//    Tree *t5=(Tree*)malloc(sizeof(Tree));
//    strcpy(t5->data,"*");
//    t1->rchild=t5;
//    Tree *t6=(Tree*)malloc(sizeof(Tree));
//    strcpy(t6->data,"c");
//    t5->lchild=t6;
//    t6->lchild=NULL;
//    t6->rchild=NULL;
//    Tree *t7=(Tree*)malloc(sizeof(Tree));
//    strcpy(t7->data,"-");
//    t5->rchild=t7;
//    Tree *t8=(Tree*)malloc(sizeof(Tree));
//    strcpy(t8->data,"d");
//    t8->lchild=NULL;
//    t8->rchild=NULL;
//    t7->rchild=t8;
//    InOrder(t1);
//    for(int i=0;i<n;i++)
//        cout<<seq[i]<<" ";
//}
//
//
//
求以孩子兄弟表示法存储的森林的叶子节点数 P148 5
//typedef struct CSNode
//{
//    int data;
//    struct CSNode *firstchild,*nextsibling;
//}CSNode;
//int Count(CSNode *t)
//{
//    if(t->firstchild==NULL)
//    {
//        return 1+Count(t->nextsibling);
//    }
//    if(t)
//    {
//        return 0;
//    }
//    return Count(t->firstchild)+Count(t->nextsibling);
//}
//
//
//
//
以孩子兄弟表示法为存储结构,递归求树的深度 P148 6
//typedef struct CSNode
//{
//    int data;
//    struct CSNode *firstchild,*nextsibling;
//}CSNode;
//int height(CSNode *t)
//{
//    if(t==NULL)
//    {
//        return 0;
//    }
//    return max(1+height(t->firstchild),height(t->nextsibling));
//}
//
//
//
//
已知一棵树的层次序列和每个节点的度,构造此树的孩子兄弟链表 P148 7
//typedef struct CSNode
//{
//    int data;
//    struct CSNode *firstchild,*nextsibling;
//}CSNode;
用一个辅助数组存储树的各节点的地址
//void CreateTree(CSNode *&t,int level[],int cnt[],int n)
//{
//    CSNode *point[maxSize];
//    int i,j,d=0,k=0;
//    for(i=0;i<n;i++)
//    {
//        point[i]=(CSNode*)malloc(sizeof(CSNode));
//        point[i]->data=level[i];
//        point[i]->firstchild=NULL;
//        point[i]->nextsibling=NULL;
//    }
//    for(i=0;i<n;i++)
//    {
//        d=cnt[i];
//        if(d)
//        {
//            k++;
//            point[i]->firstchild=point[k];
//            for(j=2;j<=d;j++)
//            {
//                point[j-1]->nextsibling=point[j];
//            }
//        }
//    }
//    t=point[0];
//}
//int Count(CSNode *t)
//{
//    if(t->firstchild==NULL)
//    {
//        return 1+Count(t->nextsibling);
//    }
//    if(t)
//    {
//        return 0;
//    }
//    return Count(t->firstchild)+Count(t->nextsibling);
//}
//void Traval(CSNode *t)
//{
//    while(t)
//    {
//        cout<<t->data<<" ";
//        t=t->firstchild;
//    }
//}
//int main()
//{
//    CSNode *t;
//    int n;
//    int level[maxSize],cnt[maxSize];
//    cin>>n;
//    for(int i=0;i<n;i++)
//    {
//        cin>>level[i];
//    }
//    for(int i=0;i<n;i++)
//    {
//        cin>>cnt[i];
//    }
//    CreateTree(t,level,cnt,n);
//    Traval(t);
//    int d=Count(t);
//    cout<<d;
//}














 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值