1.创建一个二叉树
思想:前序创建,VLR顺序,递归
int main()
{
BTDataType arr[] = { 'A', 'B', 'D', '#', '#', 'G','#','#' ,'C', 'E', '#', '#', 'F'};
int num = sizeof(arr) / sizeof(arr[0]);
int i = 0;
}
//_arr为数组,num为元素个数,i为元素下标
BTNode* BuyNodeTree(BTDataType x)
{
BTNode* newNode = (BTNode*)malloc(sizeof(BTNode));
assert(newNode);
newNode->_data = x;
newNode->_left = NULL;
newNode->_right = NULL;
return newNode;
}
BTNode* BinaryTreeCreate(BTDataType* a, int num, int* i) //前序,VLR
{
if (a[*i] != '#'&& (*i)<num)
{
BTNode* root = BuyNodeTree(a[*i]);
(*i)++;
root->_left = BinaryTreeCreate(a, num, i);
(*i)++;
root->_right = BinaryTreeCreate(a, num, i);
return root;
}
else
return NULL;
}
2.计算树 的节点
思想:左树的节点+右树的节点+1
int BinaryTreeNodeSize(BTNode* root)
{
if (root == NULL)
return 0;
return BinaryTreeNodeSize(root->_left) + BinaryTreeNodeSize(root->_right) + 1;
}
3.计算树的叶节点
思想:左树的叶节点+右树的叶节点
int BinaryTreeLeafSize(BTNode* root)
{
if (root == NULL)
return 0;
if (root->_left == NULL && root->_right == NULL)
return 1;
return BinaryTreeLeafSize(root->_le