Demo entry 6662576

好好

   

Submitted by anonymous on Nov 27, 2017 at 06:09
Language: C++. Code size: 1.5 kB.

#include<iostream>
using namespace std;

typedef char elementType;
typedef struct lbNode
{
    elementType data;
    struct lbNode *lChild,*rChild;
}Binode;

void createsubT(Binode *q,int k);
void initTree(Binode *&T)
{
   T=new Binode;
    T->lChild= NULL;
    T->rChild= NULL;
}

int createTree(Binode *& T)
{
    Binode *p;
    elementType x;
    cout<<"please input the tree element ";
    cin>>x;
    if(x == '/')
    {
         T=NULL;
        cout<<"the tree is empty ";
         return 0;
    }
     else
    {
        T->data = x;
        T->lChild=NULL;
        T->rChild=NULL;
        p=T;
        createsubT(p,1);  //将元素插到左子树
        createsubT(p,2);  //将元素插到右子树
    }
    return 1;
}

void createsubT(Binode *q,int k)
{
    Binode *u;
    elementType x;
    cin>>x;
    if(x != '/')
       {
           u=new Binode;
           u->data=x;
           u->lChild=NULL;
           u->rChild=NULL;
           if(k == 1)
             q->lChild=u;
            if(k == 2)
                q->rChild=u;
            createsubT(u,1);
            createsubT(u,2);
       }
}

void disTree(Binode *T,int &a1)
{
    if(T)
    {
        disTree(T->lChild,a1);
         a1++;
        cout<<T->data<<" "<<a1<<'\t';
        disTree(T->rChild,a1);
    }
}

int main()
{
       Binode *T,*R;
    int a1=0;
    initTree(T);
    createTree(T);
    R=T;
    disTree(T,a1);
  return 0;
} 

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).