二叉树的建立和打印

来源:互联网 发布:手机淘宝怎么看评分 编辑:程序博客网 时间:2024/05/23 12:08
#include "iostream"
using namespace  std;
#define ElemType int
typedef 
struct bNode
{
    ElemType data;
    bNode 
*left,*right;
    
int lTag,//左标志,0指向孩子,1左线索
    int rTag;//右标志,0孩子,1右线索
}
bTree;


//////////////////////////////////////////////////////////////////////////
//生成二叉树
void insert(bTree *&p,bNode *s)//必须将之改为指针的引用
{
    
if (p==NULL){
        p
=s;
    }
else if(s->data>=p->data){
        insert(p
->right,s);
    }
else if (s->data<p->data){
        insert(p
->left,s);
    }

}


void Create(bTree *&b)//改为指针的引用
{    
    ElemType x;
    bTree 
*s;
    
do 
    
{
        cout
<<"请输入节点(-1结束)"<<endl;
        cin
>>x;
        s
=new bNode;
        s
->data=x;
        s
->left=NULL;
        s
->right=NULL;
        insert(b,s);
    }
 while(x!=-1);
}


int main()
{
    bTree 
*tree=NULL;
    Create(tree);
    printTree(tree);
    
return 0;
}
 
原创粉丝点击