//建立搜索树
struct bintree
{
int data=0;
bintree* pleft=nullptr;
bintree* pright=nullptr;
};
bintree* insert(bintree* proot, int num)
{
if (proot == nullptr)
{
bintree* pnew = new bintree;
pnew->data = num;
proot = pnew;
}
else if (num < proot->data)
{
proot->pleft = insert(proot->pleft, num);
}
else if (num > proot->data)
{
proot->pright = insert(proot->pright, num);
}
return proot;
}
void show(bintree* root)//显示
{
if (root == nullptr)
{
return;
}
else
{
show(root->pleft);
cout << root->data << endl;
show(root->pright);
}
}
//查找
bool find(bintree *root, int key) {
if (n == NULL) return false;
else if (key == n->val) return true;
else if (key > n->val) return find(n->right, key);
else return find(n->left, key);
}
//广度优先遍历
void guangdu(bintree* root)
{
if (root == nullptr)return;
queue<bintree*>myq;
myq.push(root);
while (!myq.empty())
{
bintree* temp = myq.front();
cout << temp->data << endl;
myq.pop();
if (temp->pleft != nullptr)
{
myq.push(temp->pleft);
}
if (temp->pright != nullptr)
{
myq.push(temp->pright);
}
}
}
c++搜索树和BFS
最新推荐文章于 2024-02-23 21:16:05 发布