BinarySearchTree-102-sp10

BinarySearchTree-102-sp10 - removed remove private

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

View Full Document Right Arrow Icon
Review: Search Linear Search Binary Search Search demos: http://www.cosc.canterbury.ac.nz/people/mukun
Background image of page 1

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

View Full DocumentRight Arrow Icon
Binary Search Tree A binary tree where every node's left subtree has values less than the node's value, and every right subtree has values greater. A new node is added as a leaf. Check out this applet: http://www.cs.jhu.edu/~goodrich/dsa/trees/btree
Background image of page 2
Remove node algorithm Find element If it is a leaf just delete it If it has one child adjust the parent to point to the child (thus bypassing the node) If it has two children Replace data with the smallest data of the right subtree Recursively delete the node which now needs to be
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: removed remove private BinaryNode<AnyType> remove( AnyType x, BinaryNode<AnyType> t ) { /* 1*/ if( t == null ) return t; // Item not found; do nothing int compareResult = x.compareTo( t.element ); /* 2*/ if(compareResult < 0 ) t.left = remove( x, t.left ); /* 3*/ else if(compareResult > 0 ) t.right = remove( x, t.right ); /* 4*/ else if( t.left != null && t.right != null ) // Two children { t.element = findMin( t.right ).element; t.right = remove( t.element, t.right ); } /*567*/ else t = ( t.left != null ) ? t.left : t.right; return t; }...
View Full Document

This note was uploaded on 05/06/2010 for the course COMPUTER S 101 taught by Professor Sanaodeh during the Spring '08 term at NYU.

Page1 / 4

BinarySearchTree-102-sp10 - removed remove private

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

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