写了一个二叉搜索树,搜索函数返回搜索次数,为什么次数一直是0,要哭了,
//二叉查找树#includeusing namespace std;class Node{ public: // int index;//索引 int data; Node *pLchild; Node *pRchild; Node(); void Addnode(int x,Node *&p); int Find(int x,Node *p,int &count); void insubtree(Node*node);}; Node::Node() { data=0; pLchild=NULL; pRchild=NULL;} void Node::Addnode(int x,Node *&p) { if(!p){ p=new Node(); p->data =x; p->pLchild =p->pRchild =NULL; } else{ if(xdata ){ Addnode(x,p->pLchild); } else if(x>p->data){ Addnode(x,p->pRchild); } }}int Node::Find(int x,Node *p,int &count){ if(p==NULL) return 0; count+=1; if(x==p->data ) return count; else if(x>p->data ) Find(x,p->pRchild ,count); else if(xdata ) Find(x,p->pLchild ,count);} void Node::insubtree(Node*node){ if(node==NULL) return; insubtree(node->pLchild);//先遍历左子树 coutpLchild =NULL; pRoot->pRchild =NULL;}void Tree::Addnode(int x){ pRoot->Addnode(x,pRoot); }int Tree::Find(int x) { int count=0; pRoot->Find(x,pRoot,count); }void Tree::insubtree() { pRoot->insubtree(pRoot); }int main(){ int n; cin>>n; Node a[n]; cin>>a[0].data ; Tree *tree=new Tree(a[0].data); //34, 76, 45, 18, 26, 54, 92, 65 for(int i=0;i>a[i+1].data; tree->Addnode(a[i+1].data); } tree->insubtree() ; //int count=0; cout |
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|
发帖时有格式化工具,把代码格式化好再发一遍吧,你这一堆东西看得我也快哭了... |
|
|
|
|