Notes on Skew Heap Data Structure

CS 510: Computer Algorithms

Skew Heaps


Example of Merging 2 Skew Heaps

Suppose we are merging a heap containing the elements 2, 5, and 7 with a heap containing the elements 4 and 6.

		7	<- merge ->	6
	       / \		       / 
	      5   2		      4
 Step One:
   Identify the root, thus 7 becomes the new root and the left
   subtree of the heap with root 7 becomes the right subtree of 
   the other heap:
		7	2 <- merge -> 6   (to form the left subtree
		 \   		     /     of the new skew heap)
		  5  		    4

   Step Two:  
			       7
			      / \
			     6   5
			      \
			       4	<- merge -> 2

   Step Three:  
			       7
			      / \
			     6   5
			    / \
			   2   4	 	

Conclusion:

At each step, we take we find the largest element of the two heaps and place that into the skew heap as the left child of the new skew heap. We then take the left subtree of this element and make it the right subtree of that element in the new tree. Then we remove the element and its left subtree from the original 2 trees and repeat.


Inserting an Element into a Skew Heap:

 Step One:
			9	<- merge ->  2
		       / \
		      8   7
			 / \
			6   5
			   / \
			  4   3

	Result:   
			9
			 \
			  8
	Originals:
			7	<- merge ->  2
		       / \
		      6   5
			 / \
			4   3

 Step Two:
	Result:   
			9
		       / \
	              7	  8
		       \
			6
	Originals:
			5	<- merge ->  2
		       / \
		      4   3

 Step Three:
	Result:   
			9
		       / \
	              7	  8
		     / \
	            5	6
		     \
		      4
	Originals:
			3	<- merge ->  2

 Step Four:
	Result:   
			9
		       / \
	              7	  8
		     / \
	            5	6
		   / \
		  3   4
		 /
		2
Now we can insert 1:
	Beginning:   
			9      <- merge -> 1
		       / \
	              7	  8
		     / \
	            5	6
		   / \
		  3   4
		 /
		2
	First make the 7 subtree the right child of the root:   
			9      <- merge -> 1
		       / \
	              8	  7
		         / \
	             	5   6
		       / \
		      3   4
		     /
	 	    2
	Next insert 1:   
			9    
		       / \
	              8	  7
		     /   / \
	            1	5   6
		       / \
		      3   4
		     /
	 	    2

Conclusion:

A badly unbalanced tree can affect the performance of one operation, but it can be shown that subsequent insertions and deletions must as a consequence be very rapid. In fact, amortized over time, each operation is no worse than O(log n).



CS 510 Home

News

Homework

Class Projects

Class Notes

Syllabus

Bibliography