zoukankan      html  css  js  c++  java
  • 二分查找,二叉查找树

    二分查找

    二分查找,是一种在有序数组中查找某一特定元素的搜索算法。

    将要查找的值和数组的中值进行比较,若小于中值则在中值前 面找,若大于中值则在中值后面找,等于中值时直接返回。

    然后依次是一个递归过程,将前半部分或者后半部分继续分解。

    实现

     1  public static int BinarySearch(int[] arr, int low, int high, int key)
     2         {
     3             int mid = (low + high) / 2;
     4             if (low > high)
     5                 return -1;
     6             else
     7             {
     8                 if (arr[mid] == key)
     9                     return mid;
    10                 else if (arr[mid] > key)
    11                     return BinarySearch(arr, low, mid - 1, key);
    12                 else
    13                     return BinarySearch(arr, mid + 1, high, key);
    14             }
    15         }
    16     }

    二分查找树

    二叉查找树有以下特点

    • 若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
    • 任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
    • 任意节点的左、右子树也分别为二叉查找树。
    • 没有键值相等的节点(no duplicate nodes)

     实现

     1 public class Node {
     2         public int Key { get; set; }
     3         public string Value { get; set; }
     4         public Node LeftNode { get; set; }
     5         public Node RigthNode { get; set; }
     6     }
     7 
     8     public class BST {
     9         public Node Root { get; set; }
    10 
    11         public void Push(int key,string value)
    12         {
    13             Root = Push(Root, key, value);
    14         }
    15 
    16         private Node Push(Node node,int key, string value)
    17         {
    18             if (node == null)
    19             {
    20                 return new Node { Key = key ,Value = value};
    21             }
    22             if (node.Key < key)
    23             {
    24                 node.RigthNode = Push(node.RigthNode, key, value);
    25             }
    26             else if (node.Key > key)
    27             {
    28                 node.LeftNode = Push(node.LeftNode, key, value);
    29             }
    30             return node;
    31         }
    32 
    33         public void PreOrder()
    34         {
    35             Console.WriteLine("start preorder");
    36             PreOrder(Root);
    37             Console.WriteLine("end preorder");
    38         }
    39 
    40         private void PreOrder(Node node)
    41         {
    42             Console.Write(node.Key + ":" + node.Value + "; ");
    43             if (node.LeftNode != null)
    44                 PreOrder(node.LeftNode);
    45 
    46             if (node.RigthNode != null)
    47                 PreOrder(node.RigthNode);
    48         }
    49 
    50         public string  Find( int key)
    51         {
    52             return Find(Root, key);
    53         }
    54 
    55         private string Find(Node node, int key)
    56         {
    57             if (node == null)
    58             {
    59                 return string.Empty;
    60             }
    61             if (node.Key == key)
    62             {
    63                 return node.Value;
    64             }
    65             else if (node.Key > key)
    66             {
    67                 return Find(node.LeftNode, key);
    68             }
    69             else
    70             {
    71                 return Find(node.RigthNode, key);
    72             }
    73         }
    74     }

    跳跃表

  • 相关阅读:
    1.求整数最大的连续0的个数 BinaryGap Find longest sequence of zeros in binary representation of an integer.
    JTree实例
    java发送邮件完整实例 java邮件工具类
    oracle存储过程中文乱码问题
    黑马day16 jquery&amp;内容过滤选择器&amp;可见度选择器
    隐性反馈行为数据的协同过滤推荐算法
    ACdream 1083 有向无环图dp
    正則表達式
    hdu 2209 bfs+状压
    action属性注入为null
  • 原文地址:https://www.cnblogs.com/13579net/p/10134062.html
Copyright © 2011-2022 走看看