Advertisement
Guest User

Untitled

a guest
Oct 11th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.77 KB | None | 0 0
  1.  
  2.  
  3. public class MTFBinaryTree extends BinaryTree implements Tree{
  4.  
  5.  
  6.         private Node root;
  7.         private int size;
  8.  
  9.         public MTFBinaryTree() {
  10.             // Initialize the size to zero.
  11.             this.size = 0;
  12.         }
  13.  
  14.  
  15.         public int size() {
  16.             // Just return the size.
  17.             return this.size;
  18.         }
  19.  
  20.  
  21.         public Node insertRoot(int k, String v) {
  22.             // TODO Implement the insertion at root method.
  23.             insertLeaf(k, v);
  24.             Node w = get(k);
  25.             return insertRoot(w);
  26.         }
  27.        
  28.         private Node insertRoot(Node n)
  29.         {
  30.             if(n == root)
  31.                 return n;
  32.             else
  33.             {
  34.                 while(n != root)
  35.                 {
  36.                     rotate(n);
  37.                 }
  38.                 return n;
  39.             }
  40.         }
  41.        
  42.         public void rotate (Node n)
  43.         {
  44.             if (n.getKey() < n.getParent().getKey())
  45.             {
  46.                 rotateRight(n);
  47.             }
  48.             else
  49.             {
  50.                 rotateLeft(n);
  51.             }
  52.         }
  53.  
  54.         public void rotateRight(Node n) {
  55.             if (n.getParent() == root) {
  56.                 Node r = n.getRight();
  57.                 n.setRight(n.getParent());
  58.                 root = n;
  59.                 root.getRight().setLeft(r);
  60.             } else {
  61.                 Node p = n.getParent();
  62.                 Node r = n.getRight();
  63.                 n.setParent(p.getParent());
  64.                 n.setRight(p);
  65.                 p.setLeft(r);
  66.             }
  67.         }
  68.  
  69.         public void rotateLeft(Node n) {
  70.             if (n.getParent() == root) {
  71.                 Node l = n.getLeft();
  72.                 n.setLeft(n.getParent());
  73.                 root = n;
  74.                 root.getLeft().setRight(l);
  75.             } else {
  76.                 Node p = n.getParent();
  77.                 Node l = n.getLeft();
  78.                 n.setParent(p.getParent());
  79.                 n.setLeft(p);
  80.                 p.setRight(l);
  81.             }
  82.         }
  83.  
  84.         public void recursiveInsertLeaf(Node current, Node newNode) {
  85.             // Check whether the node is less than or greater than or equal
  86.             // to the node we are inserting.
  87.             int compare = newNode.compareTo(current);
  88.  
  89.             // If less than, try to insert it to the left, or recurse to the left
  90.             // subtree.
  91.             if (compare < 0) {
  92.                 if (current.getLeft() == null) {
  93.                     current.setLeft(newNode);
  94.                     size++;
  95.                 }
  96.                 else
  97.                     recursiveInsertLeaf(current.getLeft(), newNode);
  98.                 // If equal, ignore. We just ignore duplicates.
  99.             } else if (compare == 0) {
  100.                 return;
  101.                 // If greater than, try to insert to the left, or recurse to the
  102.                 // right subtree.
  103.             } else {
  104.                 if (current.getRight() == null) {
  105.                     current.setRight(newNode);
  106.                     size++;
  107.                 }
  108.                 else
  109.                     recursiveInsertLeaf(current.getRight(), newNode);
  110.             }
  111.         }
  112.  
  113.         public Node insertLeaf(int k, String v) {
  114.             // Create a new node, representing the key-value pair
  115.             Node n = new BTNode(k, v);
  116.  
  117.             // If the tree doesn't have a root, insert at the root.
  118.             if (this.root == null) {
  119.                 this.root = n;
  120.                 size++;
  121.                 return n;
  122.             }
  123.  
  124.             // Otherwise, recursively find the correct location to insert.
  125.             recursiveInsertLeaf(this.root, n);
  126.  
  127.             // Return the newly inserted node.
  128.             return n;
  129.         }
  130.  
  131.         public Node recursiveGet(int k, Node n) {
  132.             // Base Case - Null Node
  133.             if (n == null)
  134.                 return null;
  135.  
  136.             int currentKey = n.getKey();
  137.             Node temp = n;
  138.  
  139.             // Check if the key matches the node we are looking at.
  140.             if (k == currentKey)
  141.             {
  142.                 if(n == root)
  143.                     return n;
  144.                 else
  145.                 {
  146.                     while(n != root)
  147.                     {
  148.                         rotate(n);
  149.                     }
  150.                 }
  151.                 return temp;
  152.             }
  153.             // If the key is less than what we are looking for, recurse to the left subtree,
  154.             else if (k < currentKey)
  155.                 return recursiveGet(k, n.getLeft());
  156.             // Otherwise, recurse to the right subtree.
  157.             else
  158.                 return recursiveGet(k, n.getRight());
  159.         }
  160.  
  161.         public Node get(int k) {
  162.             // Use the recursive method to get the value associated with a key.
  163.             return recursiveGet(k, this.root);
  164.         }
  165.  
  166.         public int recursiveFind(int k, Node n) {
  167.             // Base Case - Null Node
  168.             if (n == null)
  169.                 return -1;
  170.  
  171.             int currentKey = n.getKey();
  172.             int result = 1;
  173.  
  174.             // Check if the key at this node is the one we are looking for.
  175.             if (k == currentKey)
  176.             {
  177.                 if(n == root)
  178.                     return 1;
  179.                 else
  180.                 {
  181.                     while(n != root)
  182.                     {
  183.                         rotate(n);
  184.                     }
  185.                 }
  186.                 return 1;
  187.             }
  188.             // If the key is less than the current node, recurse to the left.
  189.             else if (k < currentKey)
  190.                 result = recursiveFind(k, n.getLeft());
  191.             // Otherwise, we recurse to the right.
  192.             else
  193.                 result = recursiveFind(k, n.getRight());
  194.  
  195.             if (result == -1)
  196.                 // We didn't find the node we are looking for, return -1.
  197.                 return -1;
  198.             else
  199.                 // Return the number of steps it took to find the current node.
  200.                 return 1+result;
  201.         }
  202.  
  203.         public int find(int k) {
  204.             // We use a recursive find method, passing the root in
  205.             // as the first argument.
  206.             // We need to return the number of comparisons made.
  207.             return recursiveFind(k, this.root);
  208.         }
  209.  
  210.  
  211.         public void remove(Node n) {
  212.             // TODO Implement the removal method
  213.             if (n == null)
  214.                     return;
  215.            
  216.  
  217.             // leaf
  218.             else if (n.getLeft() == null && n.getRight() == null)
  219.             {
  220.                 if(n.getParent().getLeft() == n)
  221.                     n.getParent().setLeft(null);
  222.                 else {
  223.                     n.getParent().setRight(null);}
  224.                 n = null;
  225.             }
  226.             //1 child
  227.             else if (n.getLeft() == null && n.getRight() != null)
  228.             {
  229.                     Node p = n.getParent();
  230.                     Node r = n.getRight();
  231.                     r.setParent(p);
  232.                     n = null;
  233.                     size--;
  234.             }
  235.             else if (n.getLeft() != null && n.getRight() == null)
  236.             {
  237.                     Node p = n.getParent();
  238.                     Node r = n.getLeft();
  239.                     r.setParent(p);
  240.                     n = null;
  241.                     size--;
  242.             }
  243.  
  244.  
  245.             //root
  246.             else if (n.getParent() == null)
  247.             {
  248.                     Node left = n.getLeft();
  249.                     Node temp = inorderSuccessor(n);
  250.                     remove(temp);
  251.                     Node right = n.getRight();
  252.  
  253.                     root = temp;
  254.                     root.setLeft(left);
  255.                     root.setRight(right);
  256.                     n = null;
  257.                     size--;
  258.             }
  259.  
  260.             //2children
  261.             else
  262.             {
  263.                     Node left = n.getLeft();
  264.                     Node temp = inorderSuccessor(n);
  265.                     remove(inorderSuccessor(n));
  266.                     Node right = n.getRight();
  267.  
  268.                     if(n.getParent().getLeft() == n)
  269.                             n.getParent().setLeft(temp);
  270.                     else
  271.                             n.getParent().setRight(temp);
  272.                     temp.setLeft(left);
  273.                     temp.setRight(right);
  274.                     n = null;
  275.                     size--;
  276.  
  277.  
  278.             }
  279.     }
  280.  
  281.         public Node inorderSuccesor(Node v){
  282.  
  283.             if (v.getRight() != null)
  284.             {
  285.                 Node w = v.getRight();
  286.                 while (w.getLeft() != null)
  287.                 {
  288.                     w = w.getLeft();
  289.                 }
  290.                 return w;
  291.             }
  292.             else
  293.             {
  294.                 Node p = v.getParent();
  295.                 Node c = v;
  296.                 while (c != p.getLeft())
  297.                 {
  298.                     c = p;
  299.                     p = p.getParent();
  300.                 }
  301.                 return p;
  302.             }
  303.         }
  304.  
  305.  
  306.         private void recursivePrintTree(Node n) {
  307.             // Base Case - Null Node
  308.             if (n == null)
  309.                 return;
  310.  
  311.             Node left = n.getLeft();
  312.             Node right = n.getRight();
  313.  
  314.             // If there is a left child, print out the connection and
  315.             // recurse.
  316.             if (left != null) {
  317.                 System.out.println(n.getKey() + " -- " + left.getKey() + ";");
  318.                 recursivePrintTree(left);
  319.             }
  320.             // If there is a right child, print out the connection and
  321.             // recurse.
  322.             if (right != null) {
  323.                 System.out.println(n.getKey() + " -- " + right.getKey() + ";");
  324.                 recursivePrintTree(right);
  325.             }
  326.         }
  327.  
  328.         public void printTree() {
  329.             // Print out the surrounding structure for the
  330.             // graphviz format.
  331.             System.out.println("graph {");
  332.             // Recursively print out the structure of the tree by an pre-order
  333.             // traversal
  334.             recursivePrintTree(this.root);
  335.             System.out.println("}");
  336.         }
  337.  
  338.         public Node getRoot() {
  339.             // Just return the root, or null if there is no root.
  340.             return this.root;
  341.         }
  342.  
  343.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement