1.二叉树创建
2.二叉树先中后序遍历
先
void output(tree head)
{
if(NULL==head)
return;
printf("%c",head->data);
output(head->ltree);
output(head->rtree);
}
中
void output(tree head)
{
if(NULL==head)
return;
output(head->ltree);
printf("%c",head->date);
output(head->rtree);
}
后
void output(tree head)
{
if(NULL==head)
return;
output(head->ltree);
output(head->rtree);
printf("%c",head->date);
}
3.二叉树计算节点
void count(tree head,int *n0,int *n1,int *n2)
{
if(head==NULL)
return;
if(head->ltree==NULL && head->rtree==NULL)
++*(n0);
else if(head->ltree!=NULL && head->rtree!=NULL)
++*(n2);
else
++*(n1);
count(head->ltree,n0,n1,n2);
count(head->rtree,n0,n1,n2);
return;
}
4.二叉树计算深度
int high(tree head)
{
if(NULL==head)
return 0;
int life=1+high(head->ltree);
int right=1+high(head->rtree);
return left>right?left:right;
}
5.实现快速排序降序
int one_sort(int *p,int low,int high)
{
//确定基准值
int key=*(p+low);
//当low==high结束
while(low<high)
{
//从high开始比较
while(low<high && key>=*(p+high))
{
high--;
}
*(p+low)=*(p+high);
//从low开始比较
while(low<high && key<=*(p+low))
{
low++;
}
*(p+high)=*(p+low);
}
*(p+low)=key;
return low;
}
void quick_sort(int *p,int low,int high)
{
//没有元素或只有一个元素
if(low>=high)
return;
//一次排序
int mid=one_sort(p,low,high);
//递归左子序列
quick_sort(p,low,high-1);
//递归右子序列
quick_sort(p,mid+1,high);
}
int main(int argc, const char *argv[])
{
int arr[]={1,15,54,48,65,2,7};
int len=sizeof(arr)/sizeof(arr[0]);
quick_sort(arr,0,len-1);
for(int i=0;i<len;i++)
{
printf("%-4d",arr[i]);
}
puts("");
return 0;
}
思维导图