二叉查找数的操作:

 #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. innodb的读写参数优化

    (1)    读取参数,global buffer pool以及 local buffer Innodb_buffer_pool_size,理论上越大越好,建议服务器50%~80%,实际为数据大小80 ...

  2. gradle 使用本地maven 仓库 和 提交代码到maven

    /* * This build file was generated by the Gradle 'init' task. * * This generated file contains a sam ...

  3. [Leetcode]013. Roman to Integer

    public class Solution { public int romanToInt(String s) { if(s == null || s.length() == 0) return 0; ...

  4. 洛谷1541(多维dp)

    走格子拿分数,直接弄dp[i]是到了第i格的最大得分可以发现是假的. 于是此题设f[i][j][k][t]代表四种步伐各用了几次可以得到的最大得分,到达的点可以直接算出来,就好转移了. const i ...

  5. http文件上传/下载

    package unit; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputSt ...

  6. GM TECH2 Scanner Clone

    Professional Diagnostic Tools gm tech 2 scanner china with multi-languages, TIS2000 Programming CD, ...

  7. table size script :

    I think Jonathan Lewis has explained the algorithm before, but it's alsosomething that we have inves ...

  8. component: resolve => require(['../pages/home.vue'], resolve)

    component: resolve => require(['../pages/home.vue'], resolve) vue 路由的懒加载 import Vue from 'vue' im ...

  9. 查看Oracle当前连接数

    SQL> select count(*) from v$session #当前的连接数 SQL> Select count(*) from v$session where status=' ...

  10. SQL Server Reporting Service(SSRS) 第四篇 SSRS 常见问题总结

    1. 如何让表头在每页显示(译) A. 打开高级模式:  在分组栏中点击Column Goups右侧的箭头选择高级模式; B. 找到第一个Static组 在Row Groups区域中(注意不是Colu ...