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实现的具体代码:
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; } }
相关文章推荐
- PAT04-树4 是否同一棵二叉搜索树【JAVA实现】
- PAT:是否为同一棵二叉搜索树(Java实现)
- PAT04-树7 二叉搜索树的操作集【JAVA实现】
- PAT04-树6 Complete Binary Search Tree 【JAVA实现】
- PAT 04-2. File Transfer (并查集) (Java实现)
- 剑指offer--面试题24:二叉搜索树的后序遍历序列--Java实现
- 剑指offer--面试题27:二叉搜索树与双向链表--Java实现
- PAT Advanced Level 1020. Tree Traversals (25)(Java 实现)
- Java代码实现二叉搜索树-----插入
- 二叉搜索树之Java实现
- 数据结构Java实现04----循环链表、仿真链表
- 二叉搜索树及其方法的java实现
- [Java] 动态代理 04 - 想实现什么功能,就实现什么功能
- BST(二叉搜索树) Java 实现解析
- Java实现二叉搜索树及相关操作
- Java 实现PAT乙级(Basic Level)1001-1010 解题报告(一)
- Java实现二叉搜索树节点的删除
- JAVA语言 实现 SI/PSI表中的PAT解析
- [Java] 动态代理 04 - 想实现什么功能,就实现什么功能
- PAT 3-04 一元多项式的乘法与加法运算(C语言实现)