Advertisement
forgelineage

Untitled

Nov 4th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1.  
  2.  
  3. initialize the rightRree, leftTree, rightTreeMin, and leftTreeMax to null
  4. loop while root != null (root should not become null, but this protects against null parameter)
  5. if x < root
  6. check for root's left child null. If so, x not in tree, break loop.
  7. if x < root's left child we have zig zig (left) so do a single rotate (left) at root
  8. check for (new) root's left child null. If so, x not in tree, break loop.
  9. add root to rightRree at its minimum node - update the rightTreeMin to point to this node
  10. update the new working root: set root to root's left child
  11. if x > root
  12. check for root's right child null. If so, x not in tree, break loop.
  13. if x > root's right child we have zig zig (right) so do a single rotate (right) at root
  14. check for (new) root's right child null. If so, x not in tree, break loop.
  15. add root to leftTree at its maximum node - update the leftTreeMax to point to this node
  16. update the new working root: set root to root's right child
  17. otherwise we have found x at root. break.
  18. reassemble
  19. if the left tree is not null, hang root's left child onto its maximum and set root's left child = the left tree.
  20. if the right tree is not null, hang root's right child onto its minimum and set root's right child = the right tree.
  21. return root
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement