算法 树 前序,中序->后序

节点: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值