//创建二叉树
bitree *create_bitree(int i, int n) //i=1, n为节点的个数
{if (i == 0) return ;
bitree *root = (bitree*)malloc(sizeof(bitree));
root->data = i;
if (2*i <= n) //如果存在左子树
{
root->lchild = create_bitree(2*i, n);
}
else
root->lchild = NULL;
if (2*i+1 <= n) //如果存在右子树
{
root->rchild = create_bitree(2*i+1, n);
}
else
root->rchild = NULL;
return root;
}
//先序遍历二叉树
void preorder(bitree *root)
{
if (NULL == root)
return ;
printf("%d ", root->data);
preorder(root->lchild);
preorder(root->rchild);
}
//层次遍历二叉树,用队列实现
void layer_order(bitree *root)
{
bitree *temp;
queue qu;
init_queue(&qu);
join_queue(&qu, root);
while (!empty_queue(&qu))
{
temp = out_queue(&qu);
printf("%d ", temp->data);
if (temp->lchild)
join_queue(&qu, temp->lchild);
if (temp->rchild)
join_queue(&qu, temp->rchild);
}
return ;
}