Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. public class UBST {
  2.   public Node root;
  3.  
  4.   public UBST() {
  5.     root = null;
  6.   }
  7.  
  8.   public UBST(Node r) {
  9.     root = r;
  10.   }
  11.  
  12.   public void insert(int d) {
  13.     Node temp = new Node(d);
  14.     Node current = root;
  15.     if (root == null)
  16.       root = temp;
  17.     else {
  18.       while (current != null && current.right != null) {
  19.         current = current.right;
  20.         temp.parent = current;
  21.       }
  22.       temp = current;
  23.       unbalance(temp);
  24.     }
  25.   }
  26.  
  27.   private void unbalance(Node t) {
  28.     if (t.parent == root) {
  29.       if (t.data < root.data) {
  30.         Node temp = root;
  31.         t = root;
  32.         t.right = temp;
  33.       }
  34.     }
  35.     else if (t.parent != root) {
  36.       if (t.data < t.parent.data) {
  37.         Node temp = t.parent;
  38.         t = t.parent;
  39.         t.right = temp;
  40.         unbalance(t);
  41.       }
  42.     }
  43.     else
  44.       unbalance(t.right);
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement