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://azwzsj.com/article/jesed.html