Thread: Merging binaries trees into another one

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question Merging binaries trees into another one

    Good evening. I'm studying merging of two binaries trees and I was concerned about two instructions:

    Code:
    bitree_root(merge)->left = bitree_root(left);
    bitree_root(merge)->right = bitree_root(right);
    I would like to know if merging two binaries trees means to create one of the subtrees as the left node of the new (merged) binary tree and the other as the right node of it.

    Like this (please toggle to plain text):

    Code:
            root of merged tree
               /             \ 
              /               \ 
    left subtree           right subtree
    Code:
     
    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 binary 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;
    
    }
    Last edited by thames; 01-15-2013 at 02:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binaries
    By egoveneror2 in forum C Programming
    Replies: 2
    Last Post: 04-15-2008, 01:29 AM
  2. Binary Trees MINI MAXING, probability trees
    By curlious in forum General AI Programming
    Replies: 3
    Last Post: 09-30-2005, 10:57 AM
  3. Merging binary search trees
    By ibdutta in forum C Programming
    Replies: 2
    Last Post: 04-16-2004, 07:31 AM
  4. C and C++ binaries
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-10-2002, 12:36 PM
  5. traversing binary trees or partial trees
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 09:19 PM

Tags for this Thread