/****
* BinarySortTree.
*
****/
#include "Global.h"
#include <iostream>
using namespace std;
//BinarySortTree
//insert operation
BinTree Insert_BinaryTree(BinTree bt, KeyType key)
{
if(bt == 0)
{
bt = new BitNode;
bt->elem = key;
bt->leftchild = 0;
bt->rightchild = 0;
return bt;
}
if(key < bt->elem)
bt->leftchild = Insert_BinaryTree(bt->leftchild, key);
else
bt->rightchild = Insert_BinaryTree(bt->rightchild,key);
return bt;
}
//Binary sort tree search algorithm
BitNode* Search_BinaryTree(BinTree bt, KeyType key)
{
if(bt == 0) return 0;
if(key == bt->elem) return bt;
if(key < bt->elem)
return Search_BinaryTree(bt->leftchild, key);
else
return Search_BinaryTree(bt->rightchild, key);
}
//another search algorithm
int Search_BinaryTree(BinTree bt, KeyType key, BitNode** p, BitNode** pf)
{
//pf is the pointer which point to the parent.
*p = bt;
*pf = 0;
while(*p != 0)
{
if(key == (*p)->elem) return 1;
if(key < (*p)->elem)
{
*pf = *p;
*p = (*p)->leftchild;
}
else
{
*pf = *p;
*p = (*p)->rightchild;
}
}
return 0;
}
//BinarySortTree
//Delete Operation
int Delete_BinaryTree(BinTree *bt, KeyType key)
{
BitNode* p = *bt;
BitNode* pf = 0; // pf is the parent of the p.
int findflag; //whether find or not.
if(*bt == 0) return 0; // tree is null.
findflag = Search_BinaryTree(*bt, key, &p, &pf);
if(findflag == 0) return 0; // no match.
//---------------------------------------------
//You need to delete a node that have no child.
if(p->leftchild == 0 && p->rightchild == 0)
{
if(pf == 0) // delete the root node.
{
delete bt;
bt = 0;
return 1;
}
if(p == pf->leftchild)
pf->leftchild = 0;
else
pf->rightchild = 0;
delete p;
return 1;
}
//---------------------------------------------
//---------------------------------------------
//You need to delete a node that have one child
if(p->leftchild == 0) // left child is null.
{
if(pf == 0) //delete the root node.
{
*bt = p->rightchild;
delete p;
return 1;
}
if(p == pf->leftchild)
pf->leftchild = p->rightchild;
else
pf->rightchild = p->rightchild;
delete p;
return 1;
}
if(p->rightchild == 0) // right child is null.
{
if(pf == 0) // delete the root node.
{
*bt = p->leftchild;
delete p;
return 1;
}
if(p == pf->leftchild)
pf->leftchild = p->leftchild;
else
pf->rightchild = p->leftchild;
delete p;
return 1;
}
//---------------------------------------------
//---------------------------------------------
//You need to delete a node that have two child
BitNode* prf = p;
BitNode* pr = p->rightchild;
while(pr->leftchild != 0)
{
prf = pr;
pr = pr->leftchild;
}
p->elem = pr->elem; //replace
if(prf == p)
prf->rightchild = pr->rightchild;
else
prf->leftchild = pr->rightchild;
delete pr;
return 1;
}
//test function
int main()
{
int a[10] = {12, 52, 65, 84, 63, 14, 68, 69, 99,77};
// initialization and creat the Binary Sort Tree.
BinTree bt = 0;
for(int i = 0; i < 10; i++)
{
bt = Insert_BinaryTree(bt, a[i]);
}
//search start.
cout << Search_BinaryTree(bt,14) << endl;
cout << Search_BinaryTree(bt,55) << endl;
//delete start.
cout << Delete_BinaryTree(&bt,14) << endl;
//search 14 again.
cout << Search_BinaryTree(bt,14) << endl;
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
查找算法代码C++——包括顺序、二分、BST、哈希
共70个文件
pdb:10个
dsw:5个
exe:5个
5星 · 超过95%的资源 需积分: 33 99 下载量 78 浏览量
2015-06-26
12:05:15
上传
评论 2
收藏 3.46MB RAR 举报
温馨提示
用C++写的最全的查找算法,顺序查找,二分查找,BST查找,哈希查找,可用于学习查找算法
资源推荐
资源详情
资源评论
收起资源包目录
查找算法总结.rar (70个子文件)
HashTable_SeparateChain
HashTable_SeparateChain.cpp 2KB
HashTable_SeparateChain.dsw 577B
Global.h 233B
HashTable_SeparateChain.plg 1KB
HashTable_SeparateChain.opt 53KB
HashTable_SeparateChain.ncb 33KB
HashTable_SeparateChain.dsp 4KB
Debug
HashTable_SeparateChain.ilk 749KB
vc60.idb 73KB
HashTable_SeparateChain.pch 1.91MB
HashTable_SeparateChain.pdb 1.02MB
vc60.pdb 108KB
HashTable_SeparateChain.obj 152KB
HashTable_SeparateChain.exe 500KB
BinarySearch
BinarySearch.plg 0B
BinarySearch.cpp 1KB
Global.h 219B
BinarySearch.dsw 555B
BinarySearch.ncb 33KB
BinarySearch.dsp 4KB
BinarySearch.opt 53KB
Debug
BinarySearch.obj 141KB
BinarySearch.pch 1.91MB
BinarySearch.pdb 1.02MB
vc60.idb 73KB
BinarySearch.exe 496KB
vc60.pdb 108KB
BinarySearch.ilk 746KB
HashTable
HashTable.dsw 549B
HashTable.plg 1KB
Global.h 140B
HashTable.opt 53KB
HashTable.cpp 1KB
HashTable.ncb 33KB
HashTable.dsp 4KB
Debug
HashTable.pch 183KB
HashTable.pdb 1.02MB
HashTable.obj 147KB
vc60.idb 73KB
HashTable.exe 500KB
vc60.pdb 108KB
HashTable.ilk 747KB
BinarySortTree
BinarySortTree.dsp 4KB
BinarySortTree.plg 0B
BinarySortTree.dsw 559B
BinarySortTree.cpp 3KB
Global.h 167B
BinarySortTree.opt 53KB
BinarySortTree.ncb 33KB
Debug
BinarySortTree.ilk 747KB
BinarySortTree.obj 148KB
vc60.idb 73KB
BinarySortTree.exe 500KB
BinarySortTree.pdb 1.02MB
vc60.pdb 108KB
BinarySortTree.pch 1.91MB
SequenceSearch
SequenceSearch.opt 53KB
Global.h 219B
SequenceSearch.ncb 33KB
SequenceSearch.cpp 1KB
SequenceSearch.dsw 559B
SequenceSearch.dsp 4KB
SequenceSearch.plg 1KB
Debug
SequenceSearch.ilk 746KB
SequenceSearch.pch 184KB
vc60.idb 73KB
SequenceSearch.exe 496KB
vc60.pdb 108KB
SequenceSearch.obj 141KB
SequenceSearch.pdb 1.02MB
共 70 条
- 1
资源评论
- ucliaohh2016-05-18不错的 帮助还挺大的
susandebug
- 粉丝: 280
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功