8 / / 6 10 / / / / 5 7 9 11
#include "stdio.h"
#include "stdlib.h"
//二叉树节点
#define size 7
//二叉树节点定义
typedef struct node
{
int data;
struct node *left;
struct node *right;
}BTree;
int printLine(BTree * root);
BTree * CreatTree(int a[],int n);
int main(void)
{
int array[size] = {8,6,10,5,7,9,11};
BTree * root;
root = CreatTree(array,size);
printLine(root);
printf("\n");
return 0;
}
int printLine(BTree * root)
{
BTree * queue[size], *p;
int front,rear;
front = rear = 0;
rear = (rear+1)%size;
queue[rear] = root;
//循环结束为队列为空
while(front != rear)
{
//根出队列
front = (front +1)%size;
p = queue[front];
printf("%3d",p->data);
//左孩子不空,队不满入队
if(p->left && ((rear+1)%size != front))
{
rear = (rear+1)%size;
queue[rear] = p->left;
}
//右孩子不空,队不满入队
if(p->right && ((rear+1)%size != front))
{
rear = (rear+1)%size;
queue[rear] = p->right;
}
//队满,报错
if((rear+1)%size == front)
{
printf("队列空间不足,错误....\n");
return 0;
}
}
return 1;
}
//根据数组创建二叉排序树
BTree * CreatTree(int a[],int n)
{
BTree * root ,*p,*cu,*pa;
int i;
root = (BTree *)malloc(sizeof(BTree));
root->data = a[0];
root->left = root->right =NULL;
for(i=1;i<n;i++)
{
p = (BTree *)malloc(sizeof(BTree));
p->data = a[i];
p->left = p->right =NULL;
cu = root;
while(cu)
{
pa = cu;
if(cu->data > p->data)
cu = cu->left;
else
cu = cu->right;
}
if(pa->data > p->data)
pa->left = p;
else
pa->right = p;
}
return root;
}
8 / \ 6 10 / \ / \ 5 7 9 11
#include "stdio.h"
#include "stdlib.h"
int isPostorderResult(int a[],int n);
int helper(int a[],int s,int e);
int main(void)
{
int a[7] = {5,7,6,9,11,10,8};
int b[4] = {7,4,6,5};
int tmp;
tmp = isPostorderResult(a,7);
printf("%d",tmp);
return 0;
}
int isPostorderResult(int a[],int n)
{
return helper(a,0,n-1);
}
int helper(int a[],int s,int e)
{
int i,j,root;
if(s == e)
return 1;
for(i=0;i<e && a[i]<a[e];i++);
if(i != 0 && helper(a,s,i-1) == 0)
return 0;
for(j=i;j<e && a[j]>a[e];j++);
if(j==e && helper(a,i,j-1) == 1)
return 1;
else
return 0;
}
8 / \ 6 10 /\ /\ 5 7 9 11
8
/ \
10 6
/\ /\
11 9 7 5
#include "stdio.h"
#include "stdlib.h"
#define MAXSIZE 8
typedef struct node
{
int data;
struct node * left;
struct node * right;
}BTree;
void swap(BTree ** x,BTree ** y);//交换左右孩子
void mirror(BTree * root);//递归实现函数声明
void mirrorIteratively(BTree * root);//非递归实现函数声明
BTree * CreatTree(int a[],int n);//创建二叉树(产生二叉排序树)
void Iorder(BTree * root);//中序遍历查看结果
int main(void)
{
int array[MAXSIZE] = {5,3,8,7,2,4,1,9};
BTree * root;
root = CreatTree(array,MAXSIZE);
printf("变换前:\n");
Iorder(root);
printf("\n变换后:\n");//两次变换,与变化前一致
mirror(root);
mirrorIteratively(root);
Iorder(root);
printf("\n");
return 0;
}
void swap(BTree ** x,BTree ** y)
{
BTree * t = * x;
* x = * y;
* y = t;
}
void mirror(BTree * root)
{
if(root == NULL)//结束条件
return;
swap(&(root->left),&(root->right));//交换
mirror(root->left);//左子树递归
mirror(root->right);//右子树递归
}
void mirrorIteratively(BTree * root)
{
int top = 0;
BTree * t;
BTree * stack[MAXSIZE+1];
if(root == NULL)
return;
//手动压栈、弹栈
stack[top++] = root;
while(top != 0)
{
t = stack[--top];
swap(&(t->left),&(t->right));
if(t->left != NULL)
stack[top++] = t->left;
if(t->right != NULL)
stack[top++] = t->right;
}
}
//产生二叉排序树
BTree * CreatTree(int a[],int n)
{
BTree * root ,*p,*cu,*pa;
int i;
root = (BTree *)malloc(sizeof(BTree));
root->data = a[0];
root->left = root->right =NULL;
for(i=1;i<n;i++)
{
p = (BTree *)malloc(sizeof(BTree));
p->data = a[i];
p->left = p->right =NULL;
cu = root;
while(cu)
{
pa = cu;
if(cu->data > p->data)
cu = cu->left;
else
cu = cu->right;
}
if(pa->data > p->data)
pa->left = p;
else
pa->right = p;
}
return root;
}
//中序遍历
void Iorder(BTree * root)
{
if(root)
{
Iorder(root->left);
printf("%3d",root->data);
Iorder(root->right);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有