Date:2019-06-25 14:40:32

基本操作

  • 注意:数据量较大时,插入建树的时间复杂度会很高,慎用!
 //查找
void Search(node *root, int x)
{
if(root == NULL)
{
printf("search failed\n");
return;
}
if(x == root->data)
printf("%d\n", root->data);
else if(x < root->data)
Search(root->lchild, x);
else if(x > root->data)
Search(root->data)
Search(root->rchild, x);
} //插入
void Insert(node *root, int x)
{
if(root == NULL)
{
root = newNode(x); //新建结点
return;
}
if(x == root->data)
return;
else if(x > root->data)
Insert(root->rchild, x);
else if(x < root->data)
Insert(root->lchild, x);
} //建立
node* Create(int data[], int n)
{
node *root = NULL;
for(int i=; i<n; i++)
Insert(root, data[i]); return root;
} //删除 //寻找前驱
node* FindMax(node *root)
{
while(root->rchild != NULL)
root = root->rchild;
return root;
} //寻找后继
node* FindMin(node *root)
{
while(root->lchild != NULL)
root = root->lchild;
return root;
} //删除结点root
void Delete(node *&root, int x)
{
if(root == NULL)
return;
if(root->data == x)
{
if(root->lchild==NULL & root->rchild==NULL) //叶子结点直接删除即可
root = NULL;
else if(root->lchild != NULL)
{
node *pre = FindMax(root->lchild); //寻找左子树的最大值(最右结点)
root->data = pre->data; //与被删除结点替换
Delete(root->lchild, pre->data); //删除替换后的结点
}
else if(root->rchild != NULL)
{
node *pre = FindMin(root->rchild); //寻找右子树的最小值(最左结点)
root->data = pre->data;
Delete(root->rchild, pre->data);
}
}
else if(x < root->data)
Delete(root->lchild, x);
else if(x > root->data)
Delete(root->rchild, x);
}

删除优化

  • 删除操作中,找到替换结点后,该结点就是接下来需要删除的结点,直接删除即可
  • 目前PAT考试中还没有考察过删除结点相关的算法
 #include<stdio.h>
const int M = ;
const int data[M] = {,,,,,,,,,}; struct node
{
int data;
node *lchild, *rchild;
}; node* FindMax(node *root)
{
while(root->rchild->rchild)
root = root->rchild;
return root;
} node *FindMin(node *root)
{
while(root->lchild->lchild)
root = root->lchild;
return root;
} void BST(node *&root, int x)
{
if(!root)
{
root = new node;
root->data = x;
root->lchild = NULL;
root->rchild = NULL;
return;
}
if(x == root->data)
{
if(!root->lchild && !root->rchild)
root = NULL;
else if(root->lchild)
{
node *pre;
if(root->lchild->rchild == NULL)
{
pre = root->lchild;
root->lchild = pre->lchild;
}
else
{
node *fa = FindMax(root->lchild);
pre = fa->rchild;
fa->rchild = pre->lchild;
}
root->data = pre->data;
delete(pre);
}
else if(root->rchild)
{
node *pre;
if(root->rchild->lchild == NULL)
{
pre = root->rchild;
root->rchild = pre->rchild;
}
else
{
node *fa = FindMin(root->rchild);
pre = fa->rchild;
fa->rchild = pre->lchild;
}
root->data = pre->data;
delete(pre);
}
}
else if(x < root->data)
BST(root->lchild, x);
else if(x > root->data)
BST(root->rchild, x);
} void Traverse(node *root)
{
if(root == NULL)
return;
Traverse(root->lchild);
printf("%d ", root->data);
Traverse(root->rchild);
} node* Create()
{
node *root = NULL;
for(int i=; i<M; i++)
BST(root, data[i]); return root;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE node *root = Create();
Traverse(root);
printf("\n");
BST(root, );
Traverse(root); return ;
}

二叉查找树(Binary Search Tree)的更多相关文章

  1. 二叉查找树(binary search tree)详解

    二叉查找树(Binary Search Tree),也称二叉排序树(binary sorted tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树上所有结点的值均小于 ...

  2. 算法与数据结构基础 - 二叉查找树(Binary Search Tree)

    二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...

  3. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  4. 【数据结构与算法Python版学习笔记】树——二叉查找树 Binary Search Tree

    二叉搜索树,它是映射的另一种实现 映射抽象数据类型前面两种实现,它们分别是列表二分搜索和散列表. 操作 Map()新建一个空的映射. put(key, val)往映射中加入一个新的键-值对.如果键已经 ...

  5. 数据结构之Binary Search Tree (Java)

    二叉查找树简介 二叉查找树(Binary Search Tree), 也成二叉搜索树.有序二叉树(ordered binary tree).排序二叉树(sorted binary tree), 是指一 ...

  6. 二叉搜索树(Binary Search Tree)(Java实现)

    @ 目录 1.二叉搜索树 1.1. 基本概念 1.2.树的节点(BinaryNode) 1.3.构造器和成员变量 1.3.公共方法(public method) 1.4.比较函数 1.5.contai ...

  7. 【二叉查找树】03验证是否为二叉查找树【Validate Binary Search Tree】

    本质上是递归遍历左右后在与根节点做判断,本质上是后序遍历 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  8. 《数据结构与算法分析——C语言描述》ADT实现(NO.03) : 二叉搜索树/二叉查找树(Binary Search Tree)

    二叉搜索树(Binary Search Tree),又名二叉查找树.二叉排序树,是一种简单的二叉树.它的特点是每一个结点的左(右)子树各结点的元素一定小于(大于)该结点的元素.将该树用于查找时,由于二 ...

  9. 【LeetCode】Validate Binary Search Tree 二叉查找树的推断

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). 知识点:BST的特点: 1.一个节点的左子树 ...

  10. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

随机推荐

  1. 在64位的ubuntu 14.04 上开展32位Qt 程序开发环境配置(pro文件中增加 QMAKE_CXXFLAGS += -m32 命令)

    为了能中一个系统上开发64或32位C++程序,费了些周折,现在终于能够开始干过了.在此记录此时针对Q5.4版本的32位开发环境配置过程. 1. 下载Qt 5.4 的32位版本,进行安装,安装过程中会发 ...

  2. centos7 安装8188eu驱动小记

    最小化安装把lsusb和lspci装上 使用lsusb 和lspci的命令, centos上的安装命令: yum -y install usbutils yum -y install pciutils ...

  3. POJ 2080:Calendar

    Calendar Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12546   Accepted: 4547 Descrip ...

  4. edittext 底线颜色

    <style name="Custom.Widget.EditView" parent="Widget.AppCompat.EditText" > ...

  5. 最长回文子串问题 O(n)算法 manacher URAL1297 HDU3068

    先来看一道简单的题,ural1297 给定一个1000长度的字符串,求最长回文子串. 看起来很Naive,乱搞一下,O(n^2)都可以解决. 再来看这个题 HDU3068 120个110000长度的字 ...

  6. 15_传智播客iOS视频教程_OC语言完全兼容C语言

    OC支持C语言所有的运算符并且效果是一样的.C语言中所有的运算符OC都支持.这些所有的运算符OC当中全部都支持. 包括C语言的结构体.枚举全部都可以写在OC当中,没有任何问题,并且效果是一样的. 比如 ...

  7. ASP.NET给前端动态添加修改 CSS样式JS 标题 关键字(转载)

    原文地址:http://www.cnblogs.com/xbhp/p/6392225.html 有很多网站读者能换自己喜欢的样式,还有一些网站想多站点共享后端代码而只动前段样式,可以采用动态替换CSS ...

  8. 深入浅出Android makefile(2)--LOCAL_PATH(转载)

    转自:http://nfer-zhuang.iteye.com/blog/1752387 一.说明 上文我们对acp的Android.mk文件做了一个大致的描述,使得大家对Android.mk文件有了 ...

  9. Windows 和 Linux 上Redis的安装守护进程配置

    # Windows 和 Linux 上Redis的安装守护进程配置 Redis 简介 ​ Redis是目前最常用的非关系型数据库(NOSql)之一,常以Key-Value的形式存储.Redis读写速度 ...

  10. Js 使用小技巧总结(1)

    1.Js 的时间控制,小于初始时间,大于截止时间 <script type="text/javascript">        window.onload = Game ...