package algorithm.tree;
import java.util.Stack;
class Node{
private int key;
private Node leftNode,rightNode;
public Node(int key,Node leftNode,Node rightNode){
this.key = key;
this.leftNode = leftNode;
this.rightNode = rightNode;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
public Node getRightNode() {
return rightNode;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
}
public class BinTree {
private Node rootNode;
public BinTree(Node rootNode){
this.rootNode = rootNode;
}
public Node getRootNode() {
return rootNode;
}
public static void visit(Node node) {
System.out.print(node.getKey()+" ");
}
/**
* 前序遍历
* @param rootNode
*/
public static void PreOrder(Node rootNode){
if(rootNode!=null){
visit(rootNode);
PreOrder(rootNode.getLeftNode());
PreOrder(rootNode.getRightNode());
}
}
/**
* 中序遍历
* @param rootNode
*/
public static void InOrder(Node rootNode){
if(rootNode!=null){
InOrder(rootNode.getLeftNode());
visit(rootNode);
InOrder(rootNode.getRightNode());
}
}
/**
* 非递归中序遍历
* @param rootNode
*/
public static void IterativeInOrder(Node rootNode){
Stack<Node> stack = new Stack<Node>();
Node n = rootNode;
while(n!=null || !stack.isEmpty()){
if(n!=null){
stack.push(n);
n = n.getLeftNode();
}else{
n = stack.pop();
visit(n);
n = n.getRightNode();
}
}
}
/**
* 后续遍历
* @param rootNode
*/
public static void PostOrder(Node rootNode){
if(rootNode!=null){
PostOrder(rootNode.getLeftNode());
PostOrder(rootNode.getRightNode());
visit(rootNode);
}
}
/**
* 递归二叉树查找
* @param node
* @param k
* @return
*/
public static Node TreeSearch(Node node,int k){
if(k==node.getKey())
return node;
if(k<node.getKey()){
return TreeSearch(node.getLeftNode(),k);
}else{
return TreeSearch(node.getRightNode(),k);
}
}
/**
* 非递归二叉树查找
* @param node
* @param k
* @return
*/
public static Node IterativeTreeSearch(Node node,int k){
while(k!=node.getKey()){
if(k<node.getKey()){
node = node.getLeftNode();
}else{
node = node.getRightNode();
}
}
return node;
}
/**
* 最小元素
* @param node
* @return
*/
public static Node TreeMinimum(Node node){
while(node.getLeftNode()!=null){
node = node.getLeftNode();
}
return node;
}
/**
* 最大元素
* @param node
* @return
*/
public static Node TreeMaximum(Node node){
while(node.getRightNode()!=null){
node = node.getRightNode();
}
return node;
}
public static void main(String[] args) {
Node g = new Node(4,null,null);
Node d = new Node(3,null,g);
Node b = new Node(6,d,null);
Node e = new Node(17,null,null);
Node f = new Node(20,null,null);
Node c = new Node(18,e,f);
Node rootNode = new Node(15,b,c);
BinTree binTree = new BinTree(rootNode);
System.out.println("递归中序遍历");
BinTree.InOrder(rootNode);
System.out.println();
System.out.println("非递归中序遍历");
BinTree.IterativeInOrder(rootNode);
System.out.println();
Node node1 = BinTree.TreeSearch(rootNode, 17);
System.out.println("递归查找"+node1.getKey());
Node node2 = BinTree.TreeSearch(rootNode, 6);
System.out.println("非递归查找"+node2.getKey());
Node minNode = BinTree.TreeMinimum(rootNode);
System.out.println("最小元素为:"+minNode.getKey());
Node maxNode = BinTree.TreeMaximum(rootNode);
System.out.println("最小元素为:"+maxNode.getKey());
}
}