⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binarytree2.h

📁 通过程序预先输入的字母
💻 H
字号:
struct Tnode         //树结点的结构 
{
       Tnode * left;
       Tnode * right;
       Tnode * parent;
       char data;
       int pos;     //节点的位置 
};

int size(Tnode *); //计算树的结点个数 
int level(Tnode *);//计算结点层数 
Tnode * creatTree();//创建一棵树,返回root 

Tnode * creatTree()
{   
    Tnode * a=new Tnode;
    a->data='a';
    Tnode * b=new Tnode;
    b->data='b';
    Tnode * c=new Tnode;
    c->data='c';
    Tnode * d=new Tnode;
    d->data='d';
    Tnode * e=new Tnode;
    e->data='e';
    Tnode * f=new Tnode;
    f->data='f';
    Tnode * g=new Tnode;
    g->data='g';
    Tnode * h=new Tnode;
    h->data='h';
    Tnode * i=new Tnode;
    i->data='i';
    Tnode * j=new Tnode;
    j->data='j';
    Tnode * k=new Tnode;
    k->data='k';
    Tnode * l=new Tnode;
    l->data='l';
    Tnode * m=new Tnode;
    m->data='m';
    Tnode * n=new Tnode;
    n->data='n';
    
    n->left=NULL;n->right=NULL;n->parent=e;
    m->left=NULL;m->right=NULL;m->parent=i;
    l->left=NULL;l->right=NULL;l->parent=e;
    k->left=NULL;k->right=NULL;k->parent=j;
    j->left=k;j->right=NULL;j->parent=i;
    h->left=i;h->right=NULL;h->parent=f;
    i->left=j;i->right=m;i->parent=h;
    g->left=NULL;g->right=NULL;g->parent=f;
    f->left=g;f->right=h;f->parent=a;
    d->left=NULL;d->right=NULL;d->parent=c;
    e->left=n;e->right=l;e->parent=b;
    c->left=NULL;c->right=d;c->parent=b;
    
    a->left=b;a->right=f;a->parent=NULL;
    b->left=c;b->right=e;b->parent=a;

    
    return a;
}
int size(Tnode * root) 
{
    if(root==NULL)
       return 0;
    else
       return 1+size(root->left)+size(root->right);
}
int level(Tnode * root)
{
    if(root==NULL)
       return 0;
    else
    {
        int count=1;
        for(;root!=NULL;count++)
           root=root->parent;
        return count;
    } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -