动态查找之二叉树查找 c++实现
算法思想
二叉搜索树(又称二叉查找树或二叉排序树)BST树
二叉查找树
二叉查找树,也称二叉搜索树,或二叉排序树。其定义也比较简单,要么是一颗空树,要么就是具有如下性质的二叉树:
(1)若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2) 若任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3) 任意节点的左、右子树也分别为二叉查找树;
(4) 没有键值相等的节点。
二叉查找树的性质总结:
a.二叉查找树还有一个性质,即对二叉查找树进行中序遍历,即可得到有序的数列。
b.二叉查找树的查询复杂度,和二分查找一样,插入和查找的时间复杂度均为 O(logn) ,但是在最坏的情况下仍然会有 O(n) 的时间复杂度。原因在于插入和删除元素的时候,树没有保持平衡。
具体实现:
/****
* BinarySortTree.
*
****/
#include"stdafx.h"
#include <iostream>
#include<queue>
using namespace std;
typedef struct node
{
int elem;
struct node* leftchild;
struct node* rightchild;
}BitNode,*BinTree;
//insert binary tree function
BinTree Insert_BinaryTree(BinTree &bt,int key)
{
if (bt == )
{
bt = new BitNode;
bt->elem = key;
bt->leftchild = ;
bt->rightchild = ;
return bt;
}
if (key < bt->elem)
{
bt->leftchild = Insert_BinaryTree(bt->leftchild,key);
}
else
{
bt->rightchild = Insert_BinaryTree(bt->rightchild, key);
}
return bt;
}
//for one search binary tree function
int Search_BinaryTree(BinTree &bt,int key)
{
if (bt == ) return ;
if (bt->elem == key) return ; if (key < bt->elem)
{
return Search_BinaryTree(bt->leftchild,key);
}
else
{
return Search_BinaryTree(bt->rightchild, key);
}
}
// for another one search binary tree function
int Search_BinaryTree(BinTree &bt, int key, BitNode ** p, BitNode** pf)
{
*p = bt;
*pf = ;
while (*p != )
{
if ((*p)->elem == key)
return ;
if ((*p)->elem > key)
{
*pf =*p;
*p = (*p)->leftchild;
}
else
{
*pf = *p;
*p = (*p)->rightchild;
}
}
return ;
}
//delete binary tree function
int Delete_BinaryTree(BinTree *bt,int key)
{
BitNode *p=*bt;
BitNode *pf=;
int findflag;
if (bt == ) return ;
findflag = Search_BinaryTree(*bt,key,&p,&pf);
if (findflag == ) return ;
//删除的节点是叶子节点
if (p->leftchild == && p->rightchild == )
{
if (pf == )
{
delete bt;
bt = ;
return ;
}
if (p == pf->leftchild)
{
pf->leftchild = ;
delete p;
p = ;
return ;
}
else
{
pf->rightchild = ;
delete p;
p = ;
return ;
}
}
//删除的节点只有一个子节点
if (p->leftchild == )
{
if (pf = )
{
*bt = p->rightchild;
delete p;
return ;
}
if(p==pf->leftchild)
{
pf->leftchild = p->rightchild;
delete p;
return ;
}
else
{
pf->rightchild = p->rightchild;
delete p;
return ;
}
} if (p->rightchild == )
{
if (p == pf->leftchild)
{
pf->leftchild = p->leftchild;
delete p;
return ;
}
if (p == pf->rightchild)
{
pf->rightchild = p->leftchild;
delete p;
return ;
}
}
//3.删除的节点含有两个子节点
BitNode * prf = p;
BitNode * pr = p->rightchild;
while (pr->leftchild != )
{
prf = pr;
pr = pr->leftchild;
}
if(prf == p)
{
p->elem = pr->elem;
prf->rightchild = pr->rightchild;
}
else
{
p->elem = pr->elem;
prf->leftchild = pr->rightchild;
}
delete pr;
return ; } //print binary tree function
void printTree(BitNode * bt)
{
queue<BitNode*> q;
q.push(bt);
while (!q.empty())
{
BitNode* p = q.front(); q.pop();
if (p)
{
cout << p->elem << "->";
q.push(p->leftchild);
q.push(p->rightchild);
}
}
cout << endl;
}
//test function
int main()
{
int a[] = { , , , , , , , , , }; // initialization and creat the Binary Sort Tree.
BinTree bt = ;
for (int i = ; i < ; i++)
{
bt = Insert_BinaryTree(bt, a[i]);
}
printTree(bt);
//search start.
cout << Search_BinaryTree(bt, ) << endl;
cout << Search_BinaryTree(bt, ) << endl; //delete start.
cout << Delete_BinaryTree(&bt, ) << endl; //search 14 again.
cout << Search_BinaryTree(bt, ) << endl;
printTree(bt);
system("pause");
return ;
}

