life02

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks
#include <iostream>
using namespace std;

typedef 
struct node{
    
int key;
    
int data;
    
struct node *lchild,*rchild;
}
;

node
* bt=NULL;

int insertbst(node *&p,int k){
    
if (p==NULL)
    
{
        p
=(node*)malloc(sizeof(node));
        p
->key=k;
        p
->lchild=p->rchild=NULL;
        
return 1;
    }

    
else if (k==p->key)
    
{
        
return 0;
    }
else if (k<p->key)
    
{
        
return insertbst(p->lchild,k);
    }
else
    
{
        
return insertbst(p->rchild,k);
    }

}


node
* creatbst(int* a,int n){
    
    
int i=0;
    
while(i<n){
        insertbst(bt,a[i]);
        
++i;
    }

    
return bt;
}


void preorder(node* bt){
    
if (bt!=NULL)
    
{
        cout
<<bt->key<<endl;
        preorder(bt
->lchild);
        preorder(bt
->rchild);
    }

}




int main(){
    
int a[5]={12,3,4,8,10};
    node
* bl=new node();
    bl
=creatbst(a,5);
    preorder(bt);

    
return 0;
}
posted on 2009-09-21 22:46 life02 阅读(128) 评论(0)  编辑 收藏 引用 所属分类: c++学习

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理