//求二叉树的深度
int getBiTreeDepth(BiTree T, int root){
	if(!root)//根节点为空树
		return 0;
	int leftTreeDepth = getBiTreeDepth(T, T[root].llink);
	int rightTreeDepth = getBiTreeDepth(T, T[root].rlink);
	return leftTreeDepth > rightTreeDepth ? (leftTreeDepth + 1) : (rightTreeDepth + 1);
}
Exemple #2
0
/*** 获取树的高度***/ 
int getBiTreeDepth(BitTree* T) 
{
	int left, right;
	if(T == NULL)
		return 0;
	left = getBiTreeDepth(T->lchild);
	right = getBiTreeDepth(T->rchild);
	return (left > right)?(left + 1):(right + 1);  //关键语句 
}
Exemple #3
0
int main()
{
	BitTree *T;
	T = createBitTree();
	preOrder(T);
	printf("The BitTree depth is %d", getBiTreeDepth(T));
	return 0;
}
void main(){
	BiTree tree;
	int root = 1;//根节点的位置
	printf("请按先序次序输入二叉树各节点以#号结束,空树用点号代替:\n");
	int pos = 1;//控制加入静态数组的位置
	createBiTree(tree, root, pos);	

	
	printf("先序遍历打印二叉树(递归算法):\n");
	preOrderPrint(tree, root);
	printf("\n");
		

	printf("先序遍历打印二叉树(非递归算法):\n");
	preOrderPrint2(tree, root);
	printf("\n");
	

	printf("中序遍历打印二叉树(递归算法):\n");
	inOrderPrint(tree, root);
	printf("\n");

	printf("中序遍历打印二叉树(非递归算法):\n");
	inOrderPrint2(tree, root);
	printf("\n");
	
	
	printf("后序遍历打印二叉树(递归算法):\n");
	postOrderPrint(tree, root);
	printf("\n");

	printf("后序遍历打印二叉树(非递归算法):\n");
	postOrderPrint2(tree, root);
	printf("\n");
	
	
	printf("按层次遍历打印二叉树(非递归算法):\n");
	hierarchicalTraversePrint(tree, root);
	printf("\n");
	

	
	int depth = getBiTreeDepth(tree, root);
	printf("该二叉树的深度为:%d\n", depth);
	

	int size = getBiTreeSize(tree, root);
	printf("该二叉树的结点数为:%d\n", size);
	
	int leafNodesNum = getBiTreeLeafNodesNum(tree, root);
	printf("该二叉树的叶子结点数为:%d\n", leafNodesNum);
	
}