查找
基于线性表的查找
顺序查找
package search1;
public class SeqSearch {
public static void main(String[] args) {
int[] nums={-1,1,2,3,4,5,0,9,8,7,6};
int k=0;
System.out.println(new SeqSearch().seqSearch(nums, k));
System.out.println(new SeqSearch().seqSearchPlus(nums, k));
}
public int seqSearch(int[] nums,int k){
int i=nums.length-1;
while (i>=1&&nums[i]!=k){i--;}
return i;
}
public int seqSearchPlus(int[] nums,int k){
nums[0]=k;
int i=nums.length-1;
while (nums[i]!=k){i--;}
return i;
}
}
折半查找
package search1;
public class BinSrch {
public static void main(String[] args) {
int[] nums={-1,12,19,25,33,46,58,64,80};
int k=80;
System.out.println(new BinSrch().binSrchN(nums, k));
System.out.println(new BinSrch().binSrch(nums, k,1, nums.length-1));
}
public int binSrchN(int[] nums,int k){
int low=1,high= nums.length-1,mid;
while (low<=high){
mid=(low+high)/2;
if(k==nums[mid]) return mid;
else if(k<nums[mid]) high=mid-1;
else low=mid+1;
}
return 0;
}
public int binSrch(int[] nums,int k,int low,int high){
int mid=(low+high)/2;
if(k==nums[mid]) return mid;
else if(k<nums[mid]) return binSrch(nums,k,low,mid-1);
else return binSrch(nums,k,mid+1,high);
}
}
基于树的查找
二叉排序树
package search1;
import java.util.LinkedList;
import java.util.Queue;
class BSTNode {
int key;
BSTNode lChild;
BSTNode rChild;
public BSTNode() {
}
public BSTNode(int key) {
this.key = key;
}
public BSTNode(int key, BSTNode lChild, BSTNode rChild) {
this.key = key;
this.lChild = lChild;
this.rChild = rChild;
}
@Override
public String toString() {
return "BSTNode{" +
"key=" + key +
", lChild=" + lChild +
", rChild=" + rChild +
'}';
}
}
class BsTree {
BSTNode root;
BsTree(){
root=null;
}
public BSTNode setTree(Integer[] data)
{
root = new BSTNode(data[1]);
Queue<BSTNode> queue = new LinkedList<>();
queue.offer(root);
int i = 2;
while(i<data.length)
{
BSTNode node = queue.poll();
if(data[i] != null)
{
node.lChild = new BSTNode(data[i]);
queue.offer(node.lChild);
}
i++;
if(data[i] != null)
{
node.rChild = new BSTNode(data[i]);
queue.offer(node.rChild);
}
i++;
}
return root;
}
void visit(int n){
System.out.print(n+" ");
}
void inOrder(BSTNode root){
if (root!=null){
inOrder(root.lChild);
visit(root.key);
inOrder(root.rChild);
}
}
BSTNode searchBSTN(BSTNode root, int k){
BSTNode q;
q=root;
while (q!=null){
if(q.key==k){
return q;
}
if (k<q.key){
q=q.lChild;
}else{
q=q.rChild;
}
}
return null;
}
BSTNode searchBST(BSTNode root, int k){
if (root==null) return null;
else if(root.key==k) return root;
else if(k<root.key) return searchBST(root.lChild, root.key);
else return searchBST(root.rChild, k);
}
void insertBST(BSTNode root,int k){
BSTNode s;
if (root==null){
s=new BSTNode(k);
root=s;
}
else if(k<root.key) {insertBST(root.lChild,k);}
else if(k>root.key) {insertBST(root.rChild,k);}
}
public void insert(int k){
BSTNode newNode=new BSTNode(k);
if (root==null){
root=newNode;
}
else{
BSTNode current = root;
BSTNode parent;
while (true) {
parent = current;
if (k < current.key) {
current = current.lChild;
if (current == null) {
parent.lChild = newNode;
return;
}
}
else {
current = current.rChild;
if (current == null) {
parent.rChild = newNode;
return;
}
}
}
}
}
public void create(int []nums){
for (int i = 0; i < nums.length; i++) {
insert(nums[i]);
}
}
public boolean delete(int key) {
BSTNode current = root;
BSTNode parent = root;
boolean isLeftChild = true;
while (current.key != key) {
parent = current;
if (key < current.key) {
isLeftChild = true;
current = current.lChild;
} else {
isLeftChild = false;
current = current.rChild;
}
if (current == null)
return false;
}
if (current.lChild == null && current.rChild == null) {
if (current == root)
root = null;
else if (isLeftChild)
parent.lChild = null;
else
parent.rChild = null;
}
else if (current.rChild == null) {
if (current == root) root = current.lChild;
else if (isLeftChild)
parent.lChild = current.lChild;
else
parent.rChild = current.lChild;
}
else if (current.lChild == null) {
if (current == root)
root = current.rChild;
else if (isLeftChild) parent.lChild = current.rChild;
else
parent.rChild = current.rChild;
} else {
BSTNode successor = getSuccessor(current);
if (current == root)
root = successor;
else if (isLeftChild)
parent.lChild = successor;
else
parent.rChild = successor;
successor.lChild = current.lChild;
}
return true;
}
private BSTNode getSuccessor(BSTNode delNode) {
BSTNode successorParent = delNode;
BSTNode successor = delNode;
BSTNode current = delNode.rChild;
while (current != null) {
successorParent = successor;
successor = current;
current = current.lChild;
}
if (successor != delNode.rChild) {
successorParent.lChild = successor.rChild;
successor.rChild = delNode.rChild;
}
return successor;
}
}
public class SearchBSt {
public static void main(String[] args) {
Integer[] nums={null,44,21,65,14,32,58,72,null,null,null,null,null,null,null,80};
BsTree bsTree = new BsTree();
bsTree.setTree(nums);
bsTree.inOrder(bsTree.root);
System.out.println();
System.out.println(bsTree.searchBSTN(bsTree.root, 80));
System.out.println(bsTree.searchBST(bsTree.root, 80));
bsTree.insert(69);
bsTree.inOrder(bsTree.root);
System.out.println();
BsTree bst=new BsTree();
int[] numbers={44,21,65,14,32,58,72,80};
bst.create(numbers);
bst.inOrder(bst.root);
System.out.println();
bst.delete(32);
bst.inOrder(bst.root);
System.out.println();
}
}
平衡二叉树
B树和B+树
伸展树
红黑树
散列
哈希函数的构造方法
处理冲突的方法
哈希表的查找
package search1;
import java.util.Arrays;
class HashTable {
public final int HASHSIZE=11;
Integer[] nums;
int [] times;
int hashFunc(int key){
return key % HASHSIZE;
}
int collision(int di){
return (di+1)%HASHSIZE;
}
int hashSearch(Integer x){
int address;
address=hashFunc(x);
while (nums[address]!=null&&!x.equals(nums[address])){
address=collision(address);
}
if(x.equals(nums[address])){
return address;
}else {
return -1;
}
}
int hashInsert(int x){
int address;
address=hashSearch(x);
if(address>=0) return 0;
int time=1;
address= hashFunc(x);
while(nums[address]!=null){
address=collision(address);
time++;
}
nums[address]=x;
times[address]=time;
return 1;
}
void createHt(int[] numbers){
nums=new Integer[HASHSIZE];
times=new int[HASHSIZE];
for (int i = 0; i < numbers.length; i++) {
hashInsert(numbers[i]);
}
}
int hashDel(int x){
int address;
address=hashFunc(x);
if (address>=0){
nums[address]=null;
return 1;
}
return 0;
}
}
public class HashSearch{
public static void main(String[] args) {
HashTable ht=new HashTable();
int[] numbers={19,1,23,14,55,68,11,82,36};
ht.createHt(numbers);
System.out.println(Arrays.toString(ht.nums));
System.out.println(ht.hashSearch(14));
System.out.println(ht.hashDel(14));
System.out.println(ht.hashSearch(14));
}
}