package com.Thread.Learn;
import java.util.*;
/*
* author:Tammy Pi
* function:二叉树遍历的递归实现和非递归实现
*/
public class BinaryTree {
private Scanner scan = new Scanner(System.in);
//二叉树
class TreeNode{
String data;
TreeNode lchild;
TreeNode rchild;
public TreeNode(String data){
this.data = data;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public TreeNode getLchild() {
return lchild;
}
public void setLchild(TreeNode lchild) {
this.lchild = lchild;
}
public TreeNode getRchild() {
return rchild;
}
public void setRchild(TreeNode rchild) {
this.rchild = rchild;
}
}
//二叉树的建立
public TreeNode createTree(TreeNode node){
String in = scan.next();
if(in.equals("/")){
return null;
}else{
node = new TreeNode(in);
node.setLchild(createTree(node.getLchild()));
node.setRchild(createTree(node.getRchild()));
return node;
}
}
//递归先序遍历
public void preOrder1(TreeNode root){
if(root!=null){
System.out.print(root.getData()+" ");
preOrder1(root.getLchild());
preOrder1(root.getRchild());
}
}
//非递归先序遍历
public void preOrder2(TreeNode root){
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode currentNode = root;
while(currentNode!=null||!stack.isEmpty()){
while(currentNode!=null){
System.out.print(currentNode.getData()+" ");
stack.push(currentNode);
currentNode = currentNode.lchild;
}
if(!stack.isEmpty()){
currentNode = (TreeNode) stack.pop();
currentNode = currentNode.getRchild();
}
}
}
//递归中序遍历
public void inOrder1(TreeNode root) {
if(root!=null){
inOrder1(root.getLchild());
System.out.print(root.getData()+" ");
inOrder1(root.getRchild());
}
}
//非递归中序遍历
public void inOrder2(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode currentNode = root;
while(currentNode!=null||!stack.isEmpty()) {
while(currentNode!=null){
stack.push(currentNode);
currentNode = currentNode.getLchild();
}
if(!stack.isEmpty()){
currentNode = stack.pop();
System.out.print(currentNode.getData()+" ");
currentNode = currentNode.getRchild();
}
}
}
//递归后序遍历
public void postOrder1(TreeNode root) {
if(root!=null){
postOrder1(root.getLchild());
postOrder1(root.getRchild());
System.out.print(root.getData()+" ");
}
}
//非递归后序遍历
public void postOrder2(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode currentNode = null;
TreeNode preNode = null;
stack.push(root);
while(!stack.isEmpty()){
currentNode = stack.pop();
if((currentNode.getLchild()==null&¤tNode.getRchild()==null)||(preNode!=null&&(preNode.equals(currentNode.getLchild())||preNode.equals(currentNode.getRchild())))){
System.out.print(currentNode.getData()+" ");
preNode = currentNode;
}else{
stack.push(currentNode);
if(currentNode.getRchild()!=null){
stack.push(currentNode.getRchild());
}
if(currentNode.getLchild()!=null){
stack.push(currentNode.getLchild());
}
}
}
}
//层次遍历
public void levelOrder(TreeNode root){
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while(!queue.isEmpty()){
int num = queue.size();
for(int i=0;i<num;i++){
TreeNode node = queue.remove(0);
System.out.print(node.getData()+" ");
if(node.getLchild()!=null){
queue.add(node.getLchild());
}
if(node.getRchild()!=null){
queue.add(node.getRchild());
}
}
}
}
//计算树的深度
public int treeHeight(TreeNode root){
if(root==null){
return 0;
}else{
int lHeight = treeHeight(root.getLchild())+1;
int rHeight = treeHeight(root.getRchild())+1;
if(lHeight>rHeight){
return lHeight;
}else{
return rHeight;
}
}
}
public static void main(String[] args){
BinaryTree tree = new BinaryTree();
TreeNode root = null;
System.out.println("请输入树的节点序列:");
root = tree.createTree(root);
System.out.println("树的递归先序遍历的结果:");
tree.preOrder1(root);
System.out.println();
System.out.println("树的非递归先序遍历的结果:");
tree.preOrder2(root);
System.out.println();
System.out.println("树的递归中序遍历的结果:");
tree.inOrder1(root);
System.out.println();
System.out.println("树的非递归中序遍历的结果:");
tree.inOrder2(root);
System.out.println();
System.out.println("树的递归后序遍历的结果:");
tree.postOrder1(root);
System.out.println();
System.out.println("树的非递归后序遍历的结果:");
tree.postOrder2(root);
System.out.println();
System.out.println("树的层次遍历的结果:");
tree.levelOrder(root);
System.out.println();
System.out.print("树的深度为:");
System.out.println(tree.treeHeight(root));
}
}
这种基本功,应该每天写一遍,写到完全熟练为止。