測試二叉樹遍曆,遞迴演算法
| 代碼如下 |
複製代碼 |
|
public class TestBinaryTree {
public static void main(String[] args) {
Node<String> g = new Node<String>("G", null, null);
Node<String> e = new Node<String>("E", null, null);
Node<String> f = new Node<String>("F", null, null);
Node<String> d = new Node<String>("D", null, g);
Node<String> b = new Node<String>("B", d, e);
Node<String> c = new Node<String>("C", null, f);
Node<String> a = new Node<String>("A", b, c);
System.out.println("產生的二叉樹:");
System.out.println(" A");
System.out.println(" | ");
System.out.println(" |---------|");
System.out.println(" B C");
System.out.println(" | |");
System.out.println(" |---------| -----|");
System.out.println(" D E F");
System.out.println(" |");
System.out.println(" ----|");
System.out.println(" G");
System.out.println("二叉樹深度:" + BinaryTree.getDepth(a));
System.out.print("前序走訪:");
BinaryTree.priorderTraversal(a);
System.out.println();
System.out.print("中序遍曆:");
BinaryTree.inorderTraversal(a);
System.out.println();
System.out.print("後序遍曆:");
BinaryTree.postorderTraversal(a);
System.out.println();
}
}
// 二叉樹
class BinaryTree {
// 前序走訪
static <T> void priorderTraversal(Node<T> node) {
if (node != null) {
visitNode(node);
priorderTraversal(node.getLeftChild());
priorderTraversal(node.getRightChild());
}
}
// 中序遍曆
static <T> void inorderTraversal(Node<T> node) {
if (node != null) {
inorderTraversal(node.getLeftChild());
visitNode(node);
inorderTraversal(node.getRightChild());
}
}
// 後序遍曆
static <T> void postorderTraversal(Node<T> node) {
if (node != null) {
postorderTraversal(node.getLeftChild());
postorderTraversal(node.getRightChild());
visitNode(node);
}
}
// 二叉樹深度
static <T> int getDepth(Node<T> node) {
if (node == null) {
return 0;
}
int leftDepth = 0;
int rightDepth = 0;
leftDepth = getDepth(node.getLeftChild());
rightDepth = getDepth(node.getRightChild());
return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
}
// 訪問節點
static <T> void visitNode(Node<T> node) {
System.out.print(node.getKey() + " ");
}
}
// 節點
class Node<T> {
private T key;
private Node<T> leftChild;
private Node<T> rightChild;
public Node() {
}
public Node(T key, Node<T> leftChild, Node<T> rightChild) {
super(); www.2cto.com
this.key = key;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
public T getKey() {
return key;
}
public void setKey(T key) {
this.key = key;
}
public Node<T> getLeftChild() {
return leftChild;
}
public void setLeftChild(Node<T> leftChild) {
this.leftChild = leftChild;
}
public Node<T> getRightChild() {
return rightChild;
}
public void setRightChild(Node<T> rightChild) {
this.rightChild = rightChild;
}
} |
非遞迴演算法
當建立好二叉樹類後可以建立二叉樹執行個體,並實現二叉樹的先根遍曆,中根遍曆,後根遍曆,代碼如下:
| 代碼如下 |
複製代碼 |
|
package package2;
public class BinaryTreePreorder {
public static void preOrder(BinaryTree root){ //先根遍曆
if(root!=null){
System.out.print(root.data+"-");
preOrder(root.left);
preOrder(root.right);
}
}
public static void inOrder(BinaryTree root){ //中根遍曆
if(root!=null){
inOrder(root.left);
System.out.print(root.data+"--");
inOrder(root.right);
}
}
public static void postOrder(BinaryTree root){ //後根遍曆
if(root!=null){
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data+"---");
}
}
public static void main(String[] str){
int[] array = {12,76,35,22,16,48,90,46,9,40};
BinaryTree root = new BinaryTree(array[0]); //建立二叉樹
for(int i=1;i<array.length;i++){
root.insert(root, array[i]); //向二叉樹中插入資料
}
System.out.println("先根遍曆:");
preOrder(root);
System.out.println();
System.out.println("中根遍曆:");
inOrder(root);
System.out.println();
System.out.println("後根遍曆:");
postOrder(root); |