E a word to search for start at the root node is the

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2010 22 11 10/14/10 Binary Search Trees < D D < D The order constraint: •  For each node n B A if node nl is in the left subtree of n then nl < n G C E H F if node nr is in the right subtree of n then n < nr note that a Binary Search Tree is a well-formed Binary Tree CPSC 223  ­ ­ Fall 2010 23 Pointer-Based Binary Search Trees Nodes consist of values plus left and right child pointers root Entry item lebChild rightChild Entry item Entry item lebChild rightChild lebChild rightChild CPSC 223  ­ ­ Fall 2010 24 12 10/14/10 Searching for nodes in a BST Given a key (i.e., a word) to search for … •  Start at the root node –  Is the item at the root node equal to search key? If Yes … Found match! –  Is the item at the root node greater than the search key? If Yes … Search the left subtree of the root –  Is the item at the root node less than the search key? If Yes … Search the right subtree of the root CPSC 223  ­ ­ Fall 2010 25 Inserting Nodes Basic idea •  Find where the node should be (search)...
View Full Document

Ask a homework question - tutors are online