二叉排序树

来源:互联网 发布:软件授权码绑定 编辑:程序博客网 时间:2024/04/30 21:03

如果你了解二叉树的特性,和二叉排序树的排序原理的话就比较好写了,不懂的话可以 看看资料~~


#include <iostream>#include <cstring>using namespace std;struct tree{  int date;  struct tree *left;  struct tree *right;};void init (tree *&t){  t=(tree*)malloc(sizeof(tree));  t->left=NULL;  t->right=NULL;}void Create_BST(tree *&t,int key){  if(t==NULL){    tree *s;init(s);s->date=key;t=s; return ;  }     if(key>=t->date) Create_BST(t->right,key);   if(key<t->date)  Create_BST(t->left,key);    return ;}void BST_LDR(tree *&t) {  if(t==NULL) return ;  BST_LDR(t->left);  cout<<t->date<<" ";  BST_LDR(t->right);}int main(){  int n;  while(cin>>n)  {      int s;     tree *t; for(int i=1;i<=n;i++)       {        cin>>s;        if(i==1){          init(t); t->date=s; continue;               }        Create_BST(t,s);       }    BST_LDR(t);            }return 0;}



原创粉丝点击