Advertisement
tusaveeiei

Untitled

Nov 11th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.20 KB | None | 0 0
  1. package hw7;/* HW7
  2.  * Due: 12 November 2017
  3.  * Problem Header Hash Code: 253b66d5c07ac9c3474e83723d5bf028
  4. */
  5.  
  6. public class SplayTree extends BTreePrinter {
  7.     Node root;
  8.  
  9.     public SplayTree() {
  10.  
  11.     }
  12.  
  13.     public SplayTree(Node root) {
  14.  
  15.         if (root.parent != null) { // To make sure the root has no parent
  16.             if (root.key < root.parent.key) {
  17.                 root.parent.left = null;
  18.             } else {
  19.                 root.parent.right = null;
  20.             }
  21.             root.parent = null;
  22.         }
  23.         this.root = root;
  24.     }
  25.  
  26.     public void printTree() {
  27.         super.printTree(root);
  28.     }
  29.  
  30.     public Node find(int search_key) {
  31.         splay(findWithoutSplaying(search_key));
  32.         return root;
  33.     }
  34.  
  35.     // This function is already complete, no need to modify
  36.     public Node findWithoutSplaying(int search_key) {
  37.         return find(root, search_key);
  38.     }
  39.  
  40.     // This function is already complete, no need to modify
  41.     private static Node find(Node node, int search_key) {
  42.         if (search_key == node.key) {
  43.             return node;
  44.         } else if ((search_key < node.key) && (node.left != null)) {
  45.             return find(node.left, search_key);
  46.         } else if ((search_key > node.key) && (node.right != null)) {
  47.             return find(node.right, search_key);
  48.         } else {
  49.             return null;
  50.         }
  51.     }
  52.  
  53.     // This function is already complete, no need to modify
  54.     private Node findMin() {
  55.         return findMin(root);
  56.     }
  57.  
  58.     // This function is already complete, no need to modify
  59.     private static Node findMin(Node node) {
  60.         if (node.left == null) {
  61.             return node;
  62.         } else {
  63.             return findMin(node.left);
  64.         }
  65.     }
  66.  
  67.     // This function is already complete, no need to modify
  68.     public void insert(int key) {
  69.         if (root == null) {
  70.             root = new Node(key);
  71.         } else {
  72.             insert(this, root, key);
  73.         }
  74.     }
  75.  
  76.     // Fix this function to have splaying feature
  77.     private static void insert(SplayTree tree, Node node, int key) {
  78.         if (key == node.key) {
  79.             System.out.println("Duplicated key:" + key);
  80.         } else if (key < node.key) {//Go left
  81.             if (node.left == null) {
  82.                 node.left = new Node(key);
  83.                 node.left.parent = node;
  84.                 tree.splay(node.left);
  85.             } else {
  86.                 insert(tree, node.left, key);
  87.             }
  88.         } else { // Go right
  89.             if (node.right == null) {
  90.                 node.right = new Node(key);
  91.                 node.right.parent = node;
  92.                 tree.splay(node.right);
  93.             } else {
  94.                 insert(tree, node.right, key);
  95.             }
  96.         }
  97.     }
  98.  
  99.  
  100.     public void delete(int key) {
  101.         // To delete a key in a splay tree, do the following steps
  102.         // 1. splay node with the key to the root of tree1
  103.         // 2. create another tree using the node of the right-subtree (tree2)
  104.         // 3. Find min of the right-subtree and splay to the root
  105.         // 4. Take left-subtree of the tree1 and hang as the left subtree of the tree2
  106.         // 5. Reassign a new root (root of the tree2)
  107.         splay(find(key));
  108.         SplayTree t1 = new SplayTree(root.left);
  109.         SplayTree t2 = new SplayTree(root.right);
  110.         t2.splay(t2.findMin());
  111.         t2.root.left = t1.root;
  112.         root = t2.root;
  113.     }
  114.  
  115.     // Use this function to call zigzig or zigzag
  116.     public void splay(Node node) {
  117.         if (node != null && node != root) {
  118.             if (node.parent == root) {
  119.                 // Do something (just add one line of code)
  120.                 zig(node);
  121.             } else {
  122.                 if (node.key < node.parent.key) {
  123.                     if (node.parent.key < node.parent.parent.key) {
  124.                         // Left outer case
  125.                         // Do something (just add one line of code)
  126.                         zigzig(node);
  127.                     } else {
  128.                         // Left inner case
  129.                         // Do something (just add one line of code)
  130.                         zigzag(node);
  131.                     }
  132.                 } else {
  133.                     if (node.parent.key > node.parent.parent.key) {
  134.                         // Right outer case
  135.                         // Do something (just add one line of code)
  136.                         zigzig(node);
  137.                     } else {
  138.                         // Do something (just add one line of code)
  139.                         zigzag(node);
  140.                     }
  141.                 }
  142.                 // Do something (just add one line of code)
  143.                 splay(node);
  144.             }
  145.         }
  146.     }
  147.  
  148.     // Use this function to call zig
  149.     public void zigzig(Node node) { // Move two nodes up along the Outer path
  150.         zig(node.parent);
  151.         zig(node);
  152.  
  153.     }
  154.  
  155.     // Use this funciton to call zig
  156.     public void zigzag(Node node) { // Move two nodes up along the Inner path
  157.         zig(node);
  158.         zig(node);
  159.     }
  160.  
  161.     // This function should be called by zigzig or zigzag
  162.     public void zig(Node node) {// Move up one step
  163.         if (node.parent == null) {
  164.             System.out.println("Cannot perform Zig operation on the root node");
  165.         } else if (node.parent == root) { // If the node is a child of the root
  166.             if (node.key < node.parent.key) {// Zig from left
  167.                 node.parent.left = node.right;
  168.                 node.right = node.parent;
  169.                 node.right.parent = node;
  170.                 root = node;
  171.             } else { // Zig from right
  172.                 node.parent.right = node.left;
  173.                 node.left = node.parent;
  174.                 node.left.parent = node;
  175.                 root = node;
  176. //                root.parent = null;
  177.             }
  178.         } else {// if the node is not a child of the root
  179.             if (node.key < node.parent.key) {// Zig from left
  180.                 node.parent.left = node.right;
  181.                 node.right = node.parent;
  182.                 if (node.parent.parent.left == node.parent) {
  183.                     node.parent.parent.left = node;
  184.                 } else {
  185.                     node.parent.parent.right = node;
  186.                 }
  187.                 node.parent = node.parent.parent;
  188.                 node.right.parent = node;
  189.             } else {
  190.                 node.parent.right = node.left;
  191.                 node.left = node.parent;
  192.                 if (node.parent.parent.left == node.parent) {
  193.                     node.parent.parent.left = node;
  194.                 } else {
  195.                     node.parent.parent.right = node;
  196.                 }
  197.                 node.parent = node.parent.parent;
  198.                 node.left.parent = node;
  199.             }
  200.         }
  201.         if(node.left != null)
  202.         {
  203.             if(node.left.right != null) node.left.right.parent = node.left;
  204.             if(node.left.left != null) node.left.left.parent = node.left;
  205.         }
  206.         if(node.right != null)
  207.         {
  208.             if(node.right.right != null) node.right.right.parent = node.right;
  209.             if(node.right.left != null) node.right.left.parent = node.right;
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement