18版考研数据结构天勤课后习题代码-二叉树【未完】

#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

typedef struct BiTree
{
    char data;
    int lev;
    struct BiTree *lchild,*rchild,*parent;
}BiTree;

/*
//计算表达式的值 天勤P39 例6-1
BiTree *insert(BiTree *&root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}

int Calu(BiTree *t)
{
    if(t!=NULL)
    {
        if(t->lchild!=NULL&&t->rchild!=NULL)
        {
        int a=Calu(t->lchild);
        int b=Calu(t->rchild);
        if(t->data=='+')
        {
            int s=a+b;
            return s;
        }
        else if(t->data=='-')
        {
            int s=a-b;
            return s;
        }
        if(t->data=='*')
        {
            int s=a*b;
            return s;
        }
        if(t->data=='/')
        {
            int s=a/b;
            return s;
        }
        }
        else
            return t->data-'0';
    }
    return 0;
}

void inOrder(BiTree *root)
{
    if(root)
    {
        inOrder(root->lchild);
        cout<<root->data;
        inOrder(root->rchild);
    }
}
int main()
{
    BiTree *root=(BiTree*)malloc(sizeof(BiTree));
    root->data='*';
    BiTree *l1=(BiTree*)malloc(sizeof(BiTree));
    l1->data='-';
    BiTree *l2=(BiTree*)malloc(sizeof(BiTree));
    l2->data='/';
    root->lchild=l1;
    root->rchild=l2;
    BiTree *l3=(BiTree*)malloc(sizeof(BiTree));
    l3->data='4';
    BiTree *l4=(BiTree*)malloc(sizeof(BiTree));
    l4->data='3';
    l1->lchild=l3;
    l1->rchild=l4;
    BiTree *l5=(BiTree*)malloc(sizeof(BiTree));
    l5->data='8';
    BiTree *l6=(BiTree*)malloc(sizeof(BiTree));
    l6->data='4';
    l2->lchild=l5;
    l2->rchild=l6;
    int d=Calu(root);
    cout<<d<<endl;
}
*/

/*
//求二叉树的深度  天勤P140 例6-2
BiTree *insert(BiTree *root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}
int depth(BiTree *t)
{
    if(t!=NULL)
    {
        int d1=depth(t->lchild);
        int d2=depth(t->rchild);
        return max(d1,d2)+1;
    }
    return 0;
}
int main()
{
    BiTree *root=NULL;
    int n,a[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        root=insert(root,a[i]);
    }
    int d=depth(root);
    cout<<d<<endl;
    
}
5
2 1 3 4 5
*/

/*
//查找值为key的节点 天勤P140 例6-3
BiTree *insert(BiTree *&root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}
void inOrder(BiTree *root)
{
    if(root)
    {
        inOrder(root->lchild);
        cout<<root->data;
        inOrder(root->rchild);
    }
}
void Search(BiTree *t,char key,BiTree *&q)
{
    if(t!=NULL)
    {
        Search(t->lchild,key,q);
        if(t->data==key)
        {
            q=t;
        }
        Search(t->rchild,key,q);
    }
}
int main()
{
    BiTree *root=NULL;
    int n;
    char a[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        root=insert(root,a[i]);
    }
    //inOrder(root);
     BiTree *q=NULL;
     Search(root,'d',q);
     cout<<q->data<<endl;
    
}
 */

/*
//输出先序序列中第k个节点的值 天勤P142 例6-4
BiTree *insert(BiTree *&root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}
void preOrder(BiTree *t,int k)
{
    stack<BiTree*> s;
    int x=0;
    while(t!=NULL||!s.empty())
    {
        while(t)
        {
            s.push(t);
            x++;
            if(x==k)
            {
                cout<<t->data;
            }
            t=t->lchild;
        }
        if(!s.empty())
        {
            t=s.top();
            s.pop();
            t=t->rchild;
        }
    }
}
int main()
{
    BiTree *root=NULL;
    int n;
    char a[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        root=insert(root,a[i]);
    }
    preOrder(root,2);
}
*/

/*
//求二叉树的宽度  天勤P144 例6-5
typedef struct BiTree
{
    char data;
    int lev;   //存放节点的层数
    struct BiTree *lchild,*rchild;
    
}BiTree;
BiTree *insert(BiTree *&root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}

int Width(BiTree *t)
{
    BiTree *q[maxSize];
    int front=1;
    int rear=1;
    int l;
    if(t!=NULL)  //不要写成while
    {
        t->lev=1;   //判断完了再入队
        q[rear]=t;
        rear++;
        while(rear!=front)
      {
        BiTree *s=q[front];
        l=s->lev;
        front++;
        if(s->lchild!=NULL)
        {
            s->lchild->lev=l+1;
            q[rear]=s->lchild;
            rear++;
        }
        if(s->rchild!=NULL)
        {
            s->rchild->lev=l+1;
            q[rear]=s->rchild;
            rear++;
        }
      }
    }
    int max=0;
    for(int i=1;i<=l;i++)
    {
        int t=0;
        for(int j=1;j<rear;j++)
        {
            if(q[j]->lev==i)
            {
                t++;
            }
        }
        if(t>max)
        {
            max=t;
        }
    }
    return max;
  }

int main()
{
    BiTree *root=NULL;
    int n;
    char a[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        root=insert(root,a[i]);
    }
    //inOrder(root);
    int d=Width(root);
    cout<<d<<endl;
}
*/

/*
//由前序序列和中序序列构造二叉树  天勤P162 真题仿造
BiTree *CreateTree(char pre[],char in[],int pre1,int pre2,int in1,int in2)
{
    if(pre1>pre2)  //注意跳出条件,注意和二叉树的insert函数区分,insert函数只是插入一个节点,而此函数是根据一个数组建一棵树,因此建完节点不用 return,越界时才return
    {
        return NULL;
    }
        BiTree *t=(BiTree*)malloc(sizeof(BiTree));
        t->data=pre[pre1];
        t->lchild=t->rchild=NULL;
    int k;
    for(int i=in1;i<=in2;i++)
    {
        if(in[i]==pre[pre1])
        {
            k=i;
            break;
        }
    }
    t->lchild=CreateTree(pre,in,pre1+1,pre1+k-in1,in1,k-1);  //易错点!!!注意下标不要写错了
 
    t->lchild=CreateTree(t->lchild,pre,in,pre1+1,pre1+k-in1,in1,k-1);
 
    t->rchild=CreateTree(pre,in,pre1+k-in1+1,pre2,k+1,in2);
    return t;
}
 
void postOrder(BiTree *t)
{
    if(t!=NULL)
    {
        postOrder(t->lchild);
        postOrder(t->rchild);
        cout<<t->data<<endl;
    }
}
int main()
{
    char pre[101];
    char in[101];
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>pre[i];
    }
    for(int i=1;i<=n;i++)
    {
        cin>>in[i];
    }
    BiTree *root=NULL;
    root=CreateTree(pre,in,1,n,1,n);
    postOrder(root);
    
}
*/

/*
//计算二叉树所有节点树
BiTree *insert(BiTree *&root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}

int Count(BiTree *t)
{
    if(t==NULL)
        return 0;
    else
    {
        return 1+Count(t->lchild)+Count(t->rchild);
    }
}

int d=0;
void Count(BiTree *t)
{
    if(t==NULL)
    {
        return;
    }
    else
    {
        d++;
        Count(t->lchild);
        Count(t->rchild);
    }
}
int main()
{
    BiTree *root=NULL;
    int n;
    char a[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        root=insert(root,a[i]);
    }
    //inOrder(root);
    //int d=Count(root);
    Count(root);
    cout<<d<<endl;
}
*/

/*
//计算树上的所有叶子节点 天勤P165 (二)(3)
BiTree *insert(BiTree *&root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}
int cnt=0;
void preOrder(BiTree *t)
{
    if(t==NULL)
        return ;
    else
    {
        if(t->lchild==NULL&&t->rchild==NULL)
        {
            cnt++;
        }
        preOrder(t->lchild);
        preOrder(t->rchild);
    }
}
int main()
{
    BiTree *root=NULL;
    int n;
    char a[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        root=insert(root,a[i]);
    }
    preOrder(root);
    cout<<cnt<<endl;
}
*/

/*
//串叶子节点链接成链表 天勤P165 (二)(5)
BiTree *insert(BiTree *&root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}


void PreOrder(BiTree *t,BiTree *&head,BiTree *&tail)
{
    BiTree* stack[maxSize];
    int top=-1;
    int flag=0;
    while(t||top!=-1)
    {
        while(t)
        {
            stack[++top]=t;
            if(t->lchild==NULL&&t->rchild==NULL&&flag==0)
            {
                flag=1;
                head=t;
                tail=head;
                tail->rchild=NULL;
            }
            else if(t->lchild==NULL&&t->rchild==NULL&&flag==1)
            {
                tail->rchild=t;
                tail=t;
                tail->rchild=NULL;
            }
            t=t->lchild;
            
        }
        if(top!=-1)
        {
            t=stack[top];
            top--;
            t=t->rchild;
        }
    }
}
void PreOrder(BiTree *t,BiTree *&head,BiTree *&tail)
{
    if(t!=NULL)
    {
        if(t->lchild==NULL&&t->rchild==NULL)
        {
            if(head==NULL)
            {
                head=t;
                tail=head;
                tail->rchild=NULL;
            }
            else
            {
                tail->rchild=t;
                tail=t;
                tail->rchild=NULL;
            }
        }
        PreOrder(t->lchild,head,tail);
        PreOrder(t->rchild,head,tail);
    }
}

 
//void preOrder(BiTree *t)
//{
//    if(t==NULL)
//        return ;
//    else
//    {
//        cout<<t->data<<endl;
//        preOrder(t->lchild);
//        preOrder(t->rchild);
//    }
//}

int main()
{
    BiTree *root=NULL;
    int n;
    char a[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        root=insert(root,a[i]);
    }
    //preOrder(root);
    BiTree *head=NULL;
    BiTree *tail=NULL;
    PreOrder(root,head,tail);
    BiTree *p;
    p=head;
    while(p!=NULL)
    {
        cout<<p->data<<endl;
        p=p->rchild;
    }
}

7
d b e a f c g
*/

/*
//增加指向双亲节点的指针 天勤P166 (二)(5)
typedef struct BiTree
{
    char data;
    struct BiTree *lchild,*rchild,*parent;
}BiTree;
BiTree *insert(BiTree *&root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}
void Create(BiTree *t)
{
    BiTree* stack[maxSize];
    int top=-1;
    t->parent=NULL;
    while(t!=NULL||top!=-1)
    {
        while(t!=NULL)
        {
            stack[++top]=t;
            if(t->lchild)
            {
                t->lchild->parent=t;
            }
            t=t->lchild;
        }
        if(top!=-1)
        {
            t=stack[top--];
            if(t->rchild)
            {
                t->rchild->parent=t;
            }
            t=t->rchild;
        }
    }
}

void Print(BiTree *t)
{
    if(t)
    {
        if(t->lchild==NULL&&t->rchild==NULL)
        {
            BiTree *s=t;
            while(s!=NULL)   //易错点!!!
            {
                cout<<s->data<<" ";
                s=s->parent;
            }
            cout<<endl;
        }
        Print(t->lchild);
        Print(t->rchild);
    }
}

int main()
{
    BiTree *root=NULL;
    int n;
    char a[101];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        root=insert(root,a[i]);
    }
    Create(root);
    Print(root);
}

7
d b e a f c g
a b d
c b d
g f e d
*/

/*
//满二叉树先序序列构造树,然后写出后序序列 天勤P166 (二)(6)
BiTree *CreateTree(char a[],int i,int n)
{
    BiTree *t;
    if (i>n)
    {
        return NULL;
    }
    t=(BiTree*)malloc(sizeof(BiTree));
    t->data=a[i];
    t->lchild=t->rchild=NULL;
    t->lchild=CreateTree(a,2*i,n);
    t->rchild=CreateTree(a,2*i+1,n);
    return t;
}

void postOrder(BiTree *t)
{
   if(t)
   {
       postOrder(t->lchild);
       postOrder(t->rchild);
       cout<<t->data<<" ";
   }
}

int main()
{
    int n;
    cin>>n;
    char a[101];
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    BiTree *root=NULL;
    root=CreateTree(a,1,n);
    postOrder(root);
}

7
a b c d e f g
d e b f g c a
*/

/*
//求二叉树中值为x的节点的层号 天勤P166 (8)
BiTree *insert(BiTree *&root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}
void Traval(BiTree *t,char x)
{
    BiTree *q[maxSize];
    int front=1;
    int rear=1;
    int l;
    if(t!=NULL)
    {
        q[rear]=t;
        q[rear]->lev=1;
        rear++;
        while(front!=rear)
        {
            BiTree *p=q[front];
            l=p->lev;
            if(p->data==x)
            {
                cout<<l<<endl;
                return;
            }
            front++;
            if(p->lchild)
            {
                q[rear]=p->lchild;
                q[rear]->lev=l+1;
                rear++;
            }
            if(p->rchild)
            {
                q[rear]=p->rchild;
                q[rear]->lev=l+1;
                rear++;
            }
        }
    }
}
int main()
{
    BiTree *root=NULL;
    int n;
    cin>>n;
    char a[101];
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        insert(root,a[i]);
    }
    Traval(root,'s');
}
*/

/*
//写双序遍历 天勤P166 (10)
void DoubleTraval(BiTree *t)
{
    if(t!=NULL)
    {
        cout<<t->data<<endl;
        DoubleTraval(t->lchild);
        cout<<t->data<<endl;
        DoubleTraval(t->rchild);
    }
}
BiTree *insert(BiTree *&root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}
int main()
{
    BiTree *root=NULL;
    int n;
    cin>>n;
    char a[101];
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        insert(root,a[i]);
    }
    DoubleTraval(root);
}
*/

/*
//输出从根到子节点的所有路径 天勤P166 2(1)
//思路:增加一个parent域,把从每个子节点出发找根节点的路径存放到栈里,再从栈输出节点,即为根节点到每个子节点的路径
void CreateTree(BiTree *t)  //参数部分不能加引用
{
    BiTree *stack[maxSize];
    int top=-1;
    t->parent=NULL;
    while(t||top!=-1)
    {
        while(t)
        {
            stack[++top]=t;
            if(t->lchild)
            {
                t->lchild->parent=t;
            }
            t=t->lchild;
        }
       if(top!=-1)
       {
           t=stack[top--];
           if(t->rchild)
           {
               t->rchild->parent=t;
           }
           t=t->rchild;
       }
    }
}
void Traval(BiTree *t)
{
    if(t)
    {
        if(t->lchild==NULL&&t->rchild==NULL)
        {
            char stack[maxSize];
            int top=-1;
            BiTree *s=t;   //一定要记得把t保存下来啊!!!!否则会改变t
            while(s)
            {
                top++;
                stack[top]=s->data;
                s=s->parent;
            }
            while(top!=-1)
            {
                cout<<stack[top]<<" ";
                top--;
            }
            cout<<endl;
        }
        Traval(t->lchild);
        Traval(t->rchild);
    }
}

void inOrder(BiTree *t)
{
    if(t)
    {
        cout<<t->data;
        inOrder(t->lchild);
        inOrder(t->rchild);
    }
}

BiTree *insert(BiTree *&root,char ch)
{
    if(root==NULL)
    {
        root=(BiTree*)malloc(sizeof(BiTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}
int main()
{
    BiTree *root=NULL;
    int n;
    cin>>n;
    char a[101];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        insert(root,a[i]);
    }
    CreateTree(root);
    Traval(root);
    //inOrder(root);
}

*/

/*
// 天勤P166 (11)1)
typedef  struct ThreadTree
{
    char data;
    struct ThreadTree *lchild,*rchild;
    int ltag,rtag;
}ThreadTree;
ThreadTree *insert(ThreadTree *root,char ch)
{
    if(root==NULL)
    {
        root=(ThreadTree*)malloc(sizeof(ThreadTree));
        root->data=ch;
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    else if(ch<=root->data)
    {
        root->lchild=insert(root->lchild,ch);
    }
    else if(ch>root->data)
    {
        root->rchild=insert(root->rchild,ch);
    }
    return root;
}
void CreateTree(ThreadTree *t,ThreadTree *pre)
{
    if(t)
    {
        CreateTree(t->lchild,pre);
        if(t->lchild==NULL&&pre)
       {
        t->lchild=pre;
        t->ltag=1;
       }
        if(pre->rchild==NULL&&t)
       {
        pre->rtag=1;
        pre->rchild=t;
       }
        pre=t;
        CreateTree(t->rchild,pre);
    }
}
//求中序线索二叉树上节点t的子树上中序下最后一个节点
ThreadTree *SearchInLast(ThreadTree *t)
{
    ThreadTree *p=t;
    while(p&&p->rtag==0)
    {
        p=p->rchild;
    }
    return p;
}

//求中序线索二叉树上节点t的中序下的前驱 天勤P166 (11)2)
ThreadTree *SearchInPre(ThreadTree *t)
{
    
}

//求中序线索二叉树上节点t的前序下的后继 天勤P166 (11)3)
ThreadTree *SearchPrePost(ThreadTree *t)
{
    ThreadTree *p=t;
    if(p->ltag==1)
    {
        p=p->rchild;
        while(p->ltag==0)
        {
            p=p->lchild;
        }
        return p;
    }
    else
    {
        while(p->ltag==0)
        {
            p=p->lchild;
        }
        return p;
    }
}
int main()
{
    ThreadTree *root=NULL;
    int n;
    cin>>n;
    char a[101];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        root=insert(root,a[i]);
    }
    CreateTree(root,NULL);
    
    
}

*/


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值