博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉排序树查找,插入和删除实现(C++)
阅读量:4049 次
发布时间:2019-05-25

本文共 2413 字,大约阅读时间需要 8 分钟。

本文介绍了二叉排序树查找,插入和删除C++实现过程。代码如下:

#include
using namespace std;typedef int TypeName;struct Node{
TypeName data; Node *lchild, *rchild;};typedef Node* Tree;void Creat_Tree(Tree &T);bool SearchTree(Tree T, int key, Tree f, Tree &p);bool InsertTree(Tree &T, int key);bool ShowTree(const Tree &T);bool DeleteBST(Tree &T, int key);bool Delete_Node(Tree &T);void main(){
Tree T = nullptr; cout << "Enter number(62 58 47 35 0 37 0 0 51 0 0 0 88 73 0 0 99 93 0 0 0): \n"; Creat_Tree(T); Tree p; if (SearchTree(T, 73, nullptr, p)) cout << p << ": " << p->data << endl; else cout << "There is no key!\n" << endl; Tree T1 = nullptr; int num[] = {
62, 58, 47, 35, 37, 51, 88, 73, 99, 93 }; for (int temp : num) InsertTree(T1, temp); if (InsertTree(T1, 95)) {
ShowTree(T1); cout << endl; } else cout << "Insert fail!" << endl; if (DeleteBST(T1, 62)) {
ShowTree(T1); cout << endl; } else cout << "Delete fail!" << endl;}void Creat_Tree(Tree &T){
TypeName temp; cin >> temp; if (temp == 0) {
T = nullptr; } else {
T = new Node; T->data = temp; Creat_Tree(T->lchild); Creat_Tree(T->rchild); }}bool SearchTree(Tree T, int key, Tree f, Tree &p){
if (!T) {
p = f; return false; } else {
if (T->data == key) {
p = T; return true; } else if (T->data > key) SearchTree(T->lchild, key, T, p); else SearchTree(T->rchild, key, T, p); }}bool InsertTree(Tree &T, int key){
Tree p, s; if (!SearchTree(T, key, nullptr, p)) {
s = new Node; s->data = key; s->lchild = nullptr; s->rchild = nullptr; if (!p) T = s; else if (p->data > key) p->lchild = s; else p->rchild = s; return true; } else return false;}bool ShowTree(const Tree &T){
if (T == nullptr) return false; else {
cout << T->data << " "; ShowTree(T->lchild); ShowTree(T->rchild); } return true;}bool DeleteBST(Tree &T, int key){
if (!T) return false; else {
if (T->data == key) Delete_Node(T); else if (T->data > key) DeleteBST(T->lchild, key); else DeleteBST(T->rchild, key); }}bool Delete_Node(Tree &p){
Tree q, s; if (p->lchild == nullptr) {
p = p->rchild; delete p; } else if (p->rchild == nullptr) {
p = p->lchild; delete p; } else {
q = p; s = p->lchild; while (s->rchild) {
q = s; s = s->rchild; } p->data = s->data; if (q != p) q->rchild = s->lchild; else q->lchild = s->lchild; delete s; } return true;}

转载地址:http://dpyci.baihongyu.com/

你可能感兴趣的文章
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>