Advertisement
forgelineage

Untitled

Nov 4th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. FHs_treeNode<E> splay( FHs_treeNode<E> root, E x )
  2. {
  3. FHs_treeNode rtTree = null;
  4. FHs_treeNode lftTree = null;
  5. FHs_treeNode rtTreeMin = null;
  6. FHs_treeNode lftTreeMax = null;
  7. int compareResult;
  8.  
  9. while (root != null)
  10. {
  11. compareResult = x.compareTo(root.data);
  12.  
  13. if (compareResult < 0)
  14. {
  15. if (root.lftChild == null)
  16. break;
  17.  
  18. //Zig Zig left
  19. if (x.compareTo(root.lftChild.data) < 0)
  20. {
  21. rotateWithLeftChild(root);
  22. if (root.lftChild == null)
  23. break;
  24. }
  25.  
  26. rtTreeMin.lftChild = root;
  27. rtTreeMin = root;
  28. root = root.lftChild;
  29. }
  30.  
  31. else if (compareResult > 0)
  32. {
  33. if (root.rtChild == null)
  34. break;
  35.  
  36. //Zig Zig left
  37. if (x.compareTo(root.rtChild.data) < 0)
  38. {
  39. rotateWithRightChild(root);
  40. if (root.lftChild == null)
  41. break;
  42. }
  43.  
  44. lftTreeMax.rtChild = root;
  45. lftTreeMax = root;
  46. root = root.rtChild;
  47. }
  48. else
  49. break;
  50. }
  51.  
  52. if (lftTree != null)
  53. {
  54. lftTreeMax.lftChild = root.lftChild;
  55. root.lftChild = lftTree;
  56. }
  57.  
  58. if (rtTree != null)
  59. {
  60. rtTreeMin.rtChild = root.rtChild;
  61. root.rtChild = rtTree;
  62. }
  63.  
  64. return root;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement