java数据结构--二叉树与查找树
pre:树是链表的延伸,所以做好树需要对链表有一定理解,还要熟悉树的结构,话不多说,详见代码。
code:
package BinaryTreeDemo;
import static BinaryTreeDemo.BTDemo.i;
import static BinaryTreeDemo.BTDemo.size;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BTDemo<Anytype> {
BTNode root;
public static int size,i;//initial串长、串指针
BTDemo(Anytype d){
root=new BTNode(d);
}
public void addL(BTNode n,Anytype d){
BTNode p=new BTNode(d);
n.leftC=p;
}
public void addR(BTNode n,Anytype d){
BTNode p=new BTNode(d);
n.rightC=p;
}
public Anytype findMax(BTNode p){
if(p.rightC!=null)return findMax(p.rightC);
else return p.data;
}
public Anytype findMin(BTNode p){
if(p.leftC!=null)return findMin(p.leftC);
else return p.data;
}
public void preorderTraversal(){
preorderTraversal(this.root);
}
public void preorderTraversal(BTNode p){
if(p==null)return;
p.print();
preorderTraversal(p.leftC);
preorderTraversal(p.rightC);
}
public void inorderTraversal(){
inorderTraversal(this.root);
}
public void inorderTraversal(BTNode p){
if(p==null)return;
inorderTraversal(p.leftC);
p.print();
inorderTraversal(p.rightC);
}
public void postorderTraversal(){
postorderTraversal(this.root);
}
public void postorderTraversal(BTNode p){
if(p==null)return;
postorderTraversal(p.leftC);
postorderTraversal(p.rightC);
p.print();
}
public int levelCounting(){
//if(this.root==null)return 0;
//else 没必要
return levelCounting(this.root,0);
}
public int levelCounting(BTNode p,int i){
if(p==null)return i;
int l=levelCounting(p.leftC,i),r=levelCounting(p.rightC,i);
return l>r?l+1:r+1;
}
public int leafCounting(){
return leafCounting(this.root,0);
}
public int leafCounting(BTNode p,int i){
if(p.leftC==null&&p.rightC==null)return i+1;
else if(p.leftC==null)return leafCounting(p.rightC,i)+1;
else if(p.rightC==null)return leafCounting(p.leftC,i)+1;
else return leafCounting(p.leftC,i)+1+leafCounting(p.rightC,i);
}
public void levelTraversal(){
if(root.data==null){
System.out.println("This Binary Tree is null");
return;
}
Queue<BTNode> q = new LinkedList<BTNode>();
q.add(root);
BTNode first;
while(!q.isEmpty()){
first=q.remove();
if(first.leftC!=null)q.add(first.leftC);
if(first.rightC!=null)q.add(first.rightC);
first.print();
}
}
public void BTBuilding1(Anytype c[],Anytype n){
this.root.data=c[0];
BTBuilding1(c,root,n);
}
public void BTBuilding1(Anytype c[],BTNode btn,Anytype n){
//if(i>=size||c[i+1].equals(n)){i++;return;}
if(i>=size||c[i+1].equals(n)&&c[i+2].equals(n)){i+=2;return;}
if(!c[i+1].equals(n)){
btn.leftC=new BTNode(c[++i]);
BTBuilding1(c,btn.leftC,n);
if(!c[i+1].equals(n)){
btn.rightC=new BTNode(c[++i]);
BTBuilding1(c,btn.rightC,n);
}
else i++;//要记住序列是要一直向后遍历的
}
else{
btn.rightC=new BTNode(c[i+=2]);
BTBuilding1(c,btn.rightC,n);
}
}//test case one:ab cd efh i g two:ABD CE F
public class BTNode{
Anytype data;
BTNode leftC,rightC;
BTNode(){
this(null,null,null);
}
BTNode(Anytype d){
this(d,null,null);
}
BTNode(Anytype d,BTNode l,BTNode r){
this.data=d;
leftC=l;
rightC=r;
}
void print(){
System.out.print(this.data+" ");
}
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
// BTDemo<Integer> b=new BTDemo<Integer>(9);
// b.addL(b.root, 5);//暴力创建,可以放出来看看
// b.addR(b.root, 2);
// b.levelTraversal();
// System.out.println(b.findMin(b.root));
BTDemo<Character> b2=new BTDemo<Character>('0');
String s=in.nextLine();
Character c[]=new Character[1000];
size=s.length();
for(int i=0;i<size;i++){
c[i]=s.charAt(i);
}
b2.BTBuilding1(c,' ');
b2.levelTraversal();
b2.postorderTraversal();
System.out.println(b2.leafCounting());
BST<Integer> bst=new BST<Integer>();
System.out.println("构建二叉查找树,要求一串数字,请先输入数字个数");
size=in.nextInt();
Integer d[]=new Integer[1000];
for(int i=0;i<size;i++){
d[i]=in.nextInt();
}
bst.BSTBuilding(d);
bst.levelTraversal();
System.out.println("37 exsist?"+bst.contains(37));
System.out.println("50 exsist?"+bst.contains(50));
bst.remove(37);
bst.inorderTraversal();
}
}
class BST<Anytype extends Comparable> extends BTDemo{
public BST(){
super(null);
}
public void BSTBuilding(Anytype c[]){
this.root=new BTNode(c[0]);
for(i=1;i<size;i++){
//findPos(root,c[i]);//递归错了,下面注释的就是
BTNode p=root;//不论是递归、非递归,能挂到链上才是王道
while(p!=null){
if(c[i].compareTo(p.data)>0){
if(p.rightC==null){p.rightC=new BTNode(c[i]);break;}
else p=p.rightC;
}
else {
if(p.leftC==null){p.leftC=new BTNode(c[i]);break;}
else p=p.leftC;
}
}
}
}
// private BTNode findPos(BTNode p,Anytype data){
// //System.out.println("下");
// if(p==null){return new BTNode(data);}
// if(data.compareTo(p.data)>0)return findPos(p.rightC,data);
// else return findPos(p.leftC,data);
// }
public boolean contains(Anytype x){
BTNode p=root;
while(p!=null){
if(x.compareTo(p.data)>0)p=p.rightC;
else if(x.compareTo(p.data)<0)p=p.leftC;
else return true;
}
return false;
}
public void insert(Anytype x){
if(this.contains(x)){System.out.println("已有");return;}
insert(root,x);
}
public void insert(BTNode p,Anytype x){
if(x.compareTo(p.data)>0){
if(p.rightC!=null)insert(p.rightC,x);
else p.rightC=new BTNode(x);
}
else{
if(p.leftC!=null)insert(p.leftC,x);
else p.leftC=new BTNode(x);
}
}
public BTNode remove(Anytype x){
if(!contains(x)){System.out.println("无此元素");return null;}
else return remove(root,x);
}
public BTNode remove(BTNode p,Anytype x){
if(p==null)return null;
if(x.compareTo(p.data)<0)p.leftC=remove(p.leftC,x);
else if(x.compareTo(p.data)>0)p.rightC=remove(p.rightC,x);
else if(p.leftC!=null&&p.rightC!=null){
p.data=findMin(p.rightC);
p.rightC=remove(p.rightC,(Anytype) p.data);
}
else p=(p.leftC!=null)?p.leftC:p.rightC;
return p;
}
}
PS:main(String[]args)、NTNode二叉节点类都在二叉树类里。二叉树和二叉查找树写在一个文件里了,查找树类继承的二叉树类。
PSS:创建难,其余易,主要要求对树结构的理解程度(基础不提)。
PSSS:数据结构课的第二次实验,较上次单链表的实验稍难,也更有意思。