程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 1577 BSTµÄ»ù´¡Ìâ GCC¿ÉÒÔAC

POJ 1577 BSTµÄ»ù´¡Ìâ GCC¿ÉÒÔAC

編輯:C++入門知識

POJ 1577 BSTµÄ»ù´¡Ìâ GCC¿ÉÒÔAC


Description

\

Figure 1 shows a graphical representation of a binary tree of letters. PeZ†·Ÿ"http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcGxlIGZhbWlsaWFyIHdpdGggYmluYXJ5IHRyZWVzIGNhbiBza2lwIG92ZXIgdGhlIGRlZmluaXRpb25zIG9mIGEgYmluYXJ5IHRyZWUgb2YgbGV0dGVycywgbGVhdmVzIG9mIGEgYmluYXJ5IHRyZWUsIGFuZCBhIGJpbmFyeSBzZWFyY2ggdHJlZSBvZiBsZXR0ZXJzLCBhbmQgZ28gcmlnaHQgdG8gVGhlIHByb2JsZW0uCjxicj4KPGJyPgpBIGJpbmFyeSB0cmVlIG9mIGxldHRlcnMgbWF5IGJlIG9uZSBvZiB0d28gdGhpbmdzOiA8YnI+Ckl0IG1heSBiZSBlbXB0eS4gPGJyPgpJdCBtYXkgaGF2ZSBhIHJvb3Qgbm9kZS4gQSBub2RlIGhhcyBhIGxldHRlciBhcyBkYXRhIGFuZCByZWZlcnMgdG8gYSBsZWZ0IGFuZCBhIHJpZ2h0IHN1YnRyZWUuIFRoZSBsZWZ0IGFuZCByaWdodCBzdWJ0cmVlcyBhcmUgYWxzbyBiaW5hcnkgdHJlZXMgb2YgbGV0dGVycy48YnI+Cjxicj4KSW4gdGhlIGdyYXBoaWNhbCByZXByZXNlbnRhdGlvbiBvZiBhIGJpbmFyeSB0cmVlIG9mIGxldHRlcnM6IDxicj4KRW1wdHkgdHJlZXMgYXJlIG9taXR0ZWQgY29tcGxldGVseS4gPGJyPgpFYWNoIG5vZGUgaXMgaW5kaWNhdGVkIGJ5IDxicj4KSXRzIGxldHRlciBkYXRhLCA8YnI+CkEgbGluZSBzZWdtZW50IGRvd24gdG8gdGhlIGxlZnQgdG8gdGhlIGxlZnQgc3VidHJlZSwgaWYgdGhlIGxlZnQgc3VidHJlZSBpcyBub25lbXB0eSwKPGJyPgpBIGxpbmUgc2VnbWVudCBkb3duIHRvIHRoZSByaWdodCB0byB0aGUgcmlnaHQgc3VidHJlZSwgaWYgdGhlIHJpZ2h0IHN1YnRyZWUgaXMgbm9uZW1wdHkuPGJyPgo8YnI+CkEgbGVhZiBpbiBhIGJpbmFyeSB0cmVlIGlzIGEgbm9kZSB3aG9zZSBzdWJ0cmVlcyBhcmUgYm90aCBlbXB0eS4gSW4gdGhlIGV4YW1wbGUgaW4gRmlndXJlIDEsIHRoaXMgd291bGQgYmUgdGhlIGZpdmUgbm9kZXMgd2l0aCBkYXRhIEIsIEQsIEgsIFAsIGFuZCBZLgo8YnI+Cjxicj4KVGhlIHByZW9yZGVyIHRyYXZlcnNhbCBvZiBhIHRyZWUgb2YgbGV0dGVycyBzYXRpc2ZpZXMgdGhlIGRlZmluaW5nIHByb3BlcnRpZXM6IDxicj4KSWYgdGhlIHRyZWUgaXMgZW1wdHksIHRoZW4gdGhlIHByZW9yZGVyIHRyYXZlcnNhbCBpcyBlbXB0eS4gPGJyPgpJZiB0aGUgdHJlZSBpcyBub3QgZW1wdHksIHRoZW4gdGhlIHByZW9yZGVyIHRyYXZlcnNhbCBjb25zaXN0cyBvZiB0aGUgZm9sbG93aW5nLCBpbiBvcmRlcgo8YnI+ClRoZSBkYXRhIGZyb20gdGhlIHJvb3Qgbm9kZSwgPGJyPgpUaGUgcHJlb3JkZXIgdHJhdmVyc2FsIG9mIHRoZSByb290"s left subtree,
The preorder traversal of the root's right subtree.

The preorder traversal of the tree in Figure 1 is KGCBDHQMPY.

A tree like the one in Figure 1 is also a binary search tree of letters. A binary search tree of letters is a binary tree of letters in which each node satisfies:

The root's data comes later in the alphabet than all the data in the nodes in the left subtree.

The root's data comes earlier in the alphabet than all the data in the nodes in the right subtree.

The problem:

Consider the following sequence of operations on a binary search tree of letters

Remove the leaves and list the data removed
Repeat this procedure until the tree is empty
Starting from the tree below on the left, we produce the sequence of trees shown, and then the empty tree

\

by removing the leaves with data

BDHPY
CM
GQ
K

Your problem is to start with such a sequence of lines of leaves from a binary search tree of letters and output the preorder traversal of the tree.

Input

The input will contain one or more data sets. Each data set is a sequence of one or more lines of capital letters.

The lines contain the leaves removed from a binary search tree in the stages described above. The letters on a line will be listed in increasing alphabetical order. Data sets are separated by a line containing only an asterisk ('*').

The last data set is followed by a line containing only a dollar sign ('$'). There are no blanks or empty lines in the input.

Output

For each input data set, there is a unique binary search tree that would produce the sequence of leaves. The output is a line containing only the preorder traversal of that tree, with no blanks.

Sample Input

BDHPY
CM
GQ
K
*
AC
B
$

Sample Output

KGCBDHQMPY
BAC

ÍêÕûC´úÂ룺

#include 
#include 
#include 

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

//BST²åÈ뺯Êý
void Insert( BiTree *t, Elemtype e )
{
	if( NULL == *t ) {
		(*t) = (BiTree)malloc(sizeof(BiTNode));
		(*t)->data = e;
		(*t)->lchild = (*t)->rchild = NULL;
	}
	
	else if( e < (*t)->data )
		Insert( &(*t)->lchild, e );

	else if( e > (*t)->data )
		Insert( &(*t)->rchild, e );
}

//BST´´½¨º¯Êý
void CreateBST( BiTree *t, Elemtype *a )
{
	(*t) = NULL;
	int len = strlen( a );
	for( int i=0; idata);
		PreOrderBST( t->lchild );
		PreOrderBST( t->rchild );
	}
}


int main()
{
	char a[50], b[50];
	int i = 0;
	int j = 0;
	BiTree t;
	char ch;
	
	while( EOF != scanf("%c",&ch) ) {
		if( '*' != ch && '$' != ch) 
			a[i++] = ch;
		else {
			for( ; i>0; i-- ) {  //¿ÉÒÔÓÅ»¯
				if( '\n' != a[i-1] )
					b[j++]=a[i-1];
			}
			b[j] = '\0';
			CreateBST( &t, b );
			PreOrderBST( t );
			printf("\n");
			if( '$' == ch )
				break;
			i = 0;
			j = 0;
		}
		
	}

	return 0;
}

²âÊÔÊý¾ÝÒÔ¼°²âÊÔ½á¹û


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved