package cn.datastruct.tree;
import java.util.Stack;
public class StackTree {
static Node root;
class Node {
private int val;
private Node rchild;
private Node lchild;
public Node(int val) {
this.val = val;
}
}
boolean insert(int val) {//添加树节点
Node node = new Node(val);
if (root == null) {
root = node;
return true;
} else {
Node cur = root;
while (true) {
Node newNode = new Node(val);
if (cur.val > val) {
if (cur.lchild == null) {
cur.lchild = newNode;
return true;
}
cur = cur.lchild;
} else if (cur.val < val) {
if (cur.rchild == null) {
cur.rchild = newNode;
return true;
}
cur = cur.rchild;
} else {
return false;
}
}
}
}
/**
* 先序
* @param node
*/
static void preorder(Node node) {
if (node == null)
return;
Stack<Node> stack = new Stack<>();
Node cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
System.out.print(cur.val);//输出根
stack.push(cur);//进栈
cur = cur.lchild;//遍历左子树
}
cur = stack.pop();
cur = cur.rchild;
}
}
/**
* 中序
* @param node
*/
static void inorder(Node node) {
if (node == null)
return;
Stack<Node> stack = new Stack<StackTree.Node>();
Node cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.lchild;
}
cur = stack.pop();
System.out.println(cur.val);
cur = cur.rchild;
}
}
/**
* 后序,双栈法
* @param node
*/
static void postorder1(Node node) {
if(root==null) return ;
Stack<Node> stack = new Stack<StackTree.Node>();
Stack<Node> stack1 = new Stack<StackTree.Node>();
Node cur = root;
while(cur!=null||!stack.isEmpty()) {
while(cur!=null) {
stack1.push(cur);
stack.push(cur);
cur = cur.rchild;
}
cur = stack.pop();
cur = cur.lchild;
}
int n = stack1.size();
for (int i = 0; i < n; i++) {
System.out.print(stack1.pop().val);
}
}
/**
* 后序,单栈标记法
* @param node
*/
static void postorder(Node node) {
if (root == null)
return;
Stack<Node> stack = new Stack<StackTree.Node>();
Node cur = root;
Node t=null;//标识是否已经出栈
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.lchild;
}//cur.left==null
cur=stack.peek();//取栈顶元素
if(cur.rchild==null||t==cur.rchild) {//右子树为空或者右子树已经处理过
t=stack.pop();//出栈,标识是否已经处理过
System.out.print(t.val);
cur=null;
}else {
cur=cur.rchild;
}
}
}
static void preOrder(Node node) {
if (node == null)
return;
System.out.print(node.val);
preOrder(node.lchild);
preOrder(node.rchild);
}
static void inOrder(Node node) {
if (node == null)
return;
inOrder(node.lchild);
System.out.print(node.val);
inOrder(node.rchild);
}
static void postOrder(Node node) {
if (node == null)
return;
postOrder(node.lchild);
postOrder(node.rchild);
System.out.print(node.val);
}
public static void main(String[] args) {
StackTree tree = new StackTree();
int[] a = { 2, 5, 1, 3, 9, 8, 7, 4, 6 };
for (int i = 0; i < a.length; i++) {
tree.insert(a[i]);
}
preOrder(root);
System.out.println();
preorder(root);
System.out.println();
inOrder(root);
System.out.println();
inorder(root);
postOrder(root);
System.out.println();
postorder(root);
System.out.println();
postorder1(root);
}
}