How to Use a Skew Heap in Java

A skew heap is an abstract data structure. Though Java doesn't provide for a binary tree class, the skew heap can be thought of as a self-organizing binary search tree. The Java Skew Heap class implements the Comparable interface so lists of SkewHeap objects can be sorted easily.

Instructions

    • 1

      Write the skeleton of the SkewHeap class. The variables of interest are value (the node's value) and left and right (the left and right children). The tmp and indent static variables are used for temporary space in the merge and print methods. The constructor initializes value and leaves left and right as null.""public class SkewHeap implements Comparable {
      int value;
      SkewHeap left, right;
      static LinkedList tmp;
      static int indent = 0; public SkewHeap(int val) {
      value = val;
      }
      }""

    • 2

      Use the compareTo method as a way to fulfill the Comparable interface and allow lists of SkewHeap objects to be sorted. The compareTo method should return a negative number, zero or positive number, depending on how the two objects should be sorted. Achieve this by performing a subtraction on the two nodes' values such that nodes with lesser values are sorted before nodes of greater value.""public int compareTo(SkewHeap h) {
      return value--h.value;
      }""

    • 3

      Compose the chop method, an important method used by merge. When a merge is performed, both heaps are chopped apart down the right side. The chop method performs that chop and adds the remaining subheaps to the tmp list.""public void chop() {
      SkewHeap r = right;
      right = null;

      if( r != null ) r.chop();
      tmp.addLast(this);
      }""

    • 4

      Create the merge method. The insert and removeHead methods both use merge to accomplish their task. The merge method will chop both heaps to be merged, which stores all of the subheaps in tmp.

    • 5

      Accomplish sorting the tmp linked list and combining the subheaps by removing the last two heaps from the list. Add one as the right child of the other, swap the right and left children and add the heap back to the end of the list. In this way, chopped subheaps are reassembled into a single balanced heap. Left nodes are always guaranteed to be less than the right nodes, and child nodes have a greater value than parent nodes.""public SkewHeap merge(SkewHeap h) {
      // Chop the nodes down the right path
      tmp = new LinkedList();
      chop();
      h.chop();

      // Sort the nodes
      Collections.sort(tmp);

      // Merge the subheaps
      while(tmp.size() > 1) {
      SkewHeap a = tmp.removeLast();
      SkewHeap b = tmp.removeLast();

      b.right = b.left;
      b.left = a;

      tmp.addLast(b);
      }

      return tmp.getFirst();
      }""

    • 6

      Write the removeHead method. This will remove the head node and merge the left and right child heaps.""public SkewHeap removeHead() {
      if( left == null && right == null ) return null;
      else if( left == null ) return right;
      else if( right == null ) return left;
      else return left.merge(right);
      }""

    • 7

      Formulate the print method. This method is important for debugging, as debuggers don't often have the facilities to view nested data structures like this skew heap. It's recursive and indents correctly.""public void print() {
      for( int i = 0; i < indent; ++i ) System.out.print(" ");

      System.out.println(value);
      indent++;
      if( left != null ) {
      for( int i = 0; i < indent; ++i ) System.out.print(" ");
      System.out.println("<-");
      left.print();
      }
      if( right != null ) {
      for( int i = 0; i < indent; ++i ) System.out.print(" ");
      System.out.println("->");
      right.print();
      }
      indent--;
      }""

Tips & Warnings

  • The insert method is a small wrapper around merge to create a single node and merge it with the heap.

Related Searches:

Comments

You May Also Like

  • How to Set Java Heap Space

    The Java Virtual Machine (JVM) is the execution component of the Java Runtime Environment (JRE) that interprets and executes the byte code...

  • How to Change a Java Heap Size in a WebLogic Console

    Oracle WebLogic is a Java Enterprise Edition (EE) software development platform that includes the Java EE application server, WebLogic Application Server and...

  • How to Change Java Heap Size in Windows XP

    Java applications are allocated memory, called "heap" memory, to store data dynamically created during the execution of a program. Java applications launch...

  • How to Use Excel's SKEW Function

    Microsoft Excel uses functions contained within the application to assist user with complex mathematical and financial calculations. The functions provide a ...

  • How to Use PrintStream in Java

    The Java printstream class works with files to retrieve information and output the results to the user. The unique aspect about printstream...

  • How to Trade Option Skew

    Trading the option's skew is a profitable way for traders to take advantage of different implied volatility levels across time and for...

  • How to Cut Crown Molding Properly

    Using the correct tools and methods, you can save thousands of dollars by installing your own crown molding. But cutting crown molding...

  • How to Do Postorder Traversal in a Binary Tree in Java

    Though Java doesn't provide a binary tree class in the default libraries, a basic binary tree class is simple enough to be...

  • How to Change Java Heap Space

    The Java system takes a lot of worries about memory management off the developers, but it still has to have some memory...

  • How to Sort a Linked List in Java

    A linked list is one of the primary types of data structures in the programming world. It's an arrangement of nodes which...

  • How to Explain Skewed Morality

    Morality itself is difficult to pinpoint exactly and philosophers, psychologists and thinkers all around the world grapple with the ideal definition. The...

  • How to Build a Compost Heap

    Learning to build a compost heap will not only serve to recycle food and garden waste, but also provide a reliable resource...

  • How to Create a Java File

    Creating and writing data to a file in Java can be a slightly counter-intuitive task since the creation of the built-in Java...

  • What Is Skew?

    In mathematics, the term "skew" can refer either to statistical skew (also called "skewness") or to geometrical skew. In statistics, skew is...

  • How to Use Assignment Operators in Java

    Type your Java variable so that the left-hand variable is the same type as the right-hand expression. Otherwise, there must be an...

  • How to Do Inorder Traversal in Binary Tree in Java

    Java doesn't have a binary tree class, though it's simple to present a version of the data structure to do an inorder...

  • Binary Tree Traversal Methods

    Binary trees (BTs) are data structures used by computer programmers whose software must represent medium to large data sets in an organized,...

  • The Disadvantages of Process Mapping

    A process map outlines the job duties performed to complete a specific task at the workplace. Maps can be used to outline...

  • Java Binary Tree Tutorial

    Every node in a binary has at most two child nodes. Usually, each node is simply called the left and right node....

Related Ads

Featured