您的位置:首页 > 编程语言 > Java开发

PAT04-树7 二叉搜索树的操作集(Java实现)

2016-12-20 11:42 465 查看
本题为浙大数据结构课后题:

BinTree Insert( BinTree BST, ElementType X ); 

BinTree Delete( BinTree BST, ElementType X ); 

Position Find( BinTree BST, ElementType X ); 

Position FindMin( BinTree BST ); 

Position FindMax( BinTree BST );

其中BinTree结构定义如下:

typedef struct TNode *Position; 

typedef Position BinTree; 

struct TNode{ 

ElementType Data; 

BinTree Left; 

BinTree Right; 

};

测试结果:
10
5 8 6 2 4 1 0 10 9 7
先序遍历:
5 2 1 0 4 8 6 7 10 9
中序遍历:
0 1 2 4 5 6 7 8 9 10
二叉树最小值=0
二叉树最大值=10
删除7,3,8
3不存在
删除7,3,8之后的中序遍历:
0 1 2 4 5 6 9 10

下面是java实现的具体代码:


import java.util.Scanner;

/*
*建立二叉搜索树类
*/
class BinSearchNode{
int value;
BinSearchNode left;
BinSearchNode right;
BinSearchNode(int value){
this.value=value;
left=null;
right=null;
}
}

public class BinarySearchOperate {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
BinSearchNode head=null;
int N=sc.nextInt();
for(int i=0;i<N;i++){
head=insert(sc.nextInt(),head);
}
System.out.println("先序遍历:");
preOrder(head);
System.out.println();
System.out.println("中序遍历:");
inOrder(head);
System.out.println();
System.out.println("二叉树最小值="+findMin(head));
System.out.println("二叉树最大值="+findMax(head));
System.out.println("删除7,3,8");
head=delete(7,head);
head=delete(3,head);
head=delete(8,head);

System.out.println("删除7,3,8之后的中序遍历:");

inOrder(head);

sc.close();
}

/*
* 插入操作:这里默认插入元素不重复
*/
public static BinSearchNode insert(int x,BinSearchNode head){
if(head==null){
return new BinSearchNode(x);
}
if(x>head.value){
head.right=insert(x,head.right);
}else{
head.left=insert(x,head.left);
}
return head;
}

/*
* 递归实现前序遍历
*/
public static void preOrder(BinSearchNode head){
System.out.print(head.value+" ");
if(head.left!=null)
preOrder(head.left);
if(head.right!=null)
preOrder(head.right);
}

/*
* 递归实现中序遍历
*/
public static void inOrder(BinSearchNode head){
if(head.left!=null)
inOrder(head.left);
System.out.print(head.value+" ");
if(head.right!=null)
inOrder(head.right);
}

/*
* 找最小元:递归实现
*/
public static int findMin(BinSearchNode head){
if(head.left!=null)
return findMin(head.left);
return head.value;
}

/*
* 找最大元:非递归实现
*/
public static int findMax(BinSearchNode head){
while(head.right!=null){
head=head.right;
}
return head.value;
}

/*
* 删除操作
*/
public static BinSearchNode delete(int x,BinSearchNode head){
if(head==null){
System.out.println(x+"不存在");
return head;
}
if(x<head.value)
head.left=delete(x,head.left);
else if(x>head.value)
head.right=delete(x,head.right);
else if(head.left!=null && head.right!=null){
head.value=findMin(head.right);
head.right=delete(head.value,head.right);
}
else
head=(head.left==null)?head.right:head.left;

return head;
}

/*
* 寻找节点操作
*/
public static boolean contains(int x,BinSearchNode head){
if(head==null)
return false;
if(x>head.value)
return contains(x,head.right);
else if(x<head.value)
return contains(x,head.left);
else
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: