Binary Tree algorithm problem finishing (d)

A topic

Inserting a write node s in binary sequence leads the right subtree of the node p algorithm

Code

void insert(BiThrNode p,BiThrNode s)
{
	BiThrNode *p,*s;
	if(p->rtag==1)			//无右子树,则有右线索
	{
		s->rchild=p->rchild;
		s->rtag=1;
		p->rchild=s;
		p->rtag=0;
	}
	else
	{
		q=p->rchild;
		while(q->ltag==0)//查找p所指结点中序后继,即右子树中最左下的结点
			q=q->lchild;
		q->lchild=p->rchild;
		s->rtag=0;
		p->rchild=s;
	}
	s->lchild=p;
	s->ltag=1;
}

Topic two

Given binary tree from bottom to top, right to left hierarchy traversal algorithm

thought

It is the use of the original hierarchy traversal algorithm will traverse all the nodes on the stack, and then popping. Take advantage of the characteristics of the stack in reverse order of
the existing hierarchy traversal algorithm is top to bottom, left to right, where the traversal algorithm opposite. Using the original hierarchy traversal algorithm, each of the teams while node pointer stack, the stack starts from a stack access to all the nodes and then
embodied
1, root node enqueue
2, the elements of a queue traversal element
3, followed by the right child of this element, the team left the child
4, if the queue is no air conditioning to 2, otherwise end

Code

void InvertLevel(BiTree bt){
	Stack s;
	Queue Q;
	if(bt!=null)
	{
		InitStack(s);		//初始化栈,栈中存放二叉树结点的指针
		InitQueue(Q);
		EnQueue(Q,bt);
		while(IsEmpty(Q)==false){
			DeQueue(Q,p);
			Push(s,p);			//出队入栈
			if(p->lchild)
				EnQueue(Q,p->lchild);		//左不空,入队
			if(p->rchild)
				EnQueue(Q,p->rchild);
		}
		while(Isempty(s)==false){
			Pop(s,p);
			visit(p->data);
		}
	}
}


#define MAX_NODE 50
void levelorderTraverse(BTNode *T)
{
//基础层次遍历
	BTNode *Queue[MAX_NODE],*p=T;
	int front=0,rear=0;
	if(T==NULL)
		printf("Empty");
	else{
		Queue[++rear]=p;
		while(front<rear){
			p=Queue[++front];
			visit(p->data);
			if(p->Lchild!=NULL)
				Queue[++rear]=p->Lchild;
			if(p->Rchild!=NULL)
				Queue[++rear]=p->Rchild;
		}
	}
}

Published 73 original articles · won praise 20 · views 4465

Guess you like

Origin blog.csdn.net/lzl980111/article/details/103132953