动态查找之二叉树查找 c++实现的更多相关文章
- 查找算法(顺序查找、二分法查找、二叉树查找、hash查找)
查找功能是数据处理的一个基本功能.数据查找并不复杂,但是如何实现数据又快又好地查找呢?前人在实践中积累的一些方法,值得我们好好学些一下.我们假定查找的数据唯一存在,数组中没有重复的数据存在. (1)顺 ...
- 深入浅出数据结构C语言版(12)——从二分查找到二叉树
在很多有关数据结构和算法的书籍或文章中,作者往往是介绍完了什么是树后就直入主题的谈什么是二叉树balabala的.但我今天决定不按这个套路来.我个人觉得,一个东西或者说一种技术存在总该有一定的道理,不 ...
- 查找->静态查找表->次优查找(静态树表)
文字描算 之前分析顺序查找和折半查找的算法性能都是在“等概率”的前提下进行的,但是如果有序表中各记录的查找概率不等呢?换句话说,概率不等的情况下,描述查找过程的判定树为何类二叉树,其查找性能最佳? 如 ...
- 查找->静态查找表->折半查找(有序表)
文字描述 以有序表表示静态查找表时,可用折半查找算法查找指定元素. 折半查找过程是以处于区间中间位置记录的关键字和给定值比较,若相等,则查找成功,若不等,则缩小范围,直至新的区间中间位置记录的关键字等 ...
- 各种查找算法的选用分析(顺序查找、二分查找、二叉平衡树、B树、红黑树、B+树)
目录 顺序查找 二分查找 二叉平衡树 B树 红黑树 B+树 参考文档 顺序查找 给你一组数,最自然的效率最低的查找算法是顺序查找--从头到尾挨个挨个遍历查找,它的时间复杂度为O(n). 二分查找 而另 ...
- 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找
今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...
- jvascript 顺序查找和二分查找法
第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...
- DNS 正向查找与反向查找
原创地址:http://www.cnblogs.com/jfzhu/p/3996323.html 转载请注明出处 所谓正向查找,就是说在这个区域里的记录可以依据名称来查找对应的IP地址.反向查找就是在 ...
- Java中常用的查找算法——顺序查找和二分查找
Java中常用的查找算法——顺序查找和二分查找 神话丿小王子的博客 一.顺序查找: a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数据最后一位 ...
随机推荐
- Python如何去实际提高工作的效率?也许这个会有用!
4月初,班主任的某次周会议上,华华关切的问了一下:最近班主任们有什么难题吗?就是花费了你们大部分时间的工作!我们Python天团可以帮你们解决问题. 班主任大主管星星说:有.目前有一个大难题.我们每天 ...
- RxJS——主题(Subject)
主题(Subjects) 什么是主题?RxJS 主题就是一个特性类型的 Observable 对象,它允许值多路广播给观察者(Observers).当一个简单的 Observable 是单播的(每个订 ...
- pandas 之 数据合并
import numpy as np import pandas as pd Data contained in pandas objects can be combined together in ...
- Centos6.5基于GPT格式磁盘分区
1.查看分区 fdisk -l 2.设置分区类型未gpt格式. parted -s /dev/sdb mklabel gpt 3.基于ext3文件系统类型格式化. mkfs.ext3 /dev/sdb ...
- 详解Apache服务与高级配置,(主配置文件每行都有描述)
HTTP服务---> http://httpd.apache.org/(官方网站) httpd service :纯粹的web服务器,同时开源(不是GPL). 特性:1.在进程特性上通常是事先 ...
- (五)Kubernetes Pod状态和生命周期管理
什么是Pod Pod是kubernetes中你可以创建和部署的最小也是最简的单位.Pod代表着集群中运行的进程. Pod中封装着应用的容器(有的情况下是好几个容器),存储.独立的网络IP,管理容器如何 ...
- 微信之通过AppID和AppSecret获取access_token
最近在搞微信公众平台这方面的东西,,但实际使用的时候发现和access_token有关的接口都无法正常调用,于是debug了下,发现获取到了AppID和AppSecret,在最后请求access_to ...
- python+ddt+unittest+excel+request实现接口自动化
接口自动化测试流程:需求分析-用例设计--脚本开发--测试执行--结果分析1.获取接口文档,根据文档获取请求方式,传输协议,请求参数,响应参数,判断测试是否通过设计用例2.脚本开发:使用request ...
- template_DefaultType
#include <iostream> using namespace std; template <typename T1,typename T2=int> class Te ...
- 数据分析 - Matplotlib
简介 Matplotlib是一个强大的Python绘图和数据可视化的工具包.数据可视化也是我们数据分析的最重要的工作之一,可以帮助我们完成很多操作,例如:找出异常值.必要的一些数据转换等.完成数据分析 ...