BinaryTree.java
package com.binarytree.test;
import java.util.*;
public class BinaryTree>{
private TreeNode root;
private int size=0;
public BinaryTree() {
}
public BinaryTree(E[] objects) {
for (int i = 0; i < objects.length; i++) {
insert(objects[i]);
}
}
public boolean search(E e) {
// TODO Auto-generated method stub
TreeNode current=root;
while (current!=null) {
if (e.compareTo(current.element)<0) {
current=current.left;
}else if (e.compareTo(current.element)>0) {
current=current.right;
}else
return true;
}
return false;
}
public boolean insert(E e) {
// TODO Auto-generated method stub
if (root==null) {
root=creatTreeNode(e);
}else {
TreeNode parent=null;
TreeNode current = root;
while (current!=null) {
if (e.compareTo(current.element)<0) {
parent=current;
current=current.left;
} else if(e.compareTo(current.element)>0){
parent=current;
current=current.right;
}else
return false;
}
if (e.compareTo(parent.element)<0) {
parent.left=creatTreeNode(e);
}
if (e.compareTo(parent.element)>0) {
parent.right=creatTreeNode(e);
}
}
size++;
return true;
}
public TreeNode creatTreeNode(E e) {
return new TreeNode(e);
}
public void inorder() {
inorder(root);
}
public void inorder(TreeNode root) {
if (root==null) return;
inorder(root.left);
System.out.print(root.element+" ");
inorder(root.right);
}
public void postorder() {
postorder(root);
}
public void po