树
节点:1.分支节点 2.子叶节点
二叉书:1.root 2.左子树 3.右子树
preorder 前序 根-》左子树-》右子树
portorder 后序 左子树-》右子树-》根
inorder 中序 左子树-》根-》右子树
分治:divid and conquer
struct node
{
int data;
struct node *left,*right;
};
struct node* creat_tree(int s1,int n1,int s2,int n2)
{
int i,r;
if(s1>n1||s2>n2) return NULL;//结束
struct node*root;//定义这个树
/*
sizeof(struct node)
就是求 struct node 这个结构体占用的字节数。
malloc(sizeof(struct node))
申请 struct node 这个结构体占用字节数大小的空间
(struct node *) malloc(sizeof(struct node))
将申请的空间的地址强制转化为 struct node * 指针类型
x=(struct node *) malloc(sizeof(struct node))
将那个强制转化的地址赋值给 x.
*/
root=malloc(sizeof(struct node));//root指向sizeof(struct node)这么大的空间
r=pre[s1];
for(i=s2;i<=n2&&in[i]!=r;i++);//在中序找到树根
root->data=r;
root->left=creat_tree(s1+1,s1+i-s2,s2,i-1);
root->right=creat_tree(s1+i-s2+1,n1,i+1,n2);
return root;
}
void post_visit(struct node *root)
{
if(root==NULL) return ;
post_visit(root->left);
post_visit(root->right);
printf("%8d",root->data);
free(root);
}
2019.7.22