Based on Double Binary binary tree traversal of the list

#include <iostream>
using namespace std;

typedef struct BiTNode
{
	char data;
	struct BiTNode *lchild,*rchild;	
}BiTNode,*BiTree;

void CreateBiTree(BiTree &T)
{
	char ch;
	cin>>ch;
	if(ch=='0')T=NULL;
	else
	{
		T=new BiTNode;
		T->data=ch;
		CreateBiTree(T->lchild);
		CreateBiTree(T->rchild); 
	}
}

void CreateBiTree(BiTree &T,char ch)
{
	if(ch=='0')T=NULL;
	else
	{
		T=new BiTNode;
		T->data=ch;
		CreateBiTree(T->lchild);
		CreateBiTree(T->rchild); 
	}
}

int Traverse(BiTree T)
{
	if(T)
	{	
		cout<<T->data; 
		Traverse(T->lchild);
		cout<<T->data; 
		Traverse(T->rchild);	
	}
}

int main()
{
	while(1)
	{
		char ch;
		cin>>ch;
		if(ch=='0')break;
		BiTree T;
		CreateBiTree(T,ch);
		Traverse(T);
		cout<<endl;
	}
	return 0;
}
 

description

 

Each binary element provided are a character node, establishing binary sequence preorder traversal list of the preparation of a recursive algorithm implemented bis preorder traversal of the binary tree ( biordered means for traversing each node of the binary tree is first access this node, then double-order traversal its left subtree, then visit again this node, followed by a double-order traversal its right subtree ) .


 

 

Entry

Multiple sets of data. Data of one line each, for the first sequence of binary sequence (when the elements in the sequence is '0', indicates the node is empty). When you enter only a "0", the input end.

Export

Each data output line, the resulting double binary sequence preorder method.

Sample input 1 

ab000
ab00c00
0

Sample Output 1

abba
abbacc
Published 100 original articles · won praise 4 · Views 3658

Guess you like

Origin blog.csdn.net/weixin_43673589/article/details/104420896