RedBlackTree - package DataStructures; / / / / / / / / / /...

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

View Full Document Right Arrow Icon
package DataStructures; // RedBlackTree class // // CONSTRUCTION: with a negative infinity sentinel // // ******************PUBLIC OPERATIONS********************* // void insert( x ) --> Insert x // void remove( x ) --> Remove x (unimplemented) // 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 red-black tree. * Note that all "matching" is based on the compareTo method. * @author Mark Allen Weiss */ public class RedBlackTree { /** * Construct the tree. * @param negInf a value less than or equal to all others. */ public RedBlackTree( Comparable negInf ) { header = new RedBlackNode( negInf ); header.left = header.right = nullNode; } /** * Insert into the tree. Does nothing if item already present. * @param item the item to insert. */ public void insert( Comparable item ) { current = parent = grand = header; nullNode.element = item; while( current.element.compareTo( item ) != 0 ) { great = grand; grand = parent; parent = current; current = item.compareTo( current.element ) < 0 ? current.left : current.right; // Check if two red children; fix if so handleReorient( item ); } // Insertion fails if already present if( current != nullNode ) return; current = new RedBlackNode( item, nullNode, nullNode ); // Attach to parent
Background image of page 1

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

View Full DocumentRight Arrow Icon
if( item.compareTo( parent.element ) < 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 / 5

RedBlackTree - 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