1 / 4

Review: Search

Review: Search. Linear Search Binary Search Search demos: http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/appldsal.html. 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.

erunge
Download Presentation

Review: Search

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review: Search • Linear Search • Binary Search • Search demos: • http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/appldsal.html

  2. 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.html

  3. 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 removed

  4. 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; }

More Related