import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class FindNextNode {
private int [] a ={1,2,3,4,5,6,7,8,9};
private static List<Node> nodeList=null;
//内部类 节点
private static class Node{
Node leftNode;
Node rightNode;
Node parentNode;
int data;
Node(int data){
this.data=data;
leftNode=null;
rightNode=null;
parentNode=null;
}
}
public void creatBinTree(){
nodeList =new LinkedList<Node>();
for(int i=0;i<a.length;i++){
nodeList.add(new Node(a[i]));
}
for(int i=0;i<(a.length-1)/2;i++){
nodeList.get(i).leftNode=nodeList.get(i*2+1);
nodeList.get(i).rightNode=nodeList.get(i*2+2);
}
for(int i=a.length-1;i>0;i--){
nodeList.get(i).parentNode=nodeList.get((i-1)/2);
}
nodeList.get(0).parentNode=null;
}
/**
* 递归方式前,中,后序遍历
* @param node
*/
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data + " ");
preOrderTraverse(node.leftNode);
preOrderTraverse(node.rightNode);
}
public static void inOrderTraverse(Node node){
if(node==null){
return;
}
inOrderTraverse(node.leftNode);
System.out.print(node.data+" ");
inOrderTraverse(node.rightNode);
}
public static void latOrderTraverse(Node node){
if(node==null){
return;
}
latOrderTraverse(node.leftNode);
latOrderTraverse(node.rightNode);
System.out.print(node.data+" ");
}
/**
* 非递归方式前,中,后序遍历
* @param node
* @return
*/
public static void preOrder2(Node node){
Stack<Node> s=new Stack<Node>();
while(node!=null || !s.isEmpty()){
while(node!=null){
System.out.print(node.data+" ");
s.push(node);
node=node.leftNode;
}
if(!s.isEmpty()){
node=s.pop();
node=node.rightNode;
}
}
}
public static void inOrder2(Node node){
Stack<Node> s=new Stack<Node>();
while(node!=null || !s.isEmpty()){
while(node!=null){
s.push(node);
node=node.leftNode;
}
if(!s.isEmpty()){
node=s.pop();
System.out.print(node.data+" ");
node=node.rightNode;
}
}
}
public static void postOrder2(Node node){
Stack<Node> s=new Stack<Node>();
Stack<Integer> s2=new Stack<Integer>();
Integer i=new Integer(1);
while(node!=null || !s.isEmpty()){
while(node!=null){
s.push(node);
s2.push(new Integer(0));
node=node.leftNode;
}
while(!s.isEmpty()&&s2.peek().equals(i)){
s2.pop();
System.out.print(s.pop().data+" ");
}
if(!s.isEmpty()){
s2.pop();
s2.push(new Integer(1));
node=s.peek();
node=node.rightNode;
}
}
}
//发现中序遍历的下一个节点
public static Node getnextnodeininorder(Node node){
if(node==null){
return null;
}
if(node.rightNode!=null){
node=node.rightNode;
while(node.leftNode!=null){
node=node.leftNode;
}
return node;
}else if(node.parentNode!=null){
Node node1=node.parentNode;
if(node==node1.leftNode){
return node1;
}else{
while(node1!=null&&node==node1.rightNode){
node=node1;
node1=node1.parentNode;
}
return node1;
}
}
return null;
}
public static void main(String []args){
FindNextNode bintree=new FindNextNode();
bintree.creatBinTree();
Node rootNode=nodeList.get(0);
System.out.println("前序遍历1");
preOrderTraverse(rootNode);
System.out.println();
System.out.println("前序遍历2");
preOrder2(rootNode);
System.out.println();
System.out.println("中序遍历1");
inOrderTraverse(rootNode);
System.out.println();
System.out.println("中序遍历2");
inOrder2(rootNode);
System.out.println();
System.out.println("后序遍历1");
latOrderTraverse(rootNode);
System.out.println();
System.out.println("后序遍历2");
postOrder2(rootNode);
System.out.println();
System.out.println("---------------");
System.out.println("中序遍历得到下一个节点");
for(int i=0;i<nodeList.size();i++){
Node n1=nodeList.get(i);
if(n1!=null&&getnextnodeininorder(n1)!=null){
System.out.println(n1.data+" "+getnextnodeininorder(n1).data);
}else if(getnextnodeininorder(n1)==null){
System.out.println(n1.data+" "+"null");
}
}
System.out.println("---------------");
}
}