首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

hdu3999 the order of a tree - BST二叉搜寻树的前序遍历

2013-11-09 
hdu3999 the order of a tree - BST二叉搜索树的前序遍历hdu3999 the order of a tree二叉搜索树(BST)跟字

hdu3999 the order of a tree - BST二叉搜索树的前序遍历

hdu3999 the order of a tree

二叉搜索树(BST)

跟字典树相比,BST占用的资源少很多

所以要不要释放资源都无所谓?

还是养成一个好习惯吧,,

#include<iostream>using namespace std;struct BST{int num;BST *lson,*rson;BST(){num=-1;lson=rson=0;}} ;BST *insert(BST *p,int x){if(!p){p=new BST;p->num=x;return p;}if(x<p->num)p->lson=insert(p->lson,x);if(x>p->num)p->rson=insert(p->rson,x);return p;}void pre_traversal(BST *p,bool blank){if(p)if(blank==0)printf("%d",p->num);elseprintf(" %d",p->num);if(p->lson) pre_traversal(p->lson,1);if(p->rson) pre_traversal(p->rson,1);}void Free(BST *p){if(p->lson) Free(p->lson);if(p->rson) Free(p->rson);if(p) delete p;}int main(){int n,x,i;      //n个数while(scanf("%d",&n)!=EOF){BST *p=0;for(i=1;i<=n;i++){scanf("%d",&x);p=insert(p,x);}pre_traversal(p,0);printf("\n");Free(p);                    //可能因为这题数据较弱,Free的效果不明显,, }return 0;}


热点排行