数据结构二叉树的实现,前序、中序、后序遍历

typedef struct BitTreeNode_
{
void *data;
struct BitTreeNode_ *left;
struct BitTreeNode_ *right;
}BitTreeNode;
typedef struct BitTree_
{
int size;
BitTreeNode *root;
int (*compare)(const void *key1,const void *key2);
void (*destory)(void *data);
}BitTree;


void bitree_init(BitTree *tree,void (*destory)(void *data));
void bitree_destory(BitTree *tree);


void bitree_ins_left(BitTree *tree,BitTreeNode *node,const void *data);
void bitree_ins_right(BitTree *tree,BitTreeNode *node,const void *data);


void bitree_remove_left(BitTree *tree,BitTreeNode *node);
void bitree_remove_right(BitTree *tree,BitTreeNode *node);


//合并左右子数,合并为以data为跟的新二叉树
int bitree_merge(BitTree *merge,BitTree *left,BitTree *right,const void *data);




#define bitree_size(tree) ((tree)->size)
#define bitree_root(tree)  ((tree)->root)
#define bitree_is_eob(node) ((node)==NULL) //判断分支是否结束
#define bitree_is_leaf(node) ((node)->left==NULL &&(node)->right==NULL)//判断是不是叶子节点
#define bitree_data(node) ((node)->data) //获取数据域
#define bitree_left(node) ((node)->left)//获取左结点
#define bitree_right(node) ((node)->right) //获取右结点


void bitree_init(BitTree *tree,void (*destory)(void *data))
{
tree->size=0;
tree->root=NULL;
tree->destory=destory;
return;
}
void bitree_destory(BitTree *tree)
{
bitree_remove_left(tree,NULL);
memset(tree,0,sizeof(BitTree));
return;
}
void bitree_ins_left(BitTree *tree,BitTreeNode *node,const void *void)
{
BitTreeNode *new_node,**position;
if(node==NULL)
{
if(bittree_size(tree)>0)
{
return -1;
}
position=&tree->root;
}
else
{
if(bitree_left(node)!=NULL)
{
return -1;
}
position=&tree->left;
}

new_node=(BitTreeNode *)malloc(sizeof(BitTreeNode));
if(new_node==NULL)
{
return -1;
}
new_node->data=(void *)data;
new_node->left=NULL;
new_node->right=NULL;

*position=new_node;

tree->size++;
return 0;
}
int bitree_ins_right(BiTree *tree, BiTreeNode *node, const void *data)
{
    BiTreeNode *new_node, **position;


    if( node == NULL )
    {
        if( bitree_size(tree) > 0 )
            return -1;
        
        position = &tree->root;
    }
    else
    {
        if( bitree_right(node) != NULL )
            return -1;


        position = &node->right;
    }


    /* allocate the storage for the node. */
    new_node = (BiTreeNode *)malloc(sizeof(BiTreeNode));
    if( new_node == NULL )
        return -1;


    new_node->data = (void *)data;
    new_node->left = NULL;
    new_node->right = NULL;


    *position = new_node;
    
    tree->size++;
    return 0;
}
/* bitree_rem_left */
void bitree_rem_left(BiTree *tree, BiTreeNode *node)
{
    BiTreeNode **position;


    /* Do not allow removal from an empty tree. */
    if( bitree_size(tree) == 0 )
        return;


    if( node == NULL )
    {
        position = &tree->root;
    }
    else
    {
        position = &node->left;
    }


    /* Remove the nodes. */
    if( *position != NULL )
    {
        bitree_rem_left(tree, *position);
        bitree_rem_right(tree, *position);


        if( tree->destroy != NULL )
        {
            tree->destroy((*position)->data);
        }
        free(*position);
        *position = NULL;


        /* adjust the size */
        tree->size--;
    }
    return;
}


