import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.Stack;
import java.util.Vector;
import java.util.concurrent.LinkedBlockingQueue;
//2叉树常用操作练习
public class BinaryTreeTest {
//TEST
public static void main(String[] args) {
BinaryTree<Integer> tree = null;
int[] temp = {5,8,4,7,3,1,6,9,0,2};
for(int i = 0 ; i < temp.length ; i++){
tree = buildTree(temp[i],tree);
}
preIteratorTree(tree);
midIteratorTree(tree);
postIteratorTreeOne(tree);
layoutIteratorTree(tree);
preIteratorTreeRecursion(tree);
System.out.println();
midIteratorTreeRecursion(tree);
System.out.println();
postIteratorTreeRecursion(tree);
System.out.println();
Integer count = treeLeafCount(tree,0);
System.out.println();
System.out.println(count);
System.out.println(treeHeight(tree));
exchangeTreeNode(tree);
preIteratorTree(tree);
System.out.println("isExist = " + nodeIsExist(tree,3));
System.out.println("isExist = " + nodeIsExist(tree,-1));
BinaryTree<Integer> node = nearestCommonParent(tree,6,9);
if(node == null) System.out.println("null");
else System.out.println(node.data);
node = nearestCommonParent(tree,2,-1);
if(node == null) System.out.println("null");
else System.out.println(node.data);
int[] at = {1,2,3,4,5,6,7,8,9,10};
BinaryTree<Integer> tree_ = buildBalanceTree(at,0,at.length-1,null);
midIteratorTreeRecursion(tree_);
System.out.println();
Vector<BinaryTree<Integer>> vector = new Vector<BinaryTree<Integer>>();
printPath(tree,22,vector);
for(int i = 0 ; i < vector.size() ; i++){
BinaryTree<Integer> temp_ = vector.get(i);
Vector<BinaryTree<Integer>> vectori = new Vector<BinaryTree<Integer>>();
nodePath(tree,temp_.data,vectori);
Collections.reverse(vectori);
for(int j = 0 ; j < vectori.size() ; j++){
System.out.print(vectori.get(j).data + " ");
}
System.out.println();
}
}
//先序遍历递归
private static void preIteratorTreeRecursion(BinaryTree<Integer> node){
if(node != null){
System.out.print(node.data + " ");
preIteratorTreeRecursion(node.lchild);
preIteratorTreeRecursion(node.rchild);
}
}
//中序遍历递归
private static void midIteratorTreeRecursion(BinaryTree<Integer> node){
if(node != null){
midIteratorTreeRecursion(node.lchild);
System.out.print(node.data + " ");
midIteratorTreeRecursion(node.rchild);
}
}
//后续遍历递归
private static void postIteratorTreeRecursion(BinaryTree<Integer> node){
if(node != null){
postIteratorTreeRecursion(node.lchild);
postIteratorTreeRecursion(node.rchild);
System.out.print(node.data + " ");
}
}
//交换二叉树的左右儿子
private static void exchangeTreeNode(BinaryTree<Integer> node){
BinaryTree<Integer> temp = null;
if(node != null){
temp = node.lchild;
node.lchild = node.rchild;
node.rchild = temp;
exchangeTreeNode(node.lchild);
exchangeTreeNode(node.rchild);
}
}
//判断一个节点是否在一颗子树中
private static boolean nodeIsExist(BinaryTree<Integer> node,Integer data){
if(node != null){
if(data == node.data){
return true;
}else{
boolean isExist1 = nodeIsExist(node.lchild,data);
boolean isExist2 = nodeIsExist(node.rchild,data);
return isExist1 || isExist2 ;
}
}
return false;
}
//寻找行走路径
private static boolean nodePath(BinaryTree<Integer> node,Integer query,Vector<BinaryTree<Integer>> v){
if(node != null){
if(query == node.data){
v.add(node);
return true;
}else{
boolean has = false;
if(nodePath(node.lchild,query,v)){
v.add(node);
has = true;
}
if(!has && nodePath(node.rchild,query,v)){
v.add(node);
has = true;
}
return has;
}
}
return false;
}
//二叉树叶子节点个数递归先序遍历
private static Integer treeLeafCount(BinaryTree<Integer> node,Integer count){
if(node.lchild == null && node.rchild == null){
System.out.print(node.data + " ");
return ++count;
}
if(node.lchild != null)
count = treeLeafCount(node.lchild , count);
if(node.rchild != null)
count = treeLeafCount(node.rchild , count);
return count;
}
//求两个节点的最近公共祖先
private static BinaryTree<Integer> nearestCommonParent(BinaryTree<Integer> tree,Integer node1,Integer node2){
Vector<BinaryTree<Integer>> v1 = new Vector<BinaryTree<Integer>>();
Vector<BinaryTree<Integer>> v2 = new Vector<BinaryTree<Integer>>();
if(nodePath(tree,node1,v1) && nodePath(tree,node2,v2)){
Collections.reverse(v1);
Collections.reverse(v2);
for(int i = 0 ; i < (v1.size() > v2.size() ? v1.size() : v2.size()) ; i++){
if(v1.size() > i && v2.size() > i){
if(v1.get(i) != v2.get(i)){
return v1.get(i-1);
}
}else{
i = v1.size() > v2.size() ? v2.size() : v1.size();
return v1.get(i-1);
}
}
}
return null;
}
//从根节点开始找到所有路径,使得路径上的节点值和为某一数值(路径不一定以叶子节点结束)
private static void printPath(BinaryTree<Integer> node,int count,Vector<BinaryTree<Integer>> v){
if(node == null || node.data > count || count < 0){
return ;
}else{
count -= node.data;
if(count == 0){
v.add(node);
}else{
printPath(node.lchild,count,v);
printPath(node.rchild,count,v);
}
}
}
//求二叉树的高度
private static Integer treeHeight(BinaryTree<Integer> node){
if(node == null){
return 0;
}
if(node.lchild == null && node.rchild == null){
return 1;
}
int height1 = treeHeight(node.lchild);
int height2 = treeHeight(node.rchild);
return height1 - height2 > 0 ? ++height1 : ++height2;
}
//建立2叉排序树
private static BinaryTree<Integer> buildTree(Integer data,BinaryTree<Integer> node){
if(node == null){
node = new BinaryTree<Integer>();
node.data = data;
}else{
if(data < node.data){
node.setLchild(buildTree(data,node.lchild));
}else{
node.setRchild(buildTree(data,node.rchild));
}
}
return node;
}
//中序遍历非递归
private static void midIteratorTree(BinaryTree<Integer> node){
Stack<BinaryTree<Integer>> stack = new Stack<BinaryTree<Integer>>();
while(node != null || !stack.isEmpty()){
while(node != null){
stack.push(node);
node = node.lchild;
}
if(!stack.isEmpty()){
BinaryTree<Integer> temp = stack.pop();
System.out.print(temp.data + " ");
node = temp.rchild;
}
}
System.out.println();
}
//将一个排好序的数组,转变成一颗平衡二叉树(尽量平衡)
private static BinaryTree<Integer> buildBalanceTree(int[] array,int start,int end,BinaryTree<Integer> root){
int sign = 0;
if(end >= start){
sign = end > start ? (end + start ) / 2 : end;
if(root == null){
root = new BinaryTree<Integer>();
root.setData(array[sign]);
}
if(root != null){
root.setLchild(buildBalanceTree(array,start,sign-1,root.lchild));
root.setRchild(buildBalanceTree(array,sign+1,end,root.rchild));
}
}
return root;
}
//后序遍历非递归
private static void postIteratorTreeOne(BinaryTree<Integer> curr){
Stack<BinaryTree<Integer>> stack = new Stack<BinaryTree<Integer>>();
Map<BinaryTree<Integer>,Boolean> stackHeadLocal = new HashMap<BinaryTree<Integer>,Boolean>();
while(curr != null || !stack.isEmpty()){
while(curr != null){
stackHeadLocal.put(curr, true);
stack.push(curr);
curr = curr.lchild;
}
if(!stack.isEmpty()){
BinaryTree<Integer> temp = stack.lastElement();
if(temp.rchild != null && stackHeadLocal.get(temp)){
curr = temp.rchild;
stackHeadLocal.put(temp, false);
}else{
temp = stack.pop();
System.out.print(temp.data + " ");
}
}
}
System.out.println();
}
//先序遍历非递归
private static void preIteratorTree(BinaryTree<Integer> node){
Stack<BinaryTree<Integer>> stack = new Stack<BinaryTree<Integer>>();
stack.push(node);
while(node != null || !stack.isEmpty()){
if(!stack.isEmpty()){
node = stack.pop();
System.out.print(node.data + " ");
}
if(node.rchild != null){
stack.push(node.rchild);
}
if(node.lchild != null){
stack.push(node.lchild);
}
node = node.lchild;
}
System.out.println();
}
//层次遍历2叉树
private static void layoutIteratorTree(BinaryTree<Integer> node){
Queue<BinaryTree<Integer>> queue = new LinkedBlockingQueue<BinaryTree<Integer>>();
queue.offer(node);
while(!queue.isEmpty()){
if(!queue.isEmpty()){
node = queue.poll();
System.out.print(node.data + " ");
}
if(node.lchild != null){
queue.offer(node.lchild);
}
if(node.rchild != null){
queue.offer(node.rchild);
}
}
System.out.println();
}
static class BinaryTree<T>{
private T data;
private BinaryTree<T> lchild;
private BinaryTree<T> rchild;
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public BinaryTree<T> getLchild() {
return lchild;
}
public void setLchild(BinaryTree<T> lchild) {
this.lchild = lchild;
}
public BinaryTree<T> getRchild() {
return rchild;
}
public void setRchild(BinaryTree<T> rchild) {
this.rchild = rchild;
}
}
}