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

基本操作

  • 注意:数据量较大时,插入建树的时间复杂度会很高,慎用!
  1. //查找
  2. void Search(node *root, int x)
  3. {
  4. if(root == NULL)
  5. {
  6. printf("search failed\n");
  7. return;
  8. }
  9. if(x == root->data)
  10. printf("%d\n", root->data);
  11. else if(x < root->data)
  12. Search(root->lchild, x);
  13. else if(x > root->data)
  14. Search(root->data)
  15. Search(root->rchild, x);
  16. }
  17.  
  18. //插入
  19. void Insert(node *root, int x)
  20. {
  21. if(root == NULL)
  22. {
  23. root = newNode(x); //新建结点
  24. return;
  25. }
  26. if(x == root->data)
  27. return;
  28. else if(x > root->data)
  29. Insert(root->rchild, x);
  30. else if(x < root->data)
  31. Insert(root->lchild, x);
  32. }
  33.  
  34. //建立
  35. node* Create(int data[], int n)
  36. {
  37. node *root = NULL;
  38. for(int i=; i<n; i++)
  39. Insert(root, data[i]);
  40.  
  41. return root;
  42. }
  43.  
  44. //删除
  45.  
  46. //寻找前驱
  47. node* FindMax(node *root)
  48. {
  49. while(root->rchild != NULL)
  50. root = root->rchild;
  51. return root;
  52. }
  53.  
  54. //寻找后继
  55. node* FindMin(node *root)
  56. {
  57. while(root->lchild != NULL)
  58. root = root->lchild;
  59. return root;
  60. }
  61.  
  62. //删除结点root
  63. void Delete(node *&root, int x)
  64. {
  65. if(root == NULL)
  66. return;
  67. if(root->data == x)
  68. {
  69. if(root->lchild==NULL & root->rchild==NULL) //叶子结点直接删除即可
  70. root = NULL;
  71. else if(root->lchild != NULL)
  72. {
  73. node *pre = FindMax(root->lchild); //寻找左子树的最大值(最右结点)
  74. root->data = pre->data; //与被删除结点替换
  75. Delete(root->lchild, pre->data); //删除替换后的结点
  76. }
  77. else if(root->rchild != NULL)
  78. {
  79. node *pre = FindMin(root->rchild); //寻找右子树的最小值(最左结点)
  80. root->data = pre->data;
  81. Delete(root->rchild, pre->data);
  82. }
  83. }
  84. else if(x < root->data)
  85. Delete(root->lchild, x);
  86. else if(x > root->data)
  87. Delete(root->rchild, x);
  88. }

删除优化

  • 删除操作中,找到替换结点后,该结点就是接下来需要删除的结点,直接删除即可
  • 目前PAT考试中还没有考察过删除结点相关的算法
  1. #include<stdio.h>
  2. const int M = ;
  3. const int data[M] = {,,,,,,,,,};
  4.  
  5. struct node
  6. {
  7. int data;
  8. node *lchild, *rchild;
  9. };
  10.  
  11. node* FindMax(node *root)
  12. {
  13. while(root->rchild->rchild)
  14. root = root->rchild;
  15. return root;
  16. }
  17.  
  18. node *FindMin(node *root)
  19. {
  20. while(root->lchild->lchild)
  21. root = root->lchild;
  22. return root;
  23. }
  24.  
  25. void BST(node *&root, int x)
  26. {
  27. if(!root)
  28. {
  29. root = new node;
  30. root->data = x;
  31. root->lchild = NULL;
  32. root->rchild = NULL;
  33. return;
  34. }
  35. if(x == root->data)
  36. {
  37. if(!root->lchild && !root->rchild)
  38. root = NULL;
  39. else if(root->lchild)
  40. {
  41. node *pre;
  42. if(root->lchild->rchild == NULL)
  43. {
  44. pre = root->lchild;
  45. root->lchild = pre->lchild;
  46. }
  47. else
  48. {
  49. node *fa = FindMax(root->lchild);
  50. pre = fa->rchild;
  51. fa->rchild = pre->lchild;
  52. }
  53. root->data = pre->data;
  54. delete(pre);
  55. }
  56. else if(root->rchild)
  57. {
  58. node *pre;
  59. if(root->rchild->lchild == NULL)
  60. {
  61. pre = root->rchild;
  62. root->rchild = pre->rchild;
  63. }
  64. else
  65. {
  66. node *fa = FindMin(root->rchild);
  67. pre = fa->rchild;
  68. fa->rchild = pre->lchild;
  69. }
  70. root->data = pre->data;
  71. delete(pre);
  72. }
  73. }
  74. else if(x < root->data)
  75. BST(root->lchild, x);
  76. else if(x > root->data)
  77. BST(root->rchild, x);
  78. }
  79.  
  80. void Traverse(node *root)
  81. {
  82. if(root == NULL)
  83. return;
  84. Traverse(root->lchild);
  85. printf("%d ", root->data);
  86. Traverse(root->rchild);
  87. }
  88.  
  89. node* Create()
  90. {
  91. node *root = NULL;
  92. for(int i=; i<M; i++)
  93. BST(root, data[i]);
  94.  
  95. return root;
  96. }
  97.  
  98. int main()
  99. {
  100. #ifdef ONLINE_JUDGE
  101. #else
  102. freopen("Test.txt", "r", stdin);
  103. #endif // ONLINE_JUDGE
  104.  
  105. node *root = Create();
  106. Traverse(root);
  107. printf("\n");
  108. BST(root, );
  109. Traverse(root);
  110.  
  111. return ;
  112. }

二叉查找树(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. Go 语言的下一个大版本:Go 2.0 被安排上了(全面兼容1.X,改进错误处理和泛型这两大主题)

    今年 8 月 Go 开发团队公布了 Go 2.0 的设计草案,包括错误处理和泛型这两大主题.现在备受瞩目的 Go 2.0 又有了新动向 —— 昨日 Go 开发团队在其官方博客表示,Go 2 已经被安排 ...

  2. 8-12 canvas专题-阶段练习一(上)

    8-12 canvas专题-阶段练习一(上) <!DOCTYPE html> <html lang="zh-cn"> <head> <me ...

  3. 2.6 wpf标记扩展

    1.什么是标记扩展?为什么要有标记扩展? 标记扩展是扩展xmal的表达能力 为了克服现存的类型转换机制存在的 常用的标记扩展有如下: x:Array 代表一个.net数组,它的子元素都是数组元素.它必 ...

  4. luogu 3388 【模板】割点(割顶)

    点双. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> ...

  5. python调用java程序--jpype

    官方网站:http://jpype.sourceforge.net/ 官方使用文档:http://jpype.sourceforge.net/doc/user-guide/userguide.html ...

  6. Mysql建表出现1005错误

    转自:http://blog.sina.com.cn/s/blog_757807f30100vz23.html 当在创建一个表时提示1005错误无法创建时,注意检查一下几点: 1.当此表有外键时,检查 ...

  7. angularJs模版注入的两种方式

    一,声名式注入 1:app.js: var myApp = angular.module("myApp",["ngRoute"]); 2:controller. ...

  8. c++ class does not name a type (转载)

    转载:http://blog.csdn.net/typename/article/details/7173550 declare class does not name a type 出现这个编译错误 ...

  9. 对象的属性类型 和 VUE的数据双向绑定原理

    如[[Configurable]] 被两对儿中括号 括起来的表示 不可直接访问他们 修改属性类型:使用Object.defineProperty()  //IE9+  和标准浏览器  支持 查看属性的 ...

  10. Parameterized testing with any Python test framework

    1. 在进行单元测试时,很多时候需要进行参数化 尝试过使用 from nose_parameterized import parameterized 但在使用过程中会报错,后来将上面的内容改为了下面的 ...