Article Directory
A traversal algorithm
1. The output of the binary tree nodes
void PreOrder(BiTree root)
{
if(root != NULL)
{
printf("%d",root->data); // 假设数据为int类型
PreOrder(root->RChild); // 遍历左子树
PreOrder(root->RChild); // 遍历右子树
//上例为先序遍历,中序遍历和后序遍历,只是printf()位置不同。
}
}
2. The output of the binary tree leaf node
void PreOrder(BiTree root)
{
if(root != NULL)
{
if(root->LChild == NULL && root->RChild == NULL)
{
printf("%d",root->data);
}
PreOrder(root->LChild);
PreOrder(root->RChild);
}
}
3. Statistical leaf node number of points
int LeafCount = 0;
void leaf(BiTree root)
{
if(root != NULL)
{
leaf(root->LChild);
leaf(root->RChild);
if(root->LChild == NULL && root->RChild == NULL)
{
LeafCount++;
}
}
}
4. Establishment of binary binary tree list stored
void CreateBiTree(BiTree * bt)
{
char ch;
ch = getchar();
if(ch == '.')
{
*bt = NULL;
}
else
{
*bt = (BiTree)malloc(sizeof(BiTree));
(*bt)->data = ch;
CreateBiTree(&((*bt)->LChild));
CreateBiTree(&((*bt)->RChild));
}
}
The height of the binary tree seek
// 先序遍历求二叉树的高度
int PostTreeDepth(BiTree bt, int h)
{
if(bt != NULL)
{
if(h>depth)
{
depth = h;
}
PostTreeDepth(bt->LChild,h+1);
PostTreeDepth(bt->RChild,h+1);
}
}
// 后序遍历求二叉树的高度
int PostTreeDepth(BiTree bt)
{
int hl,hr,max;
if( bt != NULL)
{
hl = PostTreeDepth(bt->LChild);
hr = PostTreeDepth(bt->RChild);
max = hl>hr?hl:hr;
return max+1;
}
else return 0;
}
6. Press the binary tree print
void PrintfTree(BiTree bt,int nLayer)
{
if(bt == NULL)return ;
PrintfTree(bt->RChild,nLayer+1);
for(int i = 0;i<nLayer;i++)
{
printf(" ");
}
printf("%c\n",bt->data);
PrintfTree(bt->LChild,nLayer+1);
}
If wrong, please correct me criticism, comments are welcome.
Each text sentence: For crowned, must bear its weight. So how lucky providential, not only in exchange for all of the way through the clutter.