Binary Tree

/* DATE: 2006-12-02 BY:snowhill */

#include "iostream.h"
/* definition */
struct bitree
{
 int data;
 bitree *lchild,*rchild;
};
//插入
void Insert(bitree *&BST,int x)
{  
 if(BST==NULL)
 {
  bitree *p=new bitree;
  p->data=x;
  
  p->lchild=p->rchild=NULL;
  BST=p;
 }
 else if(BST->data>=x)
   Insert(BST->lchild,x);
 else
  Insert(BST->rchild,x);
}

//create a binary Tree with n nodes

bitree *create(bitree *BST,int n)
{  
 int x;
 BST=NULL;
 for(int i=1;i<=n;i++)
 { 
  cin>>x;
  Insert(BST,x);
 }
 if(BST==NULL)
  cout<<"It must be fun!"<<endl;
 return BST;
  
}
//先序遍历
void preorder(bitree *BST)
{
 bitree *p;
 p=BST;
 if(p!=NULL)
 {
  
 cout<<p->data<<"->";
 preorder(p->lchild);
 preorder(p->rchild);
 }
 
}

/*  主函数  */
void main()
{
 bitree *BST;
 BST=NULL;
 BST=create(BST,4);
 preorder(BST);
   
}

posted on 2006-12-02 19:31 snowhill 阅读(244) 评论(0)  编辑 收藏 引用 所属分类: data structure


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


<2006年12月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

公告

又一年...........

留言簿(3)

随笔分类(13)

文章分类(131)

文章档案(124)

c++

java

linux

oracle

常用软件

其他

网络配置

系统安全

音乐

搜索

最新评论

阅读排行榜