折半查找和二叉排序树

早过忘川 提交于 2020-01-30 12:23:43

一、实验目的
1、掌握查找的特点。
2、掌握折半查找的基本思想及其算法。
3、熟悉二叉排序树的特点,掌握二叉排序树的插入、删除操作。
二、实验内容
1、设有关键字序列k={ 5 ,14 ,18 ,21 ,23 ,29 ,31 ,35 },查找key=21和key=25的数据元素。
2、根据关键字序列{45、24、53、12、37、93}构造二叉排序树,并完成插入13删除关键字53和24的操作。
三、实验环境
Eclipse环境或C++编程环境
四、实验步骤
1、折半查找
(1)从键盘输入上述8个整数5 ,14 ,18 ,21 ,23 ,29 ,31 ,35,存放在数组bub[8]中,并输出其值。
(2)从键盘输入21,查找是否存在该数据元素,若存在,则输出该数据元素在表中的位置,否则给出查找失败的信息。
(3)从键盘输入25,查找是否存在该数据元素,若存在,则输出该数据元素在表中位置,否则给出查找失败的信息。
2、二叉排序树
(1)二叉排序树存储定义
(2)从键盘上输入六个整数45、24、53、12、37、9构造二叉排序树
(3)输出其中序遍历结果。
(4)插入数据元素13,输出其中序遍历结果。
(5)删除数据元素24和53,输出其中序遍历结果。

折半查找:

package shiyan4;

import java.util.Scanner;
public class BinarySearch {
	public static void main(String[] args) {
		System.out.println("请输入8个整数:");
		Scanner sc=new Scanner(System.in);
		int [] bub=new int[8];
		for(int i=0;i<bub.length;i++) {
			bub[i]=sc.nextInt();
			System.out.print(bub[i]+" ");
		}
		Scanner sc1=new Scanner(System.in);
		System.out.println("请输入要查找的数据:");
		for(int i=0;i<2;i++) {
			int x=sc.nextInt();
			int loc=binarySearch(bub,bub.length,x);
			System.out.print(loc);
		}
}
	private static int binarySearch(int [] bub,int length,int x) {
		int left=0;
		int right=length-1;
		int mid;
		while(left<=right) {
			mid=(left+right)/2;
			if(bub[mid]==x) {
				return mid;
			}
			else if(bub[mid]>x) {
				right=mid-1;
			}
			else {
				left=mid+1;
			}
		}
		return -1;
	}
}

测试类:

package shiyan4;
import java.util.Scanner;

	

	public class Test1 {
		public static int binarySearch(int a[],int key){
			int length=a.length;
			int low=0,high=length-1,mid;
			while(low<=high){
				mid=(low+high)/2;
				if(key==a[mid]){
					return mid;
				}
				else if(a[mid]>key){
					high=mid-1;
				}
				else{
					low= mid+1;
				}
			}
			return -1;
		}
		public static void main(String[] args) {
			
			Scanner sc=new Scanner(System.in);
			System.out.println("请输入8个元素:");
			int[] bub=new int[8];
			for(int i=0;i<8;i++) {
				bub[i]=sc.nextInt();
			}
			for(int i=0;i<8;i++) {
				System.out.print(bub[i]+" ");
			}
			System.out.println();
			System.out.println("请输入要查找的元素:");
			int key=sc.nextInt();
			System.out.println("该元素在"+binarySearch(bub, key)+"号位置");
		}
		
	}

二叉排序树:

package shiyan4;

import java.util.Scanner;

class BTNode<AnyType>{//节点类
	AnyType data;
	BTNode leftChildNode,rightChildNode;
	public BTNode() {
		data=null;
		leftChildNode=rightChildNode=null;
	}
	public BTNode(AnyType d,BTNode<AnyType> lc,BTNode<AnyType> rc) {
		data=d;
		leftChildNode=lc;
		rightChildNode=rc;
	}
	public BTNode(AnyType d) {
		data=d;
		leftChildNode=null;
		rightChildNode=null;
	}
	public BTNode<AnyType> getleftChild(){
		return leftChildNode;
	}
	public BTNode<AnyType> getrightChild(){
		return rightChildNode;
	}
	public Object getData() {
		return data;
	}
}
public class BST<AnyType extends Comparable<?super AnyType>>{
	private BTNode root=null;
	public BST() {
		
	}
	public BST(BTNode<AnyType> node) {
		root=node;
	}
	BTNode<AnyType> insert(AnyType x,BTNode<AnyType> t){//插入节点
		if(t==null) {
			return new BTNode(x,null,null);
		}
		int compareResult=x.compareTo(t.data);
		if(compareResult<0) {
			t.leftChildNode=insert(x,t.leftChildNode);
		}
		else if(compareResult>0) {
			t.rightChildNode=insert(x,t.rightChildNode);
		}
		else;
		return t;
	}
	BTNode<AnyType> remove(AnyType x,BTNode<AnyType> t){//删除节点
		if(t==null) {
			return t;
		}
		int compareResult=x.compareTo(t.data);
		if(compareResult<0) {
			t.leftChildNode=remove(x,t.leftChildNode);
		}
		else if(compareResult>0) {
			t.rightChildNode=remove(x,t.rightChildNode);
		}
		else if(t.leftChildNode!=null&&t.rightChildNode!=null) {
			t.data=(AnyType) findMin(t.rightChildNode).data;
			t.rightChildNode=remove(t.data,t.rightChildNode);
		}
		else {
			t=(t.leftChildNode!=null)?t.leftChildNode:t.rightChildNode;
		}
		return t;
	}
	public BTNode<AnyType> findMin(BTNode<AnyType> t){//寻找最小值
		if(t!=null) {
			while(t.leftChildNode!=null) {
				t=t.leftChildNode;
			}
		}
		return t;
	}
	public BTNode createBSTree(int n,AnyType a[]) {//创建一棵二叉排序树
		for(int i=0;i<n;i++) {
			root=insert(a[i],root);
		}
		return root;
	}
	public void inOrder(BTNode<AnyType> rootNode) {//中序遍历
		if(rootNode!=null) {
			inOrder(rootNode.leftChildNode);
			System.out.print(rootNode.data+" ");
			inOrder(rootNode.rightChildNode);
		}
	}
	public boolean contains(AnyType x) {
		return contains(x,root);
	}
	public boolean contains(AnyType x,BTNode<AnyType> t) {//查找某个元素
		if(t==null) {
			return false;
		}
		int compareResult=x.compareTo(t.data);
		if(compareResult<0) {
			return contains(x,t.leftChildNode);
		}
		else if(compareResult>0) {
			return contains(x,t.rightChildNode);
		}
		else {
			return true;
		}
	}
	public void init() {
		Integer a[]=new Integer[100];
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入要创建的二叉查找树的元素个数:");
		int n=sc.nextInt();
		System.out.println("请输入二叉查找树的元素:");
		for(int i=0;i<n;i++) {
			a[i]=sc.nextInt();
		}
		BST<Integer> bst=new BST<Integer>();
		BTNode<Integer> node=bst.createBSTree(n, a);
		BST<Integer> test=new BST<Integer>(node);
		System.out.println("中序遍历的结果是:");
		bst.inOrder(test.root);
		System.out.println();
		System.out.println("请输入要插入的元素个数:");
		int num=sc.nextInt();
		System.out.println("请输入要插入的元素:");
		int ts[]=new int[num];
		for(int i=0;i<num;i++) {
			ts[i]=sc.nextInt();
			//while(num>0) {
				bst.insert(ts[i],test.root);
			//	num--;
			//}
		}
		
		System.out.println("插入元素后的中序遍历结果为:");
		bst.inOrder(test.root);
		System.out.println();
		System.out.println("请输入要删除的元素的个数:");
		int num1=sc.nextInt();
		System.out.println("请输入要删除的元素:");
		int ts1[]=new int[num1];
		for(int i=0;i<num1;i++) {
			ts1[i]=sc.nextInt();
			bst.remove(ts1[i],test.root);
		}
		System.out.println("删除元素后的中序遍历结果为:");
		bst.inOrder(test.root);
		System.out.println();
	}
}

测试类:

package shiyan4;
public class text {
	public static void main(String[] args) {
		BST<Integer> bst=new BST<Integer>();
		bst.init();
	}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!