# Lecture18 - s If the key is greater than the value try the right subtree s If the key is the same as the value obviously the search stops Searching

This preview shows pages 1–10. Sign up to view the full content.

CSCI-255 Advanced Data Structures Lecture 18

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

View Full Document
Binary Search Trees (BST) b A binary tree with the following property: For each node of the tree, all values stored in its left subtree (the tree whose root is the left child) are less than the value stored in the node, and all values stored in the right subtree are greater than the value stored in the node
Implementing Binary Trees b Can be implemented in at least 2 ways s As linked structures (IN CLASS) s As arrays

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

View Full Document
Searching a BST b For every node, compare the key to be located with the value stored in the node currently pointed at s If the key is less than the value, go to the left subtree and try again

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

View Full Document

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

View Full Document

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

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

Unformatted text preview: s If the key is greater than the value, try the right subtree s If the key is the same as the value, obviously the search stops Searching a BST (cont’d) b Example: Search for the key 15 Recursive version of search? (DONE IN CLASS) Inserting an Element into a BST b Search for the position in the tree where the element would be found b Insert the element in the position s Note: A newly inserted node is a leaf Inserting an Element into a BST (cont’d) Inserting an Element into a BST (cont’d) Exercise: Write a recursive version of insert (DONE IN CLASS) Do 6.22(e) (DONE IN CLASS) Running Time?...
View Full Document

## This note was uploaded on 11/26/2009 for the course MATH AND C CSCI255 taught by Professor Dr.ikergondraluja during the Spring '09 term at St. Francis Xavier, Antigonish.

### Page1 / 10

Lecture18 - s If the key is greater than the value try the right subtree s If the key is the same as the value obviously the search stops Searching

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

View Full Document
Ask a homework question - tutors are online