1、问题描述
二叉树A和B的每个节点的数据(int型数据)存储在不同文件中,存储方式为前序遍历和中序遍历,根据这两种遍历重建二叉树,并且判断二叉树A是否包含二叉树B。
1、算法描述
(1)首先将节点数据的前序遍历和中序遍历序列读入数组
(2)分别根据各自的前序遍历和中序遍历重建二叉树A和B
(3)判断B是否在A中
代码:
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
enum COMMON_SIZE
{
?BUF_MAX_SIZE = 1024,
?MAX_NUM_LEN = 10,
};
enum ERROR_VALUE
{
?INPUT_PARAM_ERROR = -1,
?OPEN_FILE_ERROR = -2,
?FILE_NOT_EXSIT = -3,
?FILE_IS_EMTPY = -4,
?ALLOCA_FAILURE = -5,
?REBUILD_BTREE_ERROR = -6,
?NUM_OVERFLOW_ERROR = -7,
};
typedef struct BTreeNode_t_{
?int m_nValue;
?struct BTreeNode_t_ *m_pLeft;
?struct BTreeNode_t_ *m_pRight;
} BTreeNode_t;
void release_btree( BTreeNode_t *pRoot){
?if( pRoot == NULL )
? return;
?if( pRoot->m_pLeft )
? release_btree( pRoot->m_pLeft);
?if( pRoot->m_pRight)
? release_btree( pRoot->m_pRight);
?free(pRoot);
?pRoot = NULL;
?return;
}
BTreeNode_t * reconstruct_btree_by_preinorder( int *pPreOrder, int *pInOrder, int tree_nodes_total){
?if( pPreOrder == NULL || pInOrder == NULL ){
? fprintf(stderr, "ERROR: while construct btree by preinorder\n");
? return NULL;
?}?
?
?int m_nValue = pPreOrder[0];
?int root_data_index = -1;
?int i = 0;
?for( i = 0; i < tree_nodes_total; ++i ){
? if( pPreOrder[0] == pInOrder[i] ){
? ?root_data_index = i;
? ?break;
? }
?}
?if( root_data_index == -1 ){
? fprintf(stderr, "note: pPreOrder[0] is %d, pInOrder[0] is %d\n",
? ?pPreOrder[0], pInOrder[0]);
? fprintf(stderr, "note: root_data_index is -1, total nodes: %d\n", tree_nodes_total);
? return NULL;
?}
?BTreeNode_t *pRoot = NULL;
?pRoot = (BTreeNode_t *) malloc( sizeof( BTreeNode_t ));
?if( pRoot == NULL ){
? fprintf(stderr, "ERROR: can't alloca btree node: %d\n", m_nValue);
? return NULL;
?}
?pRoot->m_nValue = m_nValue;
?pRoot->m_pLeft = NULL;
?pRoot->m_pRight = NULL;
?int left_tree_len = root_data_index;
?int right_tree_len = tree_nodes_total - left_tree_len - 1;
?int *pleft_preorder = pPreOrder + 1;
?int *pright_preorder = pPreOrder + 1 + left_tree_len;
?int *pleft_inorder = pInOrder;
?int *pright_inorder = pInOrder + left_tree_len + 1;
?if( left_tree_len > 0 )
?{
? pRoot->m_pLeft = reconstruct_btree_by_preinorder( pleft_preorder, pleft_inorder, left_tree_len);
? if( pRoot->m_pLeft == NULL ){
? ?fprintf(stderr, "ERROR: failure to rebuild leftree, now release data: %d\n",
? ? m_nValue);
? ?release_btree( pRoot);
? ?pRoot = NULL;
? ?return NULL;
? }
?}
?if( right_tree_len > 0 ){
? pRoot->m_pRight = reconstruct_btree_by_preinorder( pright_preorder, pright_inorder, right_tree_len );
? if( pRoot->m_pRight == NULL ){
? ?fprintf(stderr, "ERROR: failure to right tree, now release data: %d\n",
? ? m_nValue);
? ?release_btree( pRoot );
? ?pRoot = NULL;
? ?return NULL;
? }
?}
?return pRoot;
}
int get_traver_data( char *buf, int *pOrder, int *order_len ){
?if( buf == NULL || pOrder == NULL ){
? return INPUT_PARAM_ERROR;
?}
?char *ptr = buf;
?char *pNumStart = ptr;
?int i = 0;
?int numLen = 0;
?int get_no_num = 0;
?int flag = 0;
?fprintf(stderr, "note: now enter get_traver_data()\n");
?while( *ptr != '\0' ){
? if( ( *ptr >= '0' ) && ( *ptr <= '9') ){
? ?++numLen;
? } else? if( *ptr == ' ' ){
? ?*ptr = '\0';
? ?if( numLen > 0 && numLen <= MAX_NUM_LEN ){
? ? pOrder[i] = atoi( ptr - numLen );
? ? fprintf(stderr, "note: num is: %d\n", pOrder[i]);
? ? ++i;?
? ?}
? ?else if ( numLen > MAX_NUM_LEN){
? ? flag = NUM_OVERFLOW_ERROR;
? ? break;
? ?}
? ?numLen = 0;
? ?
? } else{
? ?get_no_num = -1;
? ?break;
? }
? ++ptr;
?}
?if( numLen != 0 ){
? pOrder[i] = atoi( ptr - numLen);
? fprintf(stderr, "note: num is: %d\n", pOrder[i]);
?}
?if( get_no_num != 0 )
? return g