/* bitree_rem_right */
void bitree_rem_right(BiTree *tree, BiTreeNode *node)
{
    BiTreeNode **position;


    if( bitree_size(tree) == 0 )
        return;


    if( node == NULL )
    {
        position = &tree->root;
    }
    else
    {
        position = &node->right;
    }


    /* Remove the nodes */
    if( *position != NULL )
    {
        bitree_rem_left(tree, *position);
        bitree_rem_right(tree, *position);


        if( tree->destroy != NULL )
        {
            tree->destroy((*position)->data);
        }


        free(*position);
        *position = NULL;
        tree->size--;
    }


    return;
}


/* bitree_merge */
int bitree_merge(BiTree *merge, BiTree *left, BiTree *right, const void *data)
{
    /* Initialize the merged tree. */
    bitree_init(merge, left->destroy);


    /* Insert the data for the root node of the merged tree */
    if( bitree_ins_left(merge, NULL, data) != 0 )
    {
        bitree_destroy(merge);
        return -1;
    }


    /* Merge the two binary trees into a single binary tree */
    bitree_root(merge)->left = bitree_root(left);
    bitree_root(merge)->right = bitree_root(right);
    
    /* Adjust the size of the new tree */
    merge->size = merge->size + bitree_size(left) + bitree_size(right);


    /* Do not let the original trees access the merged nodes. */
    left->root = NULL;
    left->size = 0;
    right->root = NULL;
    right->size = 0;


    return 0;
}
/* preorder */
int preorder(const BiTreeNode *node, List *list)
{
    if( !bitree_is_eob(node) )
    {
    
        if( list_ins_next(list, list_tail(list), bitree_data(node) ) != 0 )
        {
            return -1;
        }
        
        if( !bitree_is_eob( bitree_left(node) ) )
        {
            if( preorder(bitree_left(node), list) != 0 )
                return -1;
        }
        
        if( !bitree_is_eob( bitree_right(node) ) )
        {
            if( preorder(bitree_right(node), list) != 0 )
                return -1;
        }
    }
    return 0;
}




/* inorder */
int inorder(const BiTreeNode *node, List *list)
{
    if( !bitree_is_eob(node) )
    {
        if( !bitree_is_eob(bitree_left(node)) )
        {
            if( inorder( bitree_left(node), list ) != 0 )
                return -1;
        }
        
        if( list_ins_next(list, list_tail(list), bitree_data(node)) != 0 )
        {
            return -1;
        }
        
        if( !bitree_is_eob(bitree_right(node)) )
        {
            if( inorder( bitree_right(node), list ) != 0 )
                return -1;
        }
    }
    return 0;
}
/* postorder */
int postorder(const BiTreeNode *node, List *list)
{
    if( !bitree_is_eob(node) )
    {
        if( !bitree_is_eob(bitree_left(node)) )
        {
            if( postorder(bitree_left(node), list) != 0 )
                return -1;
        }
        
        if( !bitree_is_eob(bitree_right(node)) )
        {
            if( postorder(bitree_right(node), list) != 0 )
                return -1;
        }
        
        if( list_ins_next(list, list_tail(list), bitree_data(node)) != 0 )
            return -1;
    }


    return 0;
}


/* destroy */
void destroy(void *data)
{
    free(data);
    return;
}


/* create_tree */
int create_tree(BiTree *tree, BiTreeNode *node)
{
    
    int ret;
    int *int_ptr = NULL;
    char ch;
    
    scanf("%c", &ch);


    if( ch == '#' )
    {
        return 0;
    }
    int_ptr = (int *)malloc(sizeof(int));
    if( int_ptr == NULL )
        return -1;


    *int_ptr = ch-48;


    if( node == NULL )
    {
        bitree_init(tree, destroy);
        ret = bitree_ins_left(tree, NULL, (void *)int_ptr);
        if( ret != 0 )
        {
            free(int_ptr);
            return -1;
        }
        printf("root is %d\n", *(int *)bitree_data(tree->root));
        create_tree(tree, tree->root);
    }
    else
    {
        //insert the data into left tree 
        ret = bitree_ins_left(tree, node, (void *)int_ptr);
        if( ret != 0 )
        {
            free(int_ptr);
            return -1;
        }
        printf("node: %d  's left node is :%d\n", *(int *)bitree_data(node), *(int *)bitree_data(node->left));
        ret = create_tree(tree, node->left);


        scanf("%c", &ch);


        if( ch == '#')
            return 0;


        int_ptr = (int *)malloc(sizeof(int));
        if( int_ptr == NULL )
            return -1;


        *int_ptr = ch-48;
        // insert the data into right tree.
        ret = bitree_ins_right(tree, node, (void *)int_ptr);
        if( ret != 0 )
        {
            free(int_ptr);
            return -1;
        }
        printf("node: %d  's right node is :%d\n", *(int *)bitree_data(node), *(int *)bitree_data(node->right));
        ret = create_tree(tree, node->right);
    }


    return 0;
}


/* main */
int main(int argc, char **argv)
{
    int ret;
    int *int_ptr;
    BiTree tree1, tree2, tree_merge;
    List list;
    ListElmt *member;
    BiTreeNode *nd;


    /* tree1 as follows :
                1
              /   \
             2     5
            / \   / \
           3  4  6   7
    */
    create_tree(&tree1, NULL); //input "123#4#56#7#"
    printf("\nstep1:tree1 build success\n");


    /* tree2 as follows:      
                0
              /   \
             8     9
            / \   / 
           6   7 3
     */
    int_ptr = NULL;
    create_tree(&tree2, NULL); //input "086#7#93###"
    printf("step2:tree2 build success\n");


    int_ptr = (int *)malloc(sizeof(int));
    if( int_ptr == NULL )
        return -1;
    *int_ptr = 11;
 
    /* after merged as follow( by tree1 and tree2 ) :
                                11
                          /             \
                         1               0
                     /       \         /    \
                    2         5       8      9
                 /    \    /    \   /   \   /  \
                3      4  6      9 6     7 3    NULL
    */
    ret = bitree_merge(&tree_merge, &tree1, &tree2, int_ptr);
    printf("step3: after merged: there are %d number nodes in the tree_merge.\n", bitree_size(&tree_merge));
    


    /* after remove the right tree:
                   11 
                 /   \
                1     NULL
              /   \
             2     5
            / \   / \
           3  4  6   7
     */                   
    printf("\nstep4: remove the right tree in tree_merge.\n");
    bitree_rem_right(&tree_merge,  bitree_root(&tree_merge) );
    printf("after remove the right tree, there are %d number nodes in the tree_merge.\n", bitree_size(&tree_merge));
    
    printf("\nstep5: preorder traverse the tree and insert the nodes into the list\n");
    list_init(&list, destroy);
    ret = preorder( bitree_root(&tree_merge), &list );


    printf("according to the sequence of the preorder traversing the tree:\n");
    for(member = list_head(&list); member != NULL; member = list_next(member) )
    {
       printf("%d ", *(int *)list_data(member)); 
    }
    printf("\n");




    printf("\nsetp6: inorder traverse the tree and insert the nodes into the list\n");
    list_init(&list, destroy);


    ret = inorder( bitree_root(&tree_merge), &list );


    printf("according to the sequence of the inorder traversing the tree:\n");
    for(member = list_head(&list); member != NULL; member = list_next(member) )
    {
       printf("%d ", *(int *)list_data(member)); 
    }
    printf("\n");


    printf("\nsetp7: postorder traverse the tree and insert the nodes into the list\n");
    list_init(&list, destroy);
    
    ret = postorder( bitree_root(&tree_merge), &list );
    printf("according to the sequence of the postorder traversing the tree:\n");
    for(member = list_head(&list); member != NULL; member = list_next(member) )
    {
       printf("%d ", *(int *)list_data(member)); 
    }
    printf("\n");


    printf("\nstep8: delete all the nodes in the tree.\n");
    bitree_rem_left( &tree_merge, NULL );
    printf("there are %d number nodes.\n", bitree_size(&tree_merge) );
    
    bitree_destroy(&tree_merge);


    return 0;
}

你可能感兴趣的:(数据结构)