??java???????????????????????
???????????? ???????[ 2013/2/6 9:48:31 ] ????????
?????????????????????????????????????????????????????????н??????С??y?????????????н??????????y??????
??????????????????????????????????????????????
????????????????????????????????????????????????????????????????????????С?????????????????????????????????????????????????????
?????????????????????????????????????????????????
???????????????????????廹?ж?????????塣
package binaryTree;
public class BinaryTree {
// ???????
private static class TreeNode {
Object element;
TreeNode left;
TreeNode right;
public TreeNode(Object o) {
element = o;
}
}
private TreeNode root;
private int size = 0;
public BinaryTree() {
}
public BinaryTree(Object[] objects) {
for (int i = 0; i < objects.length; i++) {
insert(objects[i]);
}
}
public boolean insert(Object o) {
if (root == null) {
root = new TreeNode(o);
} else {
TreeNode parent = null;
TreeNode current = root;
while (current != null) {
if (((Comparable) o).compareTo(current.element) < 0) {
parent = current;
current = current.left;
} else if (((Comparable) o).compareTo(current.element) > 0) {
parent = current;
current = current.right;
} else {
return false;
}
}
if (((Comparable) o).compareTo(parent.element) < 0) {
parent.left = new TreeNode(o);
} else {
parent.right = new TreeNode(o);
}
}
size++;
return true;
}
//???????
public void inorder(){
inorder(root);
}
public void inorder(TreeNode root){
if (root == null) {
return;
}
inorder(root.left);
System.out.println(root.element + " ");
inorder(root.right);
}
//???????
public void postorder(){
postorder(root);
}
public void postorder(TreeNode root){
if (root == null) {
return;
}
postorder(root.left);
postorder(root.right);
System.out.println(root.element +" ");
}
//??????
public void preorder(){
preorder(root);
}
public void preorder(TreeNode root){
if (root == null) {
return;
}
System.out.println(root.element + " ");
preorder(root.left);
preorder(root.right);
}
public int getSize(){
return size;
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11