import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BinaryTree {
public BinTreeNode root;
public BinaryTree() {
root = null;
}
public BinaryTree(Object obj, BinaryTree left, BinaryTree right) {
BinTreeNode l = null, r = null;
if (left == null)
l = null;
else
l = left.root;
if (right == null)
r = null;
else
r = right.root;
}
public boolean root(Object x) { // 返回根节是否存在
if (root != null) {
x = root.getData();
return true;
} else
return false;
}
public void Make(Object obj, BinaryTree left, BinaryTree right) { // 创建二叉树
if (root != null || left == right)
return;
root = new BinTreeNode(obj, left.root, right.root);
left.root = null;
right.root = null;
}
public void BreakTree(Object obj, BinaryTree left, BinaryTree right) {
if (root == null || left == right || left.root != null
|| right.root != null) {
return;
}
obj = root.getData();
left.root = root.getlChild();
right.root = root.getlChild();
// delete root;
root = null;
}
/**
* 二叉树的遍历(递归)
* */
public static void PreOder(BinTreeNode r, Visit vs) { // 前序遍历二叉树
if (r != null) {
vs.print(r.getData());
PreOder(r.getlChild(), vs);
PreOder(r.getrChild(), vs);
}
}
public static void InOder(BinTreeNode r, Visit vs) { // 中序遍历二叉树
if (r != null) {
InOder(r.getlChild(), vs);
vs.print(r.getData());
InOder(r.getrChild(), vs);
}
}
public static void PostOrder(BinTreeNode r, Visit vs) { // 后序遍历二叉树
if (r != null) {
PostOrder(r.getlChild(), vs);
PostOrder(r.getrChild(), vs);
vs.print(r.getData());
}
}
/**
* 二叉树的遍历(非递归)
* */
// 层次二叉树的遍历(广度优先搜索算法(BFS))
public static void LevelOrder(BinTreeNode r, Visit vs) throws Exception {
Queue<BinTreeNode> q = new LinkedList<BinTreeNode>();
if (r == null)
return;
BinTreeNode current;
q.add(r);
while (!q.isEmpty()) {
current = (BinTreeNode) q.remove();
vs.print(current.getData());
if (current.getlChild() != null)
q.add(current.getlChild());
if (current.getrChild() != null)
q.add(current.getrChild());
}
}
//前序遍历二叉树(深度优先搜索算法(DFS))
public static void preOrderNonRecu(BinTreeNode r, Visit vs)
throws Exception {
Stack<BinTreeNode> s = new Stack<BinTreeNode>();
if (r == null) {
return;
}
BinTreeNode current;
s.push(r);
while (!s.isEmpty()) {
current = s.pop();
vs.print(current.getData());
if (current.getrChild() != null)
s.push(current.getrChild());
if (current.getlChild() != null)
s.push(current.getlChild());
}
}
}
class Visit {
public void print(Object item) {
System.out.println(item + " ");
}
}
class BinTreeNode { // 二叉树节点类
private BinTreeNode lChild; // 左孩子节点对象引用
private BinTreeNode rChild; // 右孩子节点对象引用
public Object data; // 数据元素
public BinTreeNode() {
this.lChild = lChild;
this.rChild = rChild;
}
public BinTreeNode(Object obj, BinTreeNode left, BinTreeNode right) {
this.lChild = left;
this.rChild = right;
this.data = obj;
}
public BinTreeNode(Object item) {
data = item;
lChild = null;
rChild = null;
}
public BinTreeNode getlChild() {
return lChild;
}
public void setlChild(BinTreeNode lChild) {
this.lChild = lChild;
}
public BinTreeNode getrChild() {
return rChild;
}
public void setrChild(BinTreeNode rChild) {
this.rChild = rChild;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
public class Start {
public static void main(String[] args) throws Exception {
BinaryTree r = new BinaryTree();
BinTreeNode rl = new BinTreeNode("hehe_left");
BinTreeNode rl_l = new BinTreeNode("hehe_left_l");
BinTreeNode rl_r = new BinTreeNode("hehe_left_r");
rl.setlChild(rl_l);
rl.setrChild(rl_r);
BinTreeNode rr = new BinTreeNode("hehe_right");
BinTreeNode rr_l = new BinTreeNode("hehe_right_l");
BinTreeNode rr_r = new BinTreeNode("hehe_right_r");
rr.setlChild(rr_l);
rr.setrChild(rr_r);
r.root = new BinTreeNode("hehe", rl, rr);
r.LevelOrder(r.root, new Visit());
System.out.println();
r.PreOder(r.root, new Visit());
}
}