二叉查找树(c++)
二叉查找数的操作:
#include <iostream> using namespace std; typedef struct BitNode
{
int data;
struct BitNode *lChild,*rChild;
}BitNode; int main()
{
void InitTree(BitNode *&BitTree);
int PrintTree(BitNode *BitTree);
int SearchNode(BitNode *BitTree,int x);
int InsertNode(BitNode *&BitTree,int x);
int depthtree(BitNode *&BitTree);
BitNode *BitTree;
int dep=;
BitTree=new BitNode;
InitTree(BitTree);
PrintTree(BitTree);
SearchNode(BitTree,);
InsertNode(BitTree,);
InsertNode(BitTree,);
PrintTree(BitTree);
dep=depthtree(BitTree);
cout<<"二叉查找树的高度是:"<<dep<<endl;
return ;
} void InitTree(BitNode *&BitTree)
{
BitNode *pl,*pr;
BitTree->data=;
pl=new BitNode;
pl->data=;
pl->lChild=pl->rChild=NULL;
BitTree->lChild=pl;
pr=new BitNode;
pr->data=;
pr->lChild=pr->rChild=NULL;
BitTree->rChild=pr;
} int PrintTree(BitNode *BitTree)
{
if(BitTree)
{
cout<<BitTree->data<<endl;
PrintTree(BitTree->lChild);
PrintTree(BitTree->rChild);
}
return ;
}
int SearchNode(BitNode *BitTree,int x)
{
while(BitTree)
{
if(BitTree->data==x)
{
cout<<"find x="<<x<<endl;
return ;
}
if(x<BitTree->data)
BitTree=BitTree->lChild;
else
BitTree=BitTree->rChild;
}
cout<<"cannot find x="<<x<<endl;
return ;
}
int InsertNode(BitNode *&BitTree,int x)
{
if(BitTree==NULL)
{
BitNode *p;
p=new BitNode;
p->data=x;
p->lChild=p->rChild=NULL;
BitTree=p;
cout<<"insert successfully"<<endl;
return ;
}
else if(x<BitTree->data)
{
InsertNode(BitTree->lChild,x);
}
else
{
InsertNode(BitTree->rChild,x);
}
return ;
} int depthtree(BitNode *&BitTree)
{
int dr=,dl=;
if(BitTree==NULL)
return ;
dr=+depthtree(BitTree->rChild);
dl=+depthtree(BitTree->lChild);
return dr>=dl?dr:dl;
}
共勉。
二叉查找树(c++)的更多相关文章
- 数据结构:二叉查找树(C语言实现)
数据结构:二叉查找树(C语言实现) ►写在前面 关于二叉树的基础知识,请看我的一篇博客:二叉树的链式存储 说明: 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: 1.若其左子树不空,则左子树上 ...
- 数据结构笔记--二叉查找树概述以及java代码实现
一些概念: 二叉查找树的重要性质:对于树中的每一个节点X,它的左子树任一节点的值均小于X,右子树上任意节点的值均大于X. 二叉查找树是java的TreeSet和TreeMap类实现的基础. 由于树的递 ...
- codevs 1285 二叉查找树STL基本用法
C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...
- 平衡二叉查找树(AVL)的理解与实现
AVL树的介绍 平衡二叉树,又称AVL(Adelson-Velskii和Landis)树,是带有平衡条件的二叉查找树.这个平衡条件必须要容易保持,而且它必须保证树的深度是 O(log N).一棵AVL ...
- 二叉查找树 C++实现(含完整代码)
一般二叉树的查找是通过遍历整棵二叉树实现,效率较低.二叉查找树是一种特殊的二叉树,可以提高查找的效率.二叉查找树又称为二叉排序树或二叉搜索树. 二叉查找树的定义 二叉排序树(Binary Search ...
- 数据结构——二叉查找树、AVL树
二叉查找树:由于二叉查找树建树的过程即为插入的过程,所以其中序遍历一定为升序排列! 插入:直接插入,插入后一定为根节点 查找:直接查找 删除:叶子节点直接删除,有一个孩子的节点删除后将孩子节点接入到父 ...
- Java for LintCode 验证二叉查找树
给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二叉查找树. ...
- 数据结构和算法 – 9.二叉树和二叉查找树
9.1.树的定义 9.2.二叉树 人们把每个节点最多拥有不超过两个子节点的树定义为二叉树.由于限制子节点的数量为 2,人们可以为插入数据.删除数据.以及在二叉树中查找数据编写有效的程序了. 在 ...
- 二叉树-二叉查找树-AVL树-遍历
一.二叉树 定义:每个节点都不能有多于两个的儿子的树. 二叉树节点声明: struct treeNode { elementType element; treeNode * left; treeNod ...
- 二叉查找树的Java实现
为了克服对树结构编程的恐惧感,决心自己实现一遍二叉查找树,以便掌握关于树结构编程的一些技巧和方法.以下是基本思路: [1] 关于容器与封装.封装,是一种非常重要的系统设计思想:无论是面向过程的函数,还 ...
随机推荐
- <aop:aspectj-autoproxy proxy-target-class="false"/>导致出现404状态码
今天干活的时候,由于是一个web应用,想在每次发送请求和返回响应的时候记录日志,也就是代理Controller,想起了之前的spring AOP,于是按照之前的配置配置好了,可是发现每次前端发送请求都 ...
- Mineweep(扫雷)
题目描述: 每周一题之2 Mineweep(扫雷) Minesweeper (扫雷) PC/UVa IDs: 110102/10189, Popularity: A, Success rate: h ...
- C# DateTime.Compare时间大小对比
- 本地访问Vmware虚机Web网站
情况:公司是域环境,Vmware网络设置的是NAT连接模式,里外装的都是Windows,虚机网络IP地址是自动获取的. 查看: 1.虚机Ping本地的IP地址可以Ping通: 2.本地Ping虚机的I ...
- pm2 启动后台 node js
1,安装node js 参看:https://www.cnblogs.com/wf-l5201314/p/9229974.html 2,pm2安装(安装环境linux / os) 命令:npm ins ...
- Index Skip Scan in Oracle in 11g
http://viralpatel.net/blogs/oracle-index-skip-scan/ in 11g the same sql use index skip scan but in 1 ...
- python 循环类型
循环: while死循环: important time while 1== 1 print('ok') #当1==1条件成立时,会一直循环输出ok. 因为条件永远成立,所以是死循环 ...
- python csv.reader参数指定
- SSM-@Transactional 注释不生效
1.在applicationConext.xml 中配置事务注解驱动 <!-- 事务注解驱动 --> <tx:annotation-driven /> <!-- 配置事务 ...
- Huawei English Corner
Keywords Descriptions FWIW For what it's worth(不管结果怎样) ASAP As Soon As Possible(尽快) FYI For ...