Comp2521-notes - notes PDF

Title Comp2521-notes - notes
Course IT
Institution University of New South Wales
Pages 32
File Size 904.8 KB
File Type PDF
Total Downloads 34
Total Views 207

Summary

Download Comp2521-notes - notes PDF


Description

  !"#

 Downloaded by Calvin Lee ([email protected])

   

 

  

&

&

void swap(int *xp, int *yp) { ! int temp = *xp; ! *xp = *yp; ! *yp = temp; ! }!

 

      

    

  

 



 

       

Tree insertAVL(Tree t, Item it) {! if (t == NULL)! return newNode(it);! if (it == data(t))! return t;! ! if (it < data(t))! left(t) = insertAVL(left(t), it);! else! right(t) = insertAVL(right(t), it) ! int hL = TreeHeight(left(t));! int hR = TreeHeight(right(t));! if ((hL - hR) > 1) {! if (it > data(left(t)))! left(t) = rotateLeft(left(t));! t = rotateRight(t);! } else if ((hR - hL) > 1) {! if (it < data(right(t)))!

 

Tree TreeDelete(Tree t, Item it) {! if (t != NULL) {! if (it < data(t))! ! left(t) = TreeDelete(left(t), it else if (it > data(t))! ! right(t) = TreeDelete(right(t) it);! else {! ! Tree new;! ! if (left(t) == NULL && right(t == NULL) ! ! new = NULL;! ! else if (left(t) == NULL) // if only right subtree, make it the new root! ! new = right(t);! ! else if (right(t) == NULL) / if only left subtree, make it the new root! ! new = left(t);! ! else // left(t) != NULL and right(t) != NUL

Tree insertAtRoot(Tree t, int it) {! if (t == NULL) {! t = insertTree(t, it);! return t;! } else if (it < t->value) {! t->left = insertAtRoot(t->left, it);! t = rotateRight(t);! } else if (it > t->value) {! t->right = insertAtRoot(t->right, it);! t = rotateLeft(t);! } else { return t; }! }!

Tree partition(Tree t, int i) {! if (t != NULL) {! assert(0 m) {! ! right(t) = partition(right(t), i-m-1);! ! t = rotateLeft(t);! }! }! return t;! }

Tree rebalance(Tree t) {! int n = TreeNumNodes(t);! if (n >= 3) {! t = partition(t, n/2); // put node with median key at root! left(t) = rebalance(left(t)); // then rebalance each subtree! right(t) = rebalance(right(t));! }! return t;! }

 



 

    

  





  

 







  

   





 

    

    

 

  

   



          

  

  

    



  

 

    

   

   

   

    



      



 



   







  

 

  



      

 





         

   





   

 

    

        

 

 





    







...


Similar Free PDFs
Notes
  • 18 Pages
Notes
  • 12 Pages
Notes
  • 61 Pages
Notes
  • 35 Pages
Notes
  • 19 Pages
Notes
  • 70 Pages
Notes
  • 6 Pages
Notes
  • 35 Pages
Notes
  • 29 Pages
Notes
  • 70 Pages
Notes
  • 6 Pages
Notes
  • 19 Pages
Notes
  • 32 Pages
Notes
  • 28 Pages
Notes
  • 56 Pages