# 树的构建
public vlass TreeNode<T> {
/*节点:数据 + 左右孩子*/
T value;
TreeNode<T> leftChild;
TreeNode<T> rightChild;
/*相关方法*/
// 添加子树 -- addleft(T value) || addright(T value)
// 节点相等判断 -- [@override] public boolean equals(Object obj)
// 哈希值 -- hashCode()
// 字符形式返回数据内容 toString()
}
# 数的基本操作
public class TreeTools{
/*判断树节点个数*/
//传入父节点 --> 左右子树节点个数 + 1
/*判断树深度*/
//左右子树深度最大值 + 1
/*前中后序遍历 【重写visitNode()方法】*/
// 前: 头、左、右
// 中: 左、头、右
// 后: 左、右、头
/*分层遍历 -- 引入队列实现*/
}
#代码实现
/*树的节点结构*/
public calss TreeNode<T> {
T value;
TreeNode<T> leftChild;
TreeNode<T> rightChild;
TreeNode(T value){
this.value = value;
}
public void addLeft(T value){
TreeNode<T> leftChild = new TreeNode<t>(value);
this.leftChild = leftChild;
}
public void addRight(T value){
TreeNode<T> rightChild = new TreeNode<T>(value);
this.rightChild = rightChild;
}
@Override
public boolean equals(Object obj){
if(!(obj instanceof TreeNode)){
return false;
}
return this.value.equals(((TreeNode<?>)obj).value);
}
@Override
public int hashCode(){
return this.value.hashCode();
}
@override
public String toString(){
return this.value == null ? "" : this.value.toString();
}
}
/*树的基本操作*/
public class TreeTools {
/*判断树的节点个数*/
public static <T> int getTreeNum(TreeNode<T> root){
if(root == null){
return 0;
}
return getTreeNum(root.leftChild) + getTreeNum(root.rightChild) + 1;
}
/*判断树的深度*/
public static <T> int getTreeDepth(TreeNode<T> root){
if(root == null){
return 0;
}
int leftDepth = getTreeDepth(root.leftChild) + 1;
int rightDepth = getTreeDepth(root.rightChild) + 1;
return Math.max(leftDepth, rightDepth);
}
/*前序遍历*/
public static <T> void preOrderTraval(TreeNode<T> root){
if(root == null){
return ;
}
visitNode(root);
preOrderTraval(root.leftChild);
preOrderTraval(root.rightChild);
}
/*中序遍历*/
public static <T> void midOrderTravel(TreeNode<T> root){
if(root == null){
return ;
}
midOrderTravel(root.leftChild);
visitNode(root);
midOrderTravel(root.rightChild);
}
/*后序遍历*/
public static <T> void backOrderTravel(TreeNode<T> root){
if(root == null){
return ;
}
backOrderTravel(root.leftChild);
backOrderTravel(root.rightChild);
visitNode(root);
}
/*访问节点*/
private static <T> void vistNode(TreeNode<T> node){
System.out.print(node.value + "\t");
}
/*分层遍历*/
//import java.util.Queue; 继承了Collection接口
public static <T> void levelTravel(TreeNode<T> root){
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>>();
q.offer(root);//添加元素 队列满返回false
while(!q.isEmpty()){
TreeNode<T> temp = q.poll();//移除并返回队列元素
visitNode(temp);
if(temp.leftChild != null){
q.offer(temp.leftChild);
}
if(temp.rightChild != null){
q.offer(temp.rightChild);
}
}
}
/*求出第K层节点个数*/
public static <T> int getNumForKlevel(TreeNode<T> root, int k){
if(root == null || k < 1){
return 0;
}
if(k==1){
return 1;
}
int leftnum = getNumForKlevel(root.leftChild, k-1);
int rightnum = getNumForKlevel(root.rightChild, k-1);
return leftNum + rightNum;
}
/*叶子节点个数*/
public static <T> int getLeafNum(TreeNode<T> root){
if(root == null){
return 0;
}
if(root.leftChild == null && root.rightChild == null){
return 1;
}
int leftnum = getLeafNum(root.leftChild);
int rightnum = getLeafNum(root.rightChild);
return leftnum + rightnum;
}
/*变换根节点左右子树*/
public static <T> TreeNode<T> exchange(TreeNode<T> root){
if(root == null){
return null;
}
TreeNode<T> left = exchange(root.leftChild);
TreeNode<T> right = exchange(root.rightChild);
root.leftChild = right;
root.rightChild = left;
return root;
}
/*查看node是否是root子节点*/
public static <T> boolean nodeIsChild(TreeNode<T> root, TreeNode<T> node){
if(root == null || node == null){
return false;
}
if(root == node){
return true;
}
boolean isFind = nodeIsChild(root.leftChild, node);
if(!isFind){
isFind = nodeIsChild(root.rightChild, node);
}
return isFind;
}
/*查看公共父节点*/
public static <T> TreeNode<T> findAllFatherNode(TreeNode<T> root, TreeNode<T> lNode, TreeNode<T> rNode){
if(lNode == root || rNode == root){
return root;
}
if(root == null || lNode == null || rNode == null)
{
return null;
}
if(nodeIsChild(root.leftChild, lNode)){
if(nodeIsChild(root.rightChild, rNode)){
return root;
}else{
return findAllFatherNode(root.leftChild, lNode, rNode);
}
}else{
if(nodeIsChild(root.leftChild, rNode){
return root;
}else{
return findAllFatherNode(root.rightChild, lNode, rNode);
}
}
}
}