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     }

    跳跃表

  • 相关阅读:
    【转】什么是5G?居然有人用漫画把它讲得如此接地气!
    【转】Centos下编译升级安装Boost
    【转】SSH穿越跳板机:一条命令跨越跳板机直接登陆远程计算机
    部署JupyterLab和pyalgotrade搭建web策略回测环境
    [转]Linux中python3.6+ipython+Jupyter Notebook环境
    环境命令备忘
    [转]微软商店 打开就显示无法加载该页面 代码0x80131500?
    [转]Centos 7 安装部署 GitLab 服务器
    [转]本文采用all-in-one(一体化的)安装OpenShift
    [转]Linux编译和安装boost库
  • 原文地址:https://www.cnblogs.com/13579net/p/10134062.html
Copyright © 2011-2022 走看看