Advertisement
Guest User

Untitled

a guest
May 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. TREE* mergeTrees(TREE* root1 ,TREE* root2) // O(h), h = height of one of the trees
  2. {
  3. NODE2* newRoot, fatherMinimunRight;
  4. TREE* newTree;
  5.  
  6. // find the minimun of the right tree
  7. newRoot = findMinimum(root2);
  8.  
  9. // the father of the minimumRight should point to null.
  10. fatherNewRoot = father(root2->head.left, newRoot->key);
  11. fatherNewRoot->left = NULL;
  12.  
  13. // newRoot should point from right to root2
  14. newRoot->right = root2->head.left;
  15.  
  16. // newRoot should point from left to root1
  17. newRoot->left = root1->head.left;
  18.  
  19. // create the new tree
  20. if (!(newTree = (TREE*)malloc(sizeof(TREE)))) return NULL; // allocation error
  21.  
  22. if (T_init(newTree) == FALSE)
  23. {
  24. return NULL;
  25. }
  26.  
  27. newTree->head.left = newRoot;
  28.  
  29. return newTree;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement