#include<cstdio> #include<string.h> #include<algorithm> #include<stdlib.h> #include<iostream> using namespace std; struct node { int val; node *left; node *right; node() {} node(int xx) { val=xx;left=right=NULL; } }Tree; void Build(node *tree,int x)//建树的过程就是二分查找的过程 { if(tree->val>x) { if(tree->left==NULL) tree->left=new node(x); else Build(tree->left,x); } else if(tree->val<=x) { if(tree->right==NULL) tree->right=new node(x); else Build(tree->right,x); } } void InOrder(node *tree) { if(tree!=NULL) { InOrder(tree->left); printf(“%d “,tree->val); InOrder(tree->right); //printf(“%d “,tree->val); } } //void PostOrder(node* tree) //{ // if(tree!=NULL) // { // PostOrder(tree->left); // // PostOrder(tree->right); // delete tree; // } // //} int Bsearch(node *tree,int key)//此查找就是二分查找的过程 { if(tree==NULL) return -1;//表示没有此数据;查找失败 else { if(tree->val==key)//查找成功 return key; else if(tree->val>key) { Bsearch(tree->left,key); } else if(tree->val<key) { Bsearch(tree->right,key); } } } int main() { int x; int n; cin>>n; cin>>x; Tree.val=x; n–; while(n–) { cin>>x; Build(&Tree,x); } InOrder(&Tree);//已经是排过序的; printf(“\n”); //PostOrder(&Tree);//释放内存 printf(“\n”); cout<<“hello word”<<endl; return 0; }
二叉排序树,二叉检索树,二叉查找树指的都是同一一个东西,以前自己都被这些概念搞迷了;现在终于明白了,线段树也是二叉排序树的一种变形;所以说掌握二叉排序树是基本的要求,以后还有好多神奇的数据结构都是二叉排序树,任重道远;