二叉查找数的操作:

 #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++)的更多相关文章

  1. 数据结构:二叉查找树(C语言实现)

    数据结构:二叉查找树(C语言实现) ►写在前面 关于二叉树的基础知识,请看我的一篇博客:二叉树的链式存储 说明: 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: 1.若其左子树不空,则左子树上 ...

  2. 数据结构笔记--二叉查找树概述以及java代码实现

    一些概念: 二叉查找树的重要性质:对于树中的每一个节点X,它的左子树任一节点的值均小于X,右子树上任意节点的值均大于X. 二叉查找树是java的TreeSet和TreeMap类实现的基础. 由于树的递 ...

  3. codevs 1285 二叉查找树STL基本用法

    C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...

  4. 平衡二叉查找树(AVL)的理解与实现

    AVL树的介绍 平衡二叉树,又称AVL(Adelson-Velskii和Landis)树,是带有平衡条件的二叉查找树.这个平衡条件必须要容易保持,而且它必须保证树的深度是 O(log N).一棵AVL ...

  5. 二叉查找树 C++实现(含完整代码)

    一般二叉树的查找是通过遍历整棵二叉树实现,效率较低.二叉查找树是一种特殊的二叉树,可以提高查找的效率.二叉查找树又称为二叉排序树或二叉搜索树. 二叉查找树的定义 二叉排序树(Binary Search ...

  6. 数据结构——二叉查找树、AVL树

    二叉查找树:由于二叉查找树建树的过程即为插入的过程,所以其中序遍历一定为升序排列! 插入:直接插入,插入后一定为根节点 查找:直接查找 删除:叶子节点直接删除,有一个孩子的节点删除后将孩子节点接入到父 ...

  7. Java for LintCode 验证二叉查找树

    给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值.    节点的右子树中的值要严格大于该节点的值.    左右子树也必须是二叉查找树. ...

  8. 数据结构和算法 – 9.二叉树和二叉查找树

      9.1.树的定义   9.2.二叉树 人们把每个节点最多拥有不超过两个子节点的树定义为二叉树.由于限制子节点的数量为 2,人们可以为插入数据.删除数据.以及在二叉树中查找数据编写有效的程序了. 在 ...

  9. 二叉树-二叉查找树-AVL树-遍历

    一.二叉树 定义:每个节点都不能有多于两个的儿子的树. 二叉树节点声明: struct treeNode { elementType element; treeNode * left; treeNod ...

  10. 二叉查找树的Java实现

    为了克服对树结构编程的恐惧感,决心自己实现一遍二叉查找树,以便掌握关于树结构编程的一些技巧和方法.以下是基本思路: [1] 关于容器与封装.封装,是一种非常重要的系统设计思想:无论是面向过程的函数,还 ...

随机推荐

  1. 如何从GAC中复制DLL文件

    运行执行:c:\Windows\assembly\gac_msil

  2. HDU-Digital Roots(思维+大数字符串模拟)

    The digital root of a positive integer is found by summing the digits of the integer. If the resulti ...

  3. python 函数调用

    ##########定义函数######### 如果不主动调用函数,函数是不会执行的 def say_hello():     print 'hello1'     print 'hello2'    ...

  4. oracle service name connect

    oracle service name connect dest_ip=100.100.100.100 dest_port= dest_dbname=server_name connect=" ...

  5. esper(4-2)-Category Context

    语法: create context context_name group [by] group_expression as category_label [, group [by] group_ex ...

  6. vue中this.$router.push() 传参

    1  params 传参 注意⚠️:patams传参 ,路径不能使用path 只能使用name,不然获取不到传的数据 this.$router.push({name: 'dispatch', para ...

  7. 2.2 Rust 数据类型

    2.2 数据类型 let guess: u32 = "42".parse().expect("Not a number!"); Rust has four pr ...

  8. Python Pandas -- Panel

    Pandas 中一维 series, 二维DataFrame, 三维Panel class pandas.Panel(data=None, items=None, major_axis=None, m ...

  9. python面向对象, 单例模式

    目录 单利模式 实现单利模式的方法 使用模块 使用__new__ 为了使类只能出现一个实例,我们可以使用 new 来控制实例的创建过程,代码如下: 使用装饰器 使用 metaclass 补充:元类(m ...

  10. Microsoft使用技巧

    1.拍摄屏幕内容的截图 按 Win + Shift + S 以打开截图栏,然后将光标拖动到要捕获的区域. 截图区域将保存到剪贴板. 2.使用键盘添加表情符号 随心随处表达自我. 按 Ctrl + Sh ...