Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1.  
  2. /* In place BT to BST conversion , complexity quadratic time . any suggestions to improve it to nlogn time ? quicksort techinique wont work , niether will heapsort [cause heap is a almost complete binary tree structure , so thats where it gets its speed , from besided parent pointer would i think require extra space to be maintained , any modified heap technique ?? or any other ideas how to do this even if it takes same time or more time , also recursion is using space equivalent to number of nodes anyways , so dont see the point .. but discussion is good...  */
  3.  
  4.  
  5. node * maxTree(node * root)
  6. {
  7.     node * leftmax , *rightmax , *curmax;
  8.     if(root == NULL)
  9.         return NULL;
  10.     curmax = root;
  11.     leftmax = maxTree(root->left);
  12.     rightmax = maxTree(root->right);
  13.     if ( leftmax )
  14.         leftmax->data > curmax->data ? ( curmax = leftmax ) :  curmax ;
  15.     if ( rightmax )
  16.         rightmax->data > curmax->data ? (curmax = rightmax ) : curmax;
  17.    
  18.     return curmax;
  19. }
  20.  
  21. node * minTree(node * root)
  22. {
  23.     node * leftmin , *rightmin , *curmin;
  24.     if(root == NULL)
  25.         return NULL;
  26.     curmin = root;
  27.     leftmin = minTree(root->left);
  28.     rightmin = minTree(root->right);
  29.     if ( leftmin )
  30.         leftmin->data < curmin->data ? ( curmin = leftmin ) :  curmin ;
  31.     if ( rightmin )
  32.         rightmin->data < curmin->data ? (curmin = rightmin ) : curmin ;
  33.    
  34.     return curmin;
  35. }
  36.    
  37.  
  38.    
  39.  
  40.  
  41. void bin2bst(node * root)
  42. {
  43.     int temp;  // data type in tree
  44.     if(root == NULL)
  45.         return ;
  46.     node * leftT , *rightT ;
  47.     lefT = maxTree(root->left);
  48.     righT = minTree(root->right);
  49.     if (leftT != NULL && left->data > root->data){
  50.         temp = leftT->data;
  51.         leftT->data = root->data;
  52.         root->data = temp;
  53.     }
  54.    
  55.     if(rightT !=NULL && right -> data < root->data ){
  56.         temp = rightT->data;
  57.         rightT->data = root->data;
  58.         root->data = temp;
  59.     }
  60.    
  61.     bin2bst(root->left);
  62.     bin2bst(root->right);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement