Java SE Consolidation - find

Search function is a basic function of data processing. Data searching is not complicated, but how to find data quickly and well?

Sequential search

Sequential search is to find the exact location of the same number as the given key in a known no (or ordered) sequence queue.
[algorithm principle]
Compare the keyword and the number in the queue one by one from the last until you find the same number as the given keyword.
[algorithm description]

public static int ordersearch(int[] arr,int des){
    for(int i=0;i<=arr.length-1;i++){
    if(des==arr[i])
        return i;
    }
    return -1;
}

Two point search

Binary search method is to search a group of orderly numbers, transfer the corresponding data, compare and find the same data as the original data, find the corresponding array subscript returned, and find no return-1;
[Algorithm description]
1. Recursion

public static int binSearch(int srcArray[], int start, int end, int key) {  
    int mid = (end - start) / 2 + start;  
    if (srcArray[mid] == key) {  
        return mid;  
    }  
    if (start >= end) {  
        return -1;  
    } else if (key > srcArray[mid]) {  
        return binSearch(srcArray, mid + 1, end, key);  
    } else if (key < srcArray[mid]) {  
        return binSearch(srcArray, start, mid - 1, key);  
    }  
    return -1;  
} 


2. Non recursive mode

public static int binSearch(int srcArray[], int key) {  
    int mid;  
    int start = 0;  
    int end = srcArray.length - 1;  
    while (start <= end) {  
        mid = (end - start) / 2 + start;  
        if (key < srcArray[mid]) {  
            end = mid - 1;  
        } else if (key > srcArray[mid]) {  
            start = mid + 1;  
        } else {  
            return mid;  
        }  
    }  
    return -1;  
} 

Interpolation search

Interpolation search is more flexible,It's not just from the middle,It searches incrementally based on the values we need to query. The difference of interpolation search is that each time is not cut from the middle,Instead, the search is based on the distance from the evaluation.It is a deformation of binary search.

[Algorithm description]

public static boolean search(int[] arr,int key,int left,int right){
    while(left<right){
        int middle=left+(right-left)*((key-arr[left])/(arr[right]-arr[left]));
        if(arr[middle]==key){
            return true;
        }
        if(key<arr[middle]){
            right=middle-1;
        }else{
            left=middle+1;
        }
    }
    return false;
}

Binary tree search

Binary search tree(Binary Search Tree),Also known as binary search tree. It's a special binary tree: for binary trees, suppose x Is any node in a binary tree, x Node contains keywords key,node x Of key The value is recorded as key[x]. If y yes x A node in the left subtree of key[y] <= key[x];If y yes x A node of the right subtree of, then key[y] >= key[x]. So, this tree is a binary search tree.

[As shown in the figure]

[Binary search tree features]
1. If the left subtree of any node is not empty, the values of all nodes on the left subtree are smaller than the values of its root nodes;
2. If the right subtree of any node is not empty, the values of all nodes on the right subtree are greater than the values of its root nodes;
3. The left and right subtrees of any node are also binary search trees.
4. No nodes with equal key values( no duplicate nodes).

[Binary search tree node definition]

public class BSTNode<T extends Comparable<T>> {
    T key;                // Key (key value)
    BSTNode<T> left;      // Left child
    BSTNode<T> right;     // Right child
    BSTNode<T> parent;    // Parent node
    public BSTNode(T key, BSTNode<T> parent, BSTNode<T> left, BSTNode<T> right) {
        this.key = key;
        this.parent = parent;
        this.left = left;
        this.right = right;
    }
}
public class BSTree<T extends Comparable<T>> {
    private BSTNode<T> mRoot;    // Root node
    ......
}
BSTree It's a binary tree,It protects the root node of a binary tree mRoot;mRoot yes BSTNode type,and BSTNode Is the node of the binary search tree,It is BSTree Inner class of.
BSTNode Some basic information about binary search tree:
(01) key    -- It is a keyword, which is used to sort the nodes of the binary search tree.
(02) left   -- It points to the left child of the current node.
(03) right  -- It points to the right child of the current node.
(04) parent -- It points to the parent of the current node.

Binary tree traversal

If the binary tree is not empty, do the following
 1. Preorder traversal
 (01) access root
 (02) traversing left subtree first
 (03) traversing right subtree first
 2. Middle order traversal
 (01) middle order traversal left subtree
 (02) access root
 (03) middle order traversal right subtree
 3. Post order traversal
 (01) backward traversal of left subtree
 (02) backward traversal of right subtree
 (03) access root

[as shown]

For the above binary tree:
(01) previous traversal result: 3 1 2 5 4 6
 (02) middle order traversal result: 1 23 4 5 6 
(03) post order traversal result: 2 1 4 6 5 3

[algorithm description]

//Find the node with key value in "binary tree x"
private BSTNode<T> search(BSTNode<T> x, T key) {
    if (x==null)
        return x;
    int cmp = key.compareTo(x.key);
    if (cmp < 0)
        return search(x.left, key);
    else if (cmp > 0)
        return search(x.right, key);
    else
        return x;
}
public BSTNode<T> search(T key) {
    return search(mRoot, key);
}

hash search

Posted by joix on Thu, 30 Apr 2020 12:30:54 -0700