二叉树遍历

#include 
#include 
#include 
using namespace std;
typedef struct biNode
{
	char   data;
	biNode *lChild;
	biNode *rChild;
};

char pre[100];
char in[100];

void createTree(biNode* &biTree, int preStart, int preEnd, int inStart, int inEnd)
{
	biTree = (biNode *) malloc (sizeof(biNode));
	biTree->data = pre[preStart];
	biTree->lChild = NULL;
	biTree->rChild = NULL;
	//寻找签序点在中序中的位置
	int rootIndex ;
	for(int i = inStart; i <= inEnd; ++i)
	{
		if(biTree->data == in[i])
		{
			rootIndex = i;
			break;
		}
	}

	if(inStart != rootIndex)
	{
		createTree(biTree->lChild, preStart + 1, preStart + (rootIndex - inStart), inStart, rootIndex-1);
	}
	else
	{
		biTree->lChild = NULL;
	}

	if(rootIndex != inEnd)
	{
		createTree(biTree->rChild, preStart + (rootIndex - inStart) + 1, preEnd, rootIndex + 1, inEnd);
	}
	else
	{
		biTree->rChild = NULL;
	}
}


void postVisit(biNode *biTree)
{
	if(biTree == NULL)
	{
		return ;
	}
	else 
	{
		if(biTree->lChild != NULL)
		{
			postVisit(biTree->lChild);
		}
		if(biTree->rChild != NULL)
		{
			postVisit(biTree->rChild);
		}
		cout<data;
	}
	
	

}
int main()
{
	memset(pre, 0, sizeof(char)*100);
	memset(in, 0, sizeof(char)*100);

	while(scanf("%s%s", pre, in) != EOF)
	{
		biNode* biTree = NULL;
		int preEnd = strlen(pre) -1;
		int inEnd = strlen(in) -1;
		createTree(biTree, 0, preEnd, 0, inEnd);
		postVisit(biTree);
		cout<

你可能感兴趣的:(入职练习)