二叉树实现类
import java.io.*;
public class BST {
private static class BSTNode implements Serializable {
Comparable data;
BSTNode left;
BSTNode right;
public BSTNode() {
}
public BSTNode(Comparable data) {
this.data = data;
}
public BSTNode(Comparable data, BSTNode left, BSTNode right) {
this.data = data;
this.left = left;
this.right = right;
}
@Override
public String toString() {
return data.toString();
}
}
private BSTNode root;
private int count;
public BST() {
clear();
}
public final void clear() {
root = null;
count = 0;
}
public void insert(Comparable val) {
root = insert(root, val);
}
public void delete(Comparable val) {
root = delete(root, val);
}
public boolean isEmpty() {
return root == null;
}
public int size() {
return count;
}
public boolean contains(Comparable val) {
return search(root, val) != null;
}
private BSTNode search(BSTNode ref, Comparable val) {
if (ref == null) {
return null;
} else {
int comp = val.compareTo(ref.data);
if (comp == 0) {
return ref;
} else if (comp < 0) {
return search(ref.left, val);
} else {
return search(ref.right, val);
}
}
}
private BSTNode insert(BSTNode ref, Comparable val) {
if (ref == null) {
count++;
return new BSTNode(val);
} else {
int comp = val.compareTo(ref.data);
if (comp < 0) {
ref.left = insert(ref.left, val);
} else {
ref.right = insert(ref.right, val);
}
return ref;
}
}
private BSTNode delete(BSTNode ref, Comparable val) {
if (ref != null) {
int comp = val.compareTo(ref.data);
if (comp == 0) {
count--;
return deleteTopmost(ref);
} else if (comp < 0) {
ref.left = delete(ref.left, val);
} else {
ref.right = delete(ref.right, val);
}
}
return ref;
}
private BSTNode deleteTopmost(BSTNode ref) {
if (ref.left == null) {
return ref.right;
} else if (ref.right == null) {
return ref.left;
} else {
ref.data = getLeftmost(ref.right);
ref.right = deleteLeftmost(ref.right);
return ref;
}
}
private Comparable getLeftmost(BSTNode ref) {
if (ref.left == null) {
return ref.data;
} else {
return getLeftmost(ref.left);
}
}
private BSTNode deleteLeftmost(BSTNode ref) {
if (ref.left == null) {
return ref.right;
} else {
ref.left = deleteLeftmost(ref.left);
return ref;
}
}
public void toBackbone() {
root = createBackbone(root);
}
/**
* 将二叉树转化为主链
*/
private BSTNode createBackbone(BSTNode root) {
if (root != null) {
root.right = createBackbone(root.right);
if (root.left != null) {
BSTNode oldRoot = root;
BSTNode ref = root = createBackbone(root.left);
oldRoot.left = null;
while (ref.right != null) {
ref = ref.right;
}
ref.right = oldRoot;
}
}
return root;
}
public void toBalanceBST() {
root = createTree(root, count);
}
/**
* 将主链转化为平衡二叉树
*/
private BSTNode createTree(BSTNode root, int n) {
if (n > 2) {
int m = (n - 1) / 2;
BSTNode oldRoot = root;
BSTNode ref = root;
for (int i = 1; i < m; i++) {
ref = ref.right;
}
root = ref.right;
ref.right = null;
root.left = createTree(oldRoot, m);
root.right = createTree(root.right, n - m - 1);
}
return root;
}
public void writeTree(String fileName) throws IOException {
FileOutputStream outFile = new FileOutputStream(fileName);
ObjectOutputStream outStream = new ObjectOutputStream(outFile);
writeTree1(root, outStream);
outStream.close();
System.out.println("二叉树写入文件...");
}
private void writeTree1(BSTNode ref, ObjectOutputStream out) throws IOException {
if (ref == null) {
return;
}
out.writeObject(ref.data);
writeTree1(ref.left, out);
writeTree1(ref.right, out);
}
/**
* 恢复到原来形状二叉查找树
*/
public static BST readTree(String fileName) throws IOException, ClassNotFoundException {
FileInputStream inFile = new FileInputStream(fileName);
ObjectInputStream inStream = new ObjectInputStream(inFile);
BST bst = new BST();
readTree1(inStream, bst);
inStream.close();
return bst;
}
private static void readTree1(ObjectInputStream in, BST bst) throws IOException, ClassNotFoundException {
try {
while (true) {
Comparable val = (Comparable) in.readObject();
bst.insert(val);
}
} catch (EOFException e) {
System.out.println("读到文件末尾...");
}
}
public void writeTreeInOrder(String fileName) throws IOException {
FileOutputStream outFile = new FileOutputStream(fileName);
ObjectOutputStream outStream = new ObjectOutputStream(outFile);
outStream.writeInt(count);
writeTree2(root, outStream);
outStream.close();
}
private void writeTree2(BSTNode ref, ObjectOutputStream out) throws IOException {
if (ref == null) {
return;
}
writeTree2(ref.left, out);
out.writeObject(ref.data);
writeTree2(ref.right, out);
}
public void readTreeInOrder(String fileName) throws IOException, ClassNotFoundException {
FileInputStream inFile = new FileInputStream(fileName);
ObjectInputStream inStream = new ObjectInputStream(inFile);
clear();
int n = inStream.readInt();
root = readTree2(inStream, n);
inStream.close();
}
private BSTNode readTree2(ObjectInputStream in, int n) throws IOException, ClassNotFoundException {
if (n == 0) {
return null;
}
BSTNode ref = new BSTNode();
ref.left = readTree2(in, n / 2);
Comparable val = (Comparable) in.readObject();
ref.data = val;
ref.right = readTree2(in, (n - 1) / 2);
return ref;
}
public void inOrder() {
inOrder(root);
}
private void inOrder(BSTNode ref) {
if (ref == null) {
return;
}
inOrder(ref.left);
System.out.println(ref.data + " ");
inOrder(ref.right);
}
public void print() {
print(root, 0);
}
/**
* 逆中序打印二叉树
*/
private void print(BSTNode ref, int level) {
if (ref == null) {
return;
}
print(ref.right, level + 1);
for (int i = 0; i < level; i++) {
System.out.print("\t");
}
System.out.println(ref.data);
print(ref.left, level + 1);
}
}
测试类
import java.io.IOException;
public class Client {
private static final String classpath = ClassLoader.getSystemClassLoader().getResource("").getFile();
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 插入和删除
BST bst = new BST();
bst.insert(1);
bst.insert(2);
bst.insert(3);
bst.insert(4);
bst.insert(5);
bst.insert(6);
bst.insert(7);
bst.insert(8);
bst.insert(9);
System.out.println(bst.contains(3));
bst.delete(3);
bst.toBalanceBST(); // 二叉树平衡化
System.out.println("==============二叉树平衡化===============");
bst.print();
bst.toBackbone(); // 转化为主链
System.out.println("==============转化为主链===============");
bst.print();
// 二叉树写入文件与从文件读取一颗二叉树
bst.writeTree(classpath + "bst");
BST bst2 = BST.readTree(classpath + "bst");
System.out.println("==============从文件还原二叉树===============");
bst2.print();
// 将二叉树转化为平衡二叉树
bst.writeTreeInOrder(classpath + "bst");
bst.readTreeInOrder(classpath + "bst");
System.out.println("==============从文件还原平衡二叉树===============");
bst.print();
}
}