lec3 - Lecture 3 Binary search trees Toward a binary search...

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

View Full Document Right Arrow Icon
Page 1 of 22 CSE 100, UCSD: LEC 3 Lecture 3 Binary search trees Toward a binary search tree implementation using C++ templates C++ iterators and the binary search tree successor function Reading: Weiss Ch 4, sections 1-4
Background image of page 1

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

View Full DocumentRight Arrow Icon
Page 2 of 22 CSE 100, UCSD: LEC 3 Binary search trees A binary search tree is a data structure with these invariants: Structural property: a BST is a binary tree Ordering property: Each data item in a BST has a data item , sometimes called a key , associated with it Keys in a BST belong to an ordered set, which means that. .. ... for any two keys , exactly one of these is true: is less than ; is less than ; and are equal Then, for every node X in a BST: the key in X is greater than every key in X’s left subtree the key in X is less than every key in X’s right subtree (so, a BST does not hold duplicate keys) k 1 k 2 k 1 k 2 k 2 k 1 k 1 k 2
Background image of page 2
Page 3 of 22 CSE 100, UCSD: LEC 3 Binary search tree examples Which of these are BSTs, and which are not? 5 2 0 8 79 6 0 2 5 6 7 8 9 6 5 0 8 9 2 7 6 2 0 8 9 5 7
Background image of page 3

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

View Full DocumentRight Arrow Icon
Page 4 of 22 CSE 100, UCSD: LEC 3 Implementing binary search trees In an implementation of a BST, nodes should be designed to hold: a pointer to the left child of the node (null if no left child) a pointer to the right child of the node (null if no right child) a pointer to, or copy of, the data item associated with the node For some algorithms, it is convenient if the nodes also have: a pointer to the parent (null if this node is the root) a field to hold additional implementation-specific information about the node (balance number, color, priority. ..)
Background image of page 4
Page 5 of 22 CSE 100, UCSD: LEC 3 A Node class template for BST nodes Simple C++ Node class template for BST nodes and generic keys: template <typename T> class Node { public: T key; Node<T>* left; Node<T>* right; Node<T>* parent; int info; }; left right key left child right child root info
Background image of page 5

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

View Full DocumentRight Arrow Icon
Page 6 of 22 CSE 100, UCSD: LEC 3 The basic Find operation in a binary search tree Idea: exploit the ordering property of BST’s; each key comparison either finds the key you are looking for, or tells you which subtree (left or right) to look in next Note: it is a C++ STL convention to use only < for key comparisons Pseudocode for the basic iterative algorithm to Find key with value k in a BST: 1. If RootNode == NULL , tree is empty (no root). Return false . 2. Set CurrNode = RootNode . 3. If k < CurrNode.key ... /* key must be in left subtree, if it is in the tree. */ If CurrNode.left == NULL , return false . else set CurrNode = CurrNode.left , and go to 3. 4. else If CurrNode.key < k ... /* key must be in right subtree, if it is in the tree . */ If CurrNode.right == NULL , return false . else set CurrNode = CurrNode.right , and go to 3. 5. else Found the key; it is in CurrNode . Return true .
Background image of page 6
Page 7 of 22 CSE 100, UCSD: LEC 3 The Insert operation in a binary search tree Again, the idea is to make use of the ordering property of BST’s; each key comparison tells you which subtree the key must go in, so the find algorithm can find it later But (unlike finds) inserts modify the tree.
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/02/2011 for the course CSE 100 taught by Professor Staff during the Fall '08 term at UCSD.

Page1 / 22

lec3 - Lecture 3 Binary search trees Toward a binary search...

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

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