SplayTree - package DataStructures; / / / / / / / / / / / /...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
package DataStructures; // SplayTree class // // CONSTRUCTION: with no initializer // // ******************PUBLIC OPERATIONS********************* // void insert( x ) --> Insert x // void remove( x ) --> Remove x // Comparable find( x ) --> Return item that matches x // Comparable findMin( ) --> Return smallest item // Comparable findMax( ) --> Return largest item // boolean isEmpty( ) --> Return true if empty; else false // void makeEmpty( ) --> Remove all items // void printTree( ) --> Print tree in sorted order /** * Implements a top-down splay tree. * Note that all "matching" is based on the compareTo method. * @author Mark Allen Weiss */ public class SplayTree { /** * Construct the tree. */ public SplayTree( ) { root = nullNode; } /** * Insert into the tree. * @param x the item to insert. */ public void insert( Comparable x ) { if( newNode == null ) newNode = new BinaryNode( null ); newNode.element = x; if( root == nullNode ) { newNode.left = newNode.right = nullNode; root = newNode; } else { root = splay( x, root ); if( x.compareTo( root.element ) < 0 ) { newNode.left = root.left; newNode.right = root; root.left = nullNode; root = newNode; } else if( x.compareTo( root.element ) > 0 ) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
newNode.right = root.right; newNode.left = root; root.right = nullNode; root = newNode; } else return; } newNode = null; // So next insert will call new } /** * Remove from the tree. * @param x the item to remove. */ public void remove( Comparable x ) { BinaryNode newTree; // If x is found, it will be at the root root = splay( x, root ); if( root.element.compareTo( x ) != 0 )
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/22/2011 for the course CSC 532 taught by Professor Dr.gursaranadhar during the Spring '07 term at University of North Carolina Wilmington.

Page1 / 6

SplayTree - package DataStructures; / / / / / / / / / / / /...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online