普通二叉树的遍历及求节点个数

普通二叉树的遍历及求节点个数


//学习普通二叉树的意义:
// a 为后面学习更复杂的二叉树,打基础(例如搜索二叉树、AVL树、红黑树等)【这些复杂的二叉树可以实现快速搜索数据,它们增删查改才有意义】
// b 有很多二叉树的OJ算法题目,都是出在普通二叉树上。

//重点学习:遍历和控制二叉树结构

//普通二叉树的三种遍历方式
// a 前序遍历(Preorder Traversal)根 左子树 右子树
// b 中序遍历(Inorder Traversal)左子树 根 右子树
// c 后序遍历(Postorder Traversal)左子树 右子树 根
// 用递归的方法实现

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef int BTDataType;
typedef struct BinaryTreeNode //定义二叉树节点
{
	BTDataType data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BTNode;

static BTNode* BuyNode(BTDataType x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	assert(node);

	node->data = x;
	node->left = NULL;
	node->right = NULL;

	return node;
}

static BTNode* CreateBinaryTree()
{
	BTNode* node1 = BuyNode(1);
	BTNode* node2 = BuyNode(2);
	BTNode* node3 = BuyNode(3);
	BTNode* node4 = BuyNode(4);
	BTNode* node5 = BuyNode(5);
	BTNode* node6 = BuyNode(6);

	node1->left = node2;
	node1->right = node4;
	node2->left = node3;
	node4->left = node5;
	node4->right = node6;
	return node1;
}

void PreOrder(BTNode* root) //前序遍历
{
	if (NULL==root)
	{
		printf("# ");
		return;
	}

	printf("%d ",root->data);
	PreOrder(root->left);
	PreOrder(root->right);
}

void InOrder(BTNode* root) //中序遍历
{
	if (NULL == root)
	{
		printf("# ");
		return;
	}

	InOrder(root->left);
	printf("%d ", root->data);
	InOrder(root->right);
}

void PostOrder(BTNode* root) //后续遍历
{
	if (NULL == root)
	{
		printf("# ");
		return;
	}

	PostOrder(root->left);
	PostOrder(root->right);
	printf("%d ", root->data);
}

int count = 0;
void TreeSize1(BTNode* root) //遍历的思路求二叉树的元素个数
{
	if (NULL==root)
	{
		return;
	}
	count++;
	TreeSize1(root->left);
	TreeSize1(root->right);
}

int TreeSize2(BTNode* root) //分治的思路求二叉树的节点个数
{
	if (NULL==root)
	{
		return 0;
     }
	return TreeSize2(root->left) + TreeSize2(root->right)+1;
}

int TreeLeafSize(BTNode* root) //分治的思路求二叉树叶子节点个数
{
	if (NULL==root)
	{
		return 0;
	}
	if (NULL==root->left && NULL==root->right)
	{
		return 1;
	}
	return TreeLeafSize(root->left) + TreeLeafSize(root->right);
}


int TreeKLevel(BTNode* root,int k) //求二叉树第K层的节点个数
{
//思路——转化为子问题:求左子树的第k-1层+右子树的第k-1层
	assert(k>=1);
	if (NULL==root)
		return 0;

	if (1 == k)
		return 1;

	return TreeKLevel(root->left,k-1) + TreeKLevel(root->right,k-1);
}

//求二叉树的深度
int TreeDepth(BTNode* root)
{
	if (NULL == root)
		return 0;
	int ret1 = TreeDepth(root->left);
	int ret2 = TreeDepth(root->right);
	if (ret1>=ret2)
	{
		return ret1 + 1;
	}
	else
	{
		return ret2 + 1;
	}
}

//二叉树查找值为x的结点
BTNode* TreeFind(BTNode* root, BTDataType x)
{
	if (NULL == root)
		return NULL;

	if (x==root->data)
		return root;

	BTNode* ret1 = TreeFind(root->left,x);
	if (ret1)
		return ret1;

	BTNode* ret2 = TreeFind(root->right, x);
	if (ret2)
		return ret2;
	
	return NULL;
}

int main()
{
	BTNode* root = CreateBinaryTree();
	PreOrder(root);
	printf("\n");

	InOrder(root);
	printf("\n");

	PostOrder(root);
	printf("\n");

	//求二叉树的节点个数
	count = 0;//防止全局变量count累计增加
	TreeSize1(root);
	printf("TreeSize:%d\n",count);
	
	printf("TreeSize:%d\n", TreeSize2(root));
	
	//求二叉树叶子节点的个数
	printf("TreeLeafSize:%d\n", TreeLeafSize(root));

    //求二叉树第3层的结点数
	printf("TreeKLevel:%d\n", TreeKLevel(root, 3)); 

	//求二叉树的深度
	printf("TreeDepth:%d\n", TreeDepth(root));
	
	TreeFind(root, 5);

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值