zoukankan      html  css  js  c++  java
  • 查找技术

     
    查找分类:
    • 静态查找:查找不成功,只返回一个不成功标志(不涉及插入删除)
    • 动态查找:查找不成功,将被查找的记录插入到查找集合中
     
    查找结构:
    • 线性表:适用于静态查找,主要采用技术:顺序查找折半查找
    • 树表适用于动态查找,主要采用技术二叉排序树
    • 散列表适用于静态查找和动态查找,主要采用技术散列
     
     
    查找算法性能:
    平均查找长度(Pi:查找第i个记录的概率;Ci:关键码比较次数)
    查获成功、查找失败两种情况
     

    线性表
    顺序查找
    顺序表:
    单链表:
     
    (有序)折半查找
    递归算法:
    非递归算法:
    1. publicint binarySearch(int[] r,int a){
    2.          low =0,   high = r.length -1;
    3.  
    4.         while(low <= high)
    5.         {
    6.             mid =(low + high)/2;
    7.             if(a < r[mid])    high = mid -1;
    8.             elseif(a > r[mid])    low = mid +1;
    9.             else                    return mid;
    10.         }
    11.         return-1;
    12.     }
     
    折半查找判定树
     


    树表
     
    线性表的插入删除需要O(n),只适合静态查找 >> 使用树表快速完成插入和查找(动态查找)
     
    二叉排序树
     
     
     
    • 插入与构造:
    1.     privateBiNode root = null;
    2.     BST(int[] r)
    3.     {
    4.         for(int i =0; i < r.length; i++)
    5.         {
    6.             BiNode s =newBiNode(r[i],null,null);
    7.             root = insertBST(root, s);
    8. //            insertBST1(s);
    9.         }
    10.     }
    11.  
    12.     //插入(递归)
    13.     publicBiNode insertBST(BiNode head,BiNode s)
    14.     {
    15.         if(head == null) { head = s; return head; }
    16.  
    17.         if(s.data < head.data)
    18.             head.left = insertBST(head.left, s);
    19.         else
    20.            head.right = insertBST(head.right,s);
    21.  
    22.         return  head;
    23.     }
    24.  
    25.     //插入(非递归:循环、用临时变量保存过程)
    26.     publicvoid insertBST1(BiNode s)
    27.     {
    28.         if(root == null) { root = s; return; }
    29.  
    30.         BiNode temp = root;//需要临时结点记录
    31.  
    32.         while(true)
    33.         {
    34.             if(s.data < temp.data)
    35.             {
    36.                 if(temp.left == null)
    37.                 {
    38.                     temp.left = s;
    39.                     return;
    40.                 }
    41.                 temp = temp.left;
    42.             }
    43.             else
    44.             {
    45.                 if(temp.right == null)
    46.                 {
    47.                     temp.right = s;
    48.                     return;
    49.                 }
    50.                 temp = temp.right;
    51.             }
    52.         }
    53.     }
     
    • 查找:
    1.     //查找
    2.     privateBiNode searchBST(BiNode head,int a)
    3.     {
    4.         if(head == null) return null;
    5.  
    6.         if(a < head.data)
    7.             return searchBST(head.left, a);
    8.         elseif(a > head.data)
    9.             return searchBST(head.right, a);
    10.         else
    11.             return head;
    12.     }
     
    • 删除:
    1. //   
    2.     publicvoid deleteLeftBST(BiNode f,BiNode p)
    3.     {
    4.         if(p.left == null && p.right == null)    //p为叶子
    5.         {
    6.             f.left = null;
    7.             p = null;
    8.         }
    9.         elseif(p.right == null)                  //p只有左子树
    10.         {
    11.             f.left = p.left;
    12.             p = null;
    13.         }
    14.         elseif(p.left == null)                  //p只有右子树
    15.         {
    16.             f.left = p.right;
    17.             p = null;
    18.         }
    19.         else                                       //p的左右子树均不空
    20.         {
    21.             BiNode par = p, s = par.right;          //用par s 去查找p的右子树的最左下结点
    22.             while(s.left != null)
    23.             {
    24.                 par = s;
    25.                 s = par.left;
    26.             }
    27.             p.data = s.data;                       //交换最左下结点s与p结点数据
    28.  
    29.                                                     //剪枝(删除s结点)
    30.             if(par == p)                          //处理特殊情况
    31.             {
    32.                 par.right = s.right;
    33.                 s = null;
    34.             }
    35.             else                                   //处理一般情况
    36.             {
    37.                 par.left = s.right;
    38.                 s = null;
    39.             }
    40.  
    41.         }
    42.     }
     
    性能分析:
    • 当二叉树排序树是平衡的(形态均衡),则有n个结点的二叉树的高度是 [ log2(n) ] + 1,其查找效率为O(log2(n)),近似于折半查找;
    • 当二叉树排序树不平衡(最坏为一课斜树),退化为顺序查找,查找效率为O(n
    • 二叉排序树查找性能一般在:O(log2(n))~ O(n
     

     
    平衡二叉树
    当二叉排序树初始化数组按序排列时,其性能退化为O(n) >> 应构造平衡二叉树
     
    平衡二叉树
    1、根节点的左子树和右子树深度最多相差1
    2、根节点的左子树和右子树也是平衡二叉树
     
     
    性能分析:
    • All operations on AVL trees are O(log2 n).
    • The AVL tree is also weight balanced. Sizes of left and right can’t vary by more than a factor of roughly two.
    • Longest path to a leaf can’t differ from other paths to leaves by more than O(log n).
     
     

    红黑树
    1. A node is either red or black.
    2. The root is black.
    3. All leaves (NIL) are black.
    4. Every red node must have two black child nodes.
    5. Every path from a given node to any of its descendant leaves contains the same number of black nodes.
     
    1.  节点是红色或黑色。
    2.  根是黑色。
    3.  所有叶子都是黑色(叶子是NIL节点)。
    4.  每个红色节点必须有两个黑色的子节点。(从每个叶子到根的所有路径上不能有两个连续的红色节点。)
    5.  从任一节点到其每个叶子的所有简单路径都包含相同数目的黑色节点
     
     
     
    性能分析
    Properties (4) and (5) guarantee that the difference in depth of any two nodes can’t vary by more than a factor of two.
    Many insert and remove operations can preserve this without having to do a rotation.
     
     
     
    AVL树与与红黑树比较
    • AvlTrees are slightly better balanced than RedBlackTrees. 
    • Both trees take O(log n) time overall for lookups, insertions, and deletions, but for insertion and deletion AvlTrees requires O(log n) rotations, while RedBlackTrees takes only O(1) rotations. Since rotations mean writing to memory, and writing to memory is expensive, RedBlackTrees are in practice faster to update than AvlTrees.
     
    • Red–black trees offer worst-case guarantees for insertion time, deletion time, and search time. 适合于:time-sensitive applications such as real-time applications; data structures which provide worst-case guarantees
    • AVL tree is more rigidly balanced than red–black trees, leading to slower insertion and removal but faster retrieval. 适合于:datastructures that may be built once and loaded without reconstruction, such as language dictionaries (or programdictionaries, such as the opcodes of an assembler or interpreter).
    参考:
    wikipedia
     

    B树(B-树)、B+树、B*树
    外存中的文件进行索引查找
     
    • B树(B-树):
     
     
     
    • B+树:
     
    • B*树:
     


    散列表
    散列函数
    1、直接定址法
    2、除留取余法
    3、数字分析法
    4、平方取中法
    5、折叠法
     
    处理冲突方法
    1、开放定址法(闭散列表)
    (1)线性探测法
     
    (2)二次探测法
     
    (3)随机探测法
     
    线性探测代码:
     
    2、拉链法 / 链地址法(开散列表)
     
    开散列表与闭散列表的比较
     


    综合比较
     
     
     
     
     

    参考资料:《数据结构(C++版)》王红梅



  • 相关阅读:
    图的邻接链表实现(c)
    图的邻接矩阵实现(c)
    好文
    第13章 红黑树
    函数 setjmp, longjmp, sigsetjmp, siglongjmp
    域名解析
    wget www.baidu.com执行流程分析
    信号处理篇
    第11章 散列表
    第10章,基本数据结构(栈,队列)
  • 原文地址:https://www.cnblogs.com/Doing-what-I-love/p/5535054.html
Copyright © 2011-2022 走看看