Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- initialize the rightRree, leftTree, rightTreeMin, and leftTreeMax to null
- loop while root != null (root should not become null, but this protects against null parameter)
- if x < root
- check for root's left child null. If so, x not in tree, break loop.
- if x < root's left child we have zig zig (left) so do a single rotate (left) at root
- check for (new) root's left child null. If so, x not in tree, break loop.
- add root to rightRree at its minimum node - update the rightTreeMin to point to this node
- update the new working root: set root to root's left child
- if x > root
- check for root's right child null. If so, x not in tree, break loop.
- if x > root's right child we have zig zig (right) so do a single rotate (right) at root
- check for (new) root's right child null. If so, x not in tree, break loop.
- add root to leftTree at its maximum node - update the leftTreeMax to point to this node
- update the new working root: set root to root's right child
- otherwise we have found x at root. break.
- reassemble
- if the left tree is not null, hang root's left child onto its maximum and set root's left child = the left tree.
- if the right tree is not null, hang root's right child onto its minimum and set root's right child = the right tree.
- return root
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement