用递归的方式实现插入、遍历、查找等,也可以不用递归方式实现。
插入:如果根为空则创建节点,如果值比根小则递归地往左子树插,如果值比根大则递归地往右子树插。插入完后别忘了用setLeftChild(或setRightChild)指向新插入的节点。插入都是从叶子节点处插。
public class BinarySearchTree {
private BinaryTreeNode root;
public void insert(int x,BinarySearchTree tree) {
if(tree.root==null) {
tree.root=new BinaryTreeNode(x);
}
else if(x<tree.root.getData()) {
BinarySearchTree leftSubTree=new BinarySearchTree(tree.root.getLeftChild());
insert(x,leftSubTree);
tree.root.setLeftChild(leftSubTree.root);
}
else if(x>tree.root.getData()) {
BinarySearchTree rightSubTree=new BinarySearchTree(tree.root.getRightChild());
insert(x,rightSubTree);
tree.root.setRightChild(rightSubTree.root);
}
}
public BinaryTreeNode find(int x,BinaryTreeNode node) {
if(node==null) return null;
if(x<node.getData()) return find(x, node.getLeftChild());
else if(x>node.getData()) return find(x, node.getRightChild());
else return node;
}
public BinaryTreeNode findMin1(BinaryTreeNode node) {//递归实现
if(node==null) return null;
else if(node.getLeftChild()!=null) return findMin1(node.getLeftChild());
else return node;
}
public BinaryTreeNode findMin2() {//循环实现
if(root==null) return null;
BinaryTreeNode tnode=root;
while(tnode.getLeftChild()!=null) {
tnode=tnode.getLeftChild();
}
return tnode;
}
public void preOrder(BinaryTreeNode node) {//前序遍历
if(node!=null) {
System.out.println(node.getData());
preOrder(node.getLeftChild());
preOrder(node.getRightChild());
}
}
public void midOrder(BinaryTreeNode node) {//中序遍历
if(node!=null) {
midOrder(node.getLeftChild());
System.out.println(node.getData());
midOrder(node.getRightChild());
}
}
public void postOrder(BinaryTreeNode node) {//后序遍历
if(node!=null) {
postOrder(node.getLeftChild());
postOrder(node.getRightChild());
System.out.println(node.getData());
}
}
public void bfs() {
Queue<BinaryTreeNode> queue=new LinkedList<BinaryTreeNode>();
queue.add(root);
int level=0;
while (!queue.isEmpty()) {
System.out.printf("第%d层数据:\n",level);
int levelSize=queue.size();
for (int i = 0; i < levelSize; i++) {
BinaryTreeNode node=queue.poll();
System.out.println(node.getData());
if (node.getLeftChild()!=null) {
queue.add(node.getLeftChild());
}
if (node.getRightChild()!=null) {
queue.add(node.getRightChild());
}
}
level++;
}
}
public BinarySearchTree(BinaryTreeNode root) {
this.root=root;
}
public BinaryTreeNode getRoot() {
return root;
}
public void setRoot(BinaryTreeNode root) {
this.root = root;
}
public static void main(String[] args) {
BinarySearchTree tree=new BinarySearchTree(null);
tree.insert(5, tree);
tree.insert(3, tree);
tree.insert(2, tree);
tree.insert(4, tree);
tree.insert(8, tree);
tree.insert(6, tree);
tree.insert(9, tree);
System.out.println("pre:");
tree.preOrder(tree.root);
System.out.println("mid:");
tree.midOrder(tree.root);
System.out.println("post:");
tree.postOrder(tree.root);
}
}
class BinaryTreeNode {
private int data;
private BinaryTreeNode leftChild;
private BinaryTreeNode rightChild;
public BinaryTreeNode(int data) {
this.data=data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public BinaryTreeNode getLeftChild() {
return leftChild;
}
public void setLeftChild(BinaryTreeNode leftChild) {
this.leftChild = leftChild;
}
public BinaryTreeNode getRightChild() {
return rightChild;
}
public void setRightChild(BinaryTreeNode rightChild) {
this.rightChild = rightChild;
}
}
输出结果: