package learn.java.com.java.datastru;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class BinarySearchTree {
public static void main(String[] args){
BinarySortTree<Integer> BST = new BinarySortTree<Integer>();
BST.insertNode(35);
BST.insertNode(20);
BST.insertNode(15);
BST.insertNode(16);
BST.insertNode(29);
BST.insertNode(28);
BST.insertNode(30);
BST.insertNode(40);
BST.insertNode(50);
BST.insertNode(45);
BST.insertNode(55);
System.out.print("先序遍历(递归):");
BST.preRec(BST.getRoot());
System.out.println();
System.out.print("中序遍历(递归):");
BST.midRec(BST.getRoot());
System.out.println();
System.out.print("后序遍历(递归):");
BST.postRec(BST.getRoot());
System.out.println();
System.out.print("先序遍历(非递归):");
BST.preNoRec(BST.getRoot());
System.out.println();
System.out.print("中序遍历(非递归):");
BST.midNoRec(BST.getRoot());
System.out.println();
System.out.print("后序遍历(非递归):");
System.out.println(BST.postNoRec(BST.getRoot()).toString());
System.out.println();
}
}
//结点构造
class Node<E extends Comparable<E>>{
E nodeValue;
Node<E> left;
Node<E> right;
public Node(E value){
this.nodeValue=value;
left=right=null;
}
}
/*/
创建二叉查找树
*/
class BinarySortTree<E extends Comparable<E>>{
private Node<E> root;
BinarySortTree(){
root=null;
}
public void insertNode(E value){
if(root==null){
root=new Node<E>(value);
return;
}
Node<E> curr=root;
while(true){
if (value.compareTo(curr.nodeValue)>0){
if(curr.right==null){
curr.right=new Node<E>(value);
return;
}
curr=curr.right;
}
else{
if(curr.left==null){
curr.left=new Node<E>(value);
return;
}
curr=curr.left;
}
}
}
public Node<E> getRoot(){
return root;
}
/*
先序遍历二叉树(递规)
*/
public void preRec(Node<E> root){
if(root!=null){
System.out.print(root.nodeValue+" ");
preRec(root.left);
preRec(root.right);
}
}
/**
* 中序遍历(递 规)
*/
public void midRec(Node<E> root){
if(root!=null){
midRec(root.left);
System.out.print( root.nodeValue +" ");
midRec(root.right);
}
}
/**
* 后序遍历(递 规)
*/
public void postRec(Node<E> root){
if(root!=null){
postRec(root.left);
postRec(root.right);
System.out.print(root.nodeValue+" ");
}
}
/**
* 先序非递规
*/
public void preNoRec(Node<E> root){
Stack<Node<E>> stack=new Stack<Node<E>>();
Node<E> p=root;
if(p==null)
return;
stack.push(p);
while(!stack.isEmpty()){
p=(Node<E>)stack.pop();
System.out.print(p.nodeValue+" ");
if(p.right!=null)
stack.push(p.right);
if(p.left!=null)
stack.push(p.left);
}
}
/**
* 中序非递 规
*/
public void midNoRec(Node<E> root){
Stack<Node<E>> stack =new Stack<Node<E>>();
Node<E> curr=root;
while(curr!=null||!stack.isEmpty()){
while(curr!=null){
stack.push(curr);
curr=curr.left;
}
curr=stack.pop();
System.out.print(curr.nodeValue+" ");
curr=curr.right;
}
}
/**
* 后序遍历非递规
*/
public List<E> postNoRec(Node<E> root){
Stack<Node> stack =new Stack<Node>();
LinkedList<E> resul=new LinkedList<E>();
Node<E> p=root;
if (root==null)
return null ;
stack.push(p);
while(!stack.isEmpty()){
p=stack.pop();
resul.add(p.nodeValue);
if(p.left!=null)
stack.push(p.left);
if(p.right!=null)
stack.push(p.right);
}
Collections.reverse(resul);
return resul;
}
}
先序遍历(递归):35 20 15 16 29 28 30 40 50 45 55
中序遍历(递归):15 16 20 28 29 30 35 40 45 50 55
后序遍历(递归):16 15 28 30 29 20 45 55 50 40 35
先序遍历(非递归):35 20 15 16 29 28 30 40 50 45 55
中序遍历(非递归):15 16 20 28 29 30 35 40 45 50 55
后序遍历(非递归):[16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]
参考:https://www.jb51.net/article/140486.htm