资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C++辅助队列实现二叉树-创新互联

#includeusing namespace std;
typedef struct node {
	char c;
	struct node* pleft;
	struct node* pright;
}Node, * pNode;

typedef struct queue {
	pNode insert_pos;
	struct queue* next;
}Queue,*pQueue;
void pre_order(pNode tree)
{
	if(tree)
    {
		putchar(tree->c);
		pre_order(tree->pleft);
		pre_order(tree->pright);
	}
}
void build_binary_tree(pNode* phead, pQueue* qhead, pQueue* qtail, char val)
{	
	pNode p = (pNode)calloc(1, sizeof(Node));
	pQueue q = (pQueue)calloc(1, sizeof(Queue));
	pNode pcur = NULL;
	pQueue qcur = *qhead;
	p->c = val;
	q->insert_pos = p;
	if (!(*phead))
	{	
		*phead = p;
		*qhead = *qtail = q;
	}
	else {
		(*qtail)->next = q;
		(*qtail) = q;
		pcur = (*qhead)->insert_pos;
		if (NULL == pcur->pleft)
		{
			pcur->pleft = p;
		}
		else if(NULL == pcur->pright)
		{
			pcur->pright = p;
			*qhead = qcur->next;
			free(qcur);
			qcur = NULL;
		}
	}
}
int main()
{	
	char c;
	pNode tree_root = NULL;
	pQueue qhead = NULL, qtail = NULL;
	while (cin>>c)
	{
		if (c != '\n')
		build_binary_tree(&tree_root, &qhead, &qtail, c);
	}
	pre_order(tree_root);
	return 0;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

成都创新互联公司专注于云霄企业网站建设,响应式网站开发,商城网站制作。云霄网站建设公司,为云霄等地区提供建站服务。全流程按需开发网站,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务
分享标题:C++辅助队列实现二叉树-创新互联
链接地址:http://cdkjz.cn/article/jesed.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220