//树基本构造
public class TreeNode<T> {
T value;
TreeNode<T> leftChild;
TreeNode<T> rightChild;
TreeNode(T value) {
this.value = value;
}
TreeNode() {
}
/**
* 增加左子节点
* addLeft:
*
* @param value void 返回类型
*/
public void addLeft(T value) {
TreeNode<T> leftChild = new TreeNode<T>(value);
this.leftChild = leftChild;
}
/**
* addRight: 增加右子节点
*
* @param value void 返回类型
*/
public void addRight(T value) {
TreeNode<T> rightChild = new TreeNode<T>(value);
this.rightChild = rightChild;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (!(obj instanceof TreeNode)) {
return false;
}
return this.value.equals(((TreeNode<?>) obj).value);
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.value.hashCode();
}
@Override
public String toString() {
return this.value == null ? "" : this.value.toString();
}
}
//简单方法
public class Tools {
//访问根结点
private static <T> void visitNode(TreeNode<T> node) {
System.out.println(node.value + "\t");
}
//树的节点个数
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 preOrderTravel(TreeNode<T> root) {
if (root == null) {
return;
}
visitNode(root);
preOrderTravel(root.leftChild);
preOrderTravel(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 backOrderTraval(TreeNode<T> root) {
if (root == null) {
return;
}
backOrderTraval(root.leftChild);
backOrderTraval(root.rightChild);
visitNode(root);
}
//层次遍历
public static <T> void levelTravel(TreeNode<T> root) {
Queue<TreeNode<T>> q = new LinkedList<>();
//向队列尾添加一个元素
q.offer(root);
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 leafNum = getLeafNum(root.leftChild);
int rightNum = getLeafNum(root.rightChild);
return leafNum + 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 isFine = nodeIsChild(root.leftChild, node);
if (!isFine) {
isFine = nodeIsChild(root.rightChild, node);
}
return isFine;
}
//返回两个节点lnode和rnode的以root为根节点的公共子节点
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 || rNode == 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);
}
}
}
}