标签:int bit turn cpp empty highlight put 二叉查找树 logs
#include <bits/stdc++.h> using namespace std; struct Node{ Node *left,*right; int n; }; class BST{ public: void insert(int a){ if(!empty) insert1(a,root); else{ root=new Node; root->left=root->right=NULL; root->n=a; empty=false; } } bool find(int a){ return find1(a,root); } private: bool empty=true; Node *root; void insert1(int n,Node *now){ if(n==now->n) return; if(n<now->n){ if(now->left!=NULL) insert1(n,now->left); else{ Node *t=new Node; now->left=t; t->n=n; t->left=t->right=NULL; } } else{ if(now->right!=NULL) insert1(n,now->right); else{ Node *t=new Node; now->right=t; t->n=n; t->left=t->right=NULL; } } } bool find1(int n,Node *now){ if(n==now->n) return true; if(n<now->n){ if(now->left!=NULL) return find1(n,now->left); else return false; } else{ if(now->right!=NULL) return find1(n,now->right); else return false; } } }; int main(){ int f; BST tree; while(cin>>f){ if(f==1){ int a; cin>>a; tree.insert(a); } if(f==2){ int a; cin>>a; if(tree.find(a)) puts("Yes"); else puts("No"); } } return 0; }
标签:int bit turn cpp empty highlight put 二叉查找树 logs
原文地址:http://www.cnblogs.com/HC-LittleJian/p/7672552.html