二叉树
首先看节点的定义
package per.lihao.tree;
/**
* @author : LiHao
* @date : 2018/12/4 9:59
*/
public class TreeNode {
/**
* 关键字
*/
private int data;
/**
* 左子树节点
*/
private TreeNode lnode;
/**
* 右子树节点
*/
private TreeNode rnode;
/**
* 父节点
*/
private TreeNode parent;
/**
* 为了循环-后序遍历 记录
*/
private boolean stackFirst = false;
public TreeNode getParent() {
return parent;
}
public void setParent(TreeNode parent) {
this.parent = parent;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public TreeNode getLnode() {
return lnode;
}
public void setLnode(TreeNode lnode) {
this.lnode = lnode;
}
public TreeNode getRnode() {
return rnode;
}
public void setRnode(TreeNode rnode) {
this.rnode = rnode;
}
public boolean isStackFirst() {
return stackFirst;
}
public void setStackFirst(boolean stackFirst) {
this.stackFirst = stackFirst;
}
public TreeNode(){}
public TreeNode(int k){
data = k;
}
}
目前的二叉树包含了前序、中序、后序遍历的循环版本、递归版本的实现,后序会在平衡二叉树中体现创建、删除、层次遍历等
package per.lihao.tree;
import java.util.Stack;
/**
* 二叉树
* @author : LiHao
* @date : 2018/12/26 10:41
*/
public class BinaryTree {
/**
* 根节点
*/
protected TreeNode root;
public BinaryTree(){}
/**
* 构造函数
* @param node
*/
public BinaryTree(TreeNode node){
root = node;
}
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
/**
* 递归
* 中序遍历根节点
*/
public void inOrderTraversalRecursion(){
inOrderTraversalRecursion(root);
System.out.println();
}
/**
* 递归
* 中序遍历
* @param node
*/
protected void inOrderTraversalRecursion(TreeNode node){
if (node==null){
return;
}
inOrderTraversalRecursion(node.getLnode());
System.out.print(node.getData()+" ");
inOrderTraversalRecursion(node.getRnode());
}
/**
* 循环
* 中序遍历根节点
*/
public void inOrderTraversalLoop(){
inOrderTraversalLoop(root);
System.out.println();
}
/**
* 循环
* 中序遍历
* @param node
*/
protected void inOrderTraversalLoop(TreeNode node){
Stack<TreeNode> stack = new Stack<>();
TreeNode currentNode = node;
TreeNode temp;
// 一直往左找,同时沿线的节点放入栈,直到最左边的子节点为空
// 出栈,并打印该节点,然后转向该节点的右子节点
while (currentNode!=null || !stack.isEmpty()){
while (currentNode!=null){
stack.push(currentNode);
currentNode = currentNode.getLnode();
}
if (!stack.isEmpty()){
temp = stack.pop();
System.out.print(temp.getData() + " ");
currentNode = temp.getRnode();
}
}
}
/**
* 递归
* 前序遍历根节点
*/
public void preOrderTraversalRecursion(){
preOrderTraversalRecursion(root);
System.out.println();
}
/**
* 递归
* 前序遍历
* @param node
*/
protected void preOrderTraversalRecursion(TreeNode node){
if (node==null){
return;
}
System.out.print(node.getData() + " ");
preOrderTraversalRecursion(node.getLnode());
preOrderTraversalRecursion(node.getRnode());
}
/**
* 循环
* 前序遍历根节点
*/
public void preOrderTraversalLoop(){
preOrderTraversalLoop(root);
System.out.println();
}
/**
* 循环
* 前序遍历
* @param node
*/
protected void preOrderTraversalLoop(TreeNode node){
Stack<TreeNode> stack = new Stack<>();
TreeNode currentNode = node;
TreeNode temp;
// 一直往左找,同时打印沿线的节点并放入栈,直到最左边的子节点为空
// 出栈,然后转向该节点的右子节点
while (currentNode!=null || !stack.isEmpty()){
while (currentNode!=null){
System.out.print(currentNode.getData() + " ");
stack.push(currentNode);
currentNode = currentNode.getLnode();
}
if (!stack.isEmpty()){
temp = stack.pop();
currentNode = temp.getRnode();
}
}
}
/**
* 递归
* 后序遍历根节点
*/
public void postOrderTraversalRecursion(){
postOrderTraversalRecursion(root);
System.out.println();
}
/**
* 递归
* 后序遍历
* @param node
*/
protected void postOrderTraversalRecursion(TreeNode node){
if (node==null){
return;
}
postOrderTraversalRecursion(node.getLnode());
postOrderTraversalRecursion(node.getRnode());
System.out.print(node.getData() + " ");
}
/**
* 循环
* 后序遍历根节点
*/
public void postOrderTraversalLoop(){
postOrderTraversalLoop(root);
System.out.println();
}
/**
* 循环
* 后续遍历
* @param node
*/
public void postOrderTraversalLoop(TreeNode node){
Stack<TreeNode> stack = new Stack<>();
TreeNode currentNode = node;
TreeNode temp;
// 先一直往左找,沿线节点放入栈,且节点处的stackfirst=false
// 当遇到左孩子为空时,取其右孩子继续寻找,此时父节点的stackfirst=true
// 为true时打印节点
while (currentNode!=null || !stack.isEmpty()){
while (currentNode!=null){
stack.push(currentNode);
currentNode = currentNode.getLnode();
}
if (!stack.isEmpty()){
// 不取出节点,先判断其stackfirst值
temp = stack.peek();
if (temp.isStackFirst()){
temp = stack.pop();
System.out.print(temp.getData() + " ");
}else {
temp.setStackFirst(true);
currentNode = temp.getRnode();
}
}
}
}
/**
* 生成测试树
* 10
* / \
* 3 12
* /
* 34
* \
* 23
* @return node
*/
public static TreeNode makeTreeTest(){
TreeNode root = new TreeNode(10);
root.setLnode(new TreeNode(3));
TreeNode node1 = new TreeNode(12);
TreeNode node2 = new TreeNode(34);
TreeNode node3 = new TreeNode(23);
node1.setLnode(node2);
node2.setRnode(node3);
root.setRnode(node1);
return root;
}
}