1.1 前序遍历
/**
* 前序遍历
*/
private void preOrder(BtNode root) {
if (root == null) {
return;
}
System.out.print(root.getDate() + " ");
preOrder(root.getLeftChild());
preOrder(root.getRightChild());
}
public void preOrder() {
preOrder(root);
System.out.println();
}
测试类
public class TestBinaryTree {
public static void main(String[] args) {
String str = "ABC##DE##F##G#H##";
char[] arr = str.toCharArray();
BinaryTree mytree = new BinaryTree();
mytree.createTree(arr);
System.out.println("-----前序遍历-----");
mytree.preOrder();
}
1.2中序遍历
/**
* 中序遍历
*/
private void inOrder(BtNode root) {
if (root == null) {
return;
}
inOrder(root.getLeftChild());
System.out.print(root.getDate() + " ");
inOrder(root.getRightChild());
}
public void inOrder() {
inOrder(root);
System.out.println();
}
测试类
public class TestBinaryTree {
public static void main(String[] args) {
String str = "ABC##DE##F##G#H##";
char[] arr = str.toCharArray();
BinaryTree mytree = new BinaryTree();
mytree.createTree(arr);
System.out.println("-----中序遍历-----");
mytree.inOrder();
}
}
1.3后序遍历
/**
*后序遍历
*/
private void postOrder(BtNode root) {
if (root == null) {
return;
}
postOrder(root.getLeftChild());
postOrder(root.getRightChild());
System.out.print(root.getDate() + " ");
}
public void postOrder() {
postOrder(root);
System.out.println();
}
测试类
public class TestBinaryTree {
public static void main(String[] args) {
String str = "ABC##DE##F##G#H##";
char[] arr = str.toCharArray();
BinaryTree mytree = new BinaryTree();
mytree.createTree(arr);
System.out.println("-----后序遍历-----");
mytree.postOrder();
}
}