package 无序表查找;
public class 顺序表查找算法Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int array[] = new int[]{0,1,2,3,4,5};
System.out.println(new 顺序表查找算法Test().Sequential_Search2(array,4));
}
//
int Sequential_Search(int [] array,int key){
if(array == null || array.length <1) return -1;
for(int i = 0;i<array.length;i++){
if(key == array[i]) return i;
}
return -1;
}
//顺序查找的改进 可改变顺序表的结构
int Sequential_Search2(int [] array,int key){
if(array == null || array.length <1 ) return -1;
if(array[0] == key) return 0;
array[0]=key;//哨兵
int i = array.length-1;
while(array[i] != key){//免去了每次循环时候都要检查是否越界的判断
i--;
}
if(i == 0) return -1;
else return i;
}
}
package 有序表查找;
public class 折半查找Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
//array升序
int Binary_Search(int [] array,int key){
if(array == null || array.length < 1) return 0;
int start = 0;
int end = array.length-1;
int mid = 0;
while(start <= end){
mid=(start+end)/2;
if( array[mid] == key) return mid;
else if(array[mid] > key) end = mid-1;
else start=mid+1;
}
return -1;
}
}
package 二叉排序树;
public class BST{
BinaryTreeNode root;
public BST(BinaryTreeNode root){
this.root = root;
}
public boolean SearchBST(int key){
return SearchBST(root,key);
}
private boolean SearchBST(BinaryTreeNode node,int key){
if(node == null) return false;
if(node.val == key){
return true;
}else if(node.val < key){
return SearchBST(node.right,key);
}else{
return SearchBST(node.left,key);
}
}
public boolean InsertBST(int key){
if(SearchBST(key)) {
System.out.println("key已经存在于此二叉树中");
return false;
}
return InsertBST(this.root,key);
}
private boolean InsertBST(BinaryTreeNode node,int key){
if(node.val < key){
if(node.right == null){
BinaryTreeNode n = new BinaryTreeNode(key);
node.right = n;
return true;
}else{
return InsertBST(node.right,key);
}
}else{
if(node.left == null){
BinaryTreeNode n = new BinaryTreeNode(key);
node.left = n;
return true;
}else{
return InsertBST(node.left,key);
}
}
}
public boolean DeleteBST(int key){
if(!SearchBST(key)) {
System.out.println("不存在此结点,无法进行删除操作");
return false;
}
return DeleteBST(this.root,null,key);
}
private boolean DeleteBST(BinaryTreeNode node,BinaryTreeNode parent,int key){
if(node.val < key) return DeleteBST(node.right,node,key);
else if(node.val > key) return DeleteBST(node.left,node,key);
else{
if(node.left == null && node.right == null){//叶子结点
if(node == root){//只有一个根结点
this.root=null;
return true;
}else{
if(parent.left == node) parent.left = null ;
else parent.right = null;
node=null;
return true;
}
}else if(node.left == null && node.right != null){//左子树为空
if(parent.left == node) parent.left = node.right;
else parent.right = node.right;
node = null;
return true;
}else if(node.left != null && node.right == null){//右子树为空
if(parent.left == node) parent.left = node.left;
else parent.right = node.left;
node = null;
return true;
}else{//左右子树均不为空
BinaryTreeNode temp = node.right;
BinaryTreeNode q = node;
while(temp.left != null){
q=temp;
temp=temp.left;
}
int t = temp.val;
temp.val=node.val;
node.val=t;
return DeleteBST(temp,q,key);
}
}
}
public static void main(String[] args) {
BinaryTreeNode root = new BinaryTreeNode(62);
BST tree = new BST(root);
/*tree.InsertBST(58);
tree.InsertBST(88);
tree.InsertBST(47);
tree.InsertBST(73);
tree.InsertBST(99);
tree.InsertBST(93);
tree.InsertBST(102);*/
/* System.out.println(tree.SearchBST(88));
System.out.println(tree.SearchBST(47));
System.out.println(tree.SearchBST(99));
System.out.println(tree.SearchBST(58));
System.out.println(tree.SearchBST(93));*/
//System.out.println(tree.DeleteBST(102));
System.out.println(tree.root);
System.out.println(tree.DeleteBST(62));
System.out.println(tree.root);
/*System.out.println(tree.root);
System.out.println(tree.removeBST(62));
System.out.println(tree.root);*/
}
}
/*
* 二叉树结点
* */
public class BinaryTreeNode {
int val;
BinaryTreeNode left;
BinaryTreeNode right;
public BinaryTreeNode(int val){
this.val=val;
}
@Override
public String toString() {
return "BinaryTreeNode [val=" + val + ", left=" + left + ", right="
+ right + "]";
}
}