二叉樹的構建【java】
import java.util.LinkedList;
import java.util.Stack;
/**
* Created by akapandaroad on 2017-08-01.
*/
/**
* Created by akapandaroad on 2017-08-01.
*/
public class BinaryTreeNode {
private String data;
private BinaryTreeNode left;
public BinaryTreeNode() {
}
public BinaryTreeNode(int key,String data) {
this.data = data;
this.left = left;
this.right = right;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public BinaryTreeNode getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;
}
public BinaryTreeNode getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
this.right = right;
}
}
public class Test {
public static void main(String[] args) {
LinkedList<BinaryTreeNode> linkTree=new LinkedList<BinaryTreeNode>();
Stack<BinaryTreeNode> stack=new Stack<BinaryTreeNode>();
BinaryTreeNode node1=new BinaryTreeNode(1,"1");
BinaryTreeNode node2=new BinaryTreeNode(2,"2");
BinaryTreeNode node3=new BinaryTreeNode(3,"3");
BinaryTreeNode node4=new BinaryTreeNode(4,"4");
BinaryTreeNode node5=new BinaryTreeNode(5,"5");
BinaryTreeNode node6=new BinaryTreeNode(6,"6");
BinaryTreeNode node7=new BinaryTreeNode(7,"7");
BinaryTreeNode node8=new BinaryTreeNode(8,"7");
BinaryTreeNode root=new BinaryTreeNode();
root=node1;
root.setLeft(node2);
node2.setLeft(node4);
node2.setRight(node5);
root.setRight(node3);
node3.setLeft(node6);
node3.setRight(node7);
// PreOrderRecursion(root);
// System.out.println("");
// InOrderRecursion(root);
// System.out.println("");
// PostOrderRecursion(root);
// System.out.println("");
PreOrderNoRecursion(root,stack);
// InOrderNoRecursion(root,stack);
//PostOrderNoRecursion(root,stack);
}
private static void PreOrderNoRecursion(BinaryTreeNode root, Stack<BinaryTreeNode> stack) {
if(root==null)
System.out.println("root is null");
if(!stack.isEmpty())
stack.removeAllElements();
while(true){
while(root!=null){
System.out.println(root.getData());
stack.push(root);
root=root.getLeft();
}
if(!stack.isEmpty()){
root=stack.pop();
root=root.getRight();
}
}
}
private static void InOrderNoRecursion(BinaryTreeNode root, Stack<BinaryTreeNode> stack) {
if(root==null)
System.out.println("root is null");
if(!stack.isEmpty())
stack.removeAllElements();
while(true){
while(root!=null){
stack.push(root);
root=root.getLeft();
}
if(!stack.isEmpty()){
root=stack.pop();
System.out.println(root.getData());
root=root.getRight();
}
}
}
private static void PostOrderNoRecursion(BinaryTreeNode root, Stack<BinaryTreeNode> stack) {
while(true){
if(root!=null){
stack.push(root);
root=root.getLeft();
}
else{
if(!stack.isEmpty()){
root=stack.pop();
System.out.println(root.getData());
if(stack.peek().getRight()!=null){
System.out.println(stack.peek().getData());
stack.pop();
}
root=stack.peek().getRight();
}
}
}
}
private static void PreOrderRecursion(BinaryTreeNode root) {
if(root!=null){
System.out.println(root.getData());
PreOrderRecursion(root.getLeft());
PreOrderRecursion(root.getRight());
}
}
private static void InOrderRecursion(BinaryTreeNode root) {
if(root!=null){
InOrderRecursion(root.getLeft());
System.out.println(root.getData());
InOrderRecursion(root.getRight());
}
}
private static void PostOrderRecursion(BinaryTreeNode root) {
if(root!=null){
PostOrderRecursion(root.getLeft());
PostOrderRecursion(root.getRight());
System.out.println(root.getData());
}
}
}