public static int BiTreeDepth(BitNode T) {
int depthval, depthLeft, depthRight;
if (T == null)
depthval = 0;
else if (T.lchild == null && T.rchild == null)
depthval = 1;
else {
depthLeft = BiTreeDepth(T.lchild);
depthRight = BiTreeDepth(T.rchild);
depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight);
}
return depthval;
}
public static int level(BitNode bitNode, int data) {
int leftLevel, rightLevel;
if (bitNode == null)
return -1;
if (data == bitNode.data)
return 1;
leftLevel = bitNode.lchild == null ? -1 : level(bitNode.lchild, data);
rightLevel = bitNode.rchild == null ? -1 : level(bitNode.rchild, data);
if (leftLevel < 0 && rightLevel < 0)
return -1;
return leftLevel > rightLevel ? leftLevel + 1 : rightLevel + 1;
}
public static int leafNum(BitNode tree) {
if (tree == null)
return 0;
else {
int left = leafNum(tree.lchild);
int right = leafNum(tree.rchild);
if (tree.lchild == null && tree.rchild == null)
return left + right + 1;
else
return left + right;
}
}
public static int fatherNodes(BitNode tree) {
if (tree == null || (tree.lchild == null && tree.rchild == null))
return 0;
else {
int left = fatherNodes(tree.lchild);
int right = fatherNodes(tree.rchild);
return left + right + 1;
}
}
public static int oneChildFather(BitNode tree) {
int left, right;
if (tree == null || (tree.rchild == null && tree.lchild == null))
return 0;
else {
left = oneChildFather(tree.lchild);
right = oneChildFather(tree.rchild);
if ((tree.lchild != null && tree.rchild == null)
|| (tree.lchild == null && tree.rchild != null))
return left + right + 1;
else
return left + right;
}
}
public static int leftChildFather(BitNode tree) {
if (tree == null)
return 0;
else {
int left = leftChildFather(tree.lchild);
int right = leftChildFather(tree.rchild);
if ((tree.lchild != null && tree.rchild == null))
return left + right + 1;
else
return left + right;
}
}
public static int rightChildFather(BitNode tree) {
if (tree == null)
return 0;
else {
int left = leftChildFather(tree.lchild);
int right = leftChildFather(tree.rchild);
if ((tree.lchild == null && tree.rchild != null))
return left + right + 1;
else
return left + right;
}
}
public static int doubleChildFather(BitNode tree) {
int left, right;
if (tree == null)
return 0;
else {
left = doubleChildFather(tree.lchild);
right = doubleChildFather(tree.rchild);
if (tree.lchild != null && tree.rchild != null)
return (left + right + 1);
else
return (left + right);
}
}
public static void exChange(BitNode tree) {
if (tree == null)
return;
if (tree.lchild != null)
exChange(tree.lchild);
if (tree.rchild != null)
exChange(tree.rchild);
BitNode temp = tree.lchild;
tree.lchild = tree.rchild;
tree.rchild = temp;
}
public static int getSumByRecursion(BitNode tree) {
if (tree == null) {
return 0;
} else {
int left = getSumByRecursion(tree.lchild);
int right = getSumByRecursion(tree.rchild);
return tree.data + left + right;
}
}