public class BTreeNode {
BTreeNode left,right;
char element;
public BTreeNode(char obj){
element=obj;
}
}
public interface BTreeInterface {
void createBTree(String str);
void printBTree(BTreeNode root);
void inOrder(BTreeNode root);
void preOrder(BTreeNode root);
void postOrder(BTreeNode root);
void depthFirstSearch(BTreeNode root);
void breadFirstSearch(BTreeNode root);
}
public class BinaryTree implements BTreeInterface{
static BTreeNode root;
public BinaryTree(){
root=null;
}
public static void main(String[] args){
BinaryTree bt=new BinaryTree();
String str="A(B(D,E),C(,F))";
bt.createBTree(str);
System.out.print("中序遍历:");
bt.inOrder(root);
System.out.println();
System.out.print("先序遍历:");
bt.preOrder(root);
System.out.println();
System.out.print("后序遍历:");
bt.postOrder(root);
System.out.println();
System.out.print("深度遍历:");
bt.depthFirstSearch(root);
System.out.println();
System.out.print("广度遍历:");
bt.breadFirstSearch(root);
System.out.println();
//bt.printBTree(root);
}
public void createBTree(String str) {
// TODO Auto-generated method stub
Stack stack=new Stack();
int k=1;
BTreeNode p=null;
char[] ch=str.toCharArray();
for(int i=0;i<ch.length;i++){
switch (ch[i]) {
case ' ':
break;
case ',':
k=2;
break;
case '(':
k=1;
stack.push(p);
break;
case ')':
if (stack.isEmpty()) {
System.out.println("输入的字符串广义表不能描述一颗二叉树,因为括号不匹配");
System.exit(1);
}
stack.pop();
break;
default:
p=new BTreeNode(ch[i]);
if(root==null){
root=p;
}else{
if(k==1){
((BTreeNode)stack.peek()).left=p;
}else {
((BTreeNode)stack.peek()).right=p;
}
}
break;
}
}
}
public void printBTree(BTreeNode root) {
// TODO Auto-generated method stub
while(root!=null){
System.out.print(root.element);
if(root.left!=null){
System.out.print("(");
System.out.print(root.left.element);
root=root.left;
}else {
System.out.print(",");
if(root.right!=null){
System.out.print(root.right.element);
root=root.right;
}
}
}
}
public void inOrder(BTreeNode root) {
// TODO Auto-generated method stub
if (root!=null) {
inOrder(root.left);
System.out.print(root.element);
inOrder(root.right);
}
}
public void preOrder(BTreeNode root) {
// TODO Auto-generated method stub
if(root!=null){
System.out.print(root.element);
preOrder(root.left);
preOrder(root.right);
}
}
public void postOrder(BTreeNode root) {
// TODO Auto-generated method stub
if(root!=null){
preOrder(root.left);
preOrder(root.right);
System.out.print(root.element);
}
}
public void depthFirstSearch(BTreeNode root) {
// TODO Auto-generated method stub
if(root!=null)
System.out.print(root.element);
if(root.left!=null)
depthFirstSearch(root.left);
if(root.right!=null)
depthFirstSearch(root.right);
}
public void breadFirstSearch(BTreeNode root) {
// TODO Auto-generated method stub
Queue queue=new LinkedList();
BTreeNode p;
if(root!=null){
System.out.print(root.element);
queue.add(root);
}
while (!queue.isEmpty()) {
p=(BTreeNode)queue.remove();
if(p.left!=null){
System.out.print(p.left.element);
queue.add(p.left);
}
if(p.right!=null){
System.out.print(p.right.element);
queue.add(p.right);
}
}
}
}