# lec5 - Lecture 5 Binary search trees Binary search tree...

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

Page 1 of 42 CSE 100, UCSD: LEC 5 Lecture 5 Binary search trees Binary search tree average cost analysis The importance of being balanced AVL trees and AVL rotations Insert in AVL trees Reading: Weiss Ch 4, sections 1-4

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

View Full Document
Page 2 of 42 CSE 100, UCSD: LEC 5 Binary search tree invariants Structural property: a BST is a binary tree Ordering property: Each data item in a BST has a key associated with it Keys in a BST must be comparable to each other, which means that... ... for any two keys , exactly one of these is true: is greater than ; is greater than ; and are equal 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) In a BST, the key in a node “splits” the keys in its left and right subtrees... this permits efficient Insert and Find operations, at least in the average case k 1 k 2 k 1 k 2 k 2 k 1 k 1 k 2
Page 3 of 42 CSE 100, UCSD: LEC 5 Binary search trees Which of these are BSTs, and which are not? 5 2 0 8 7 9 6 0 2 5 6 7 8 9 6 5 0 8 9 2 7 6 2 0 8 9 5 7

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

View Full Document
Page 4 of 42 CSE 100, UCSD: LEC 5 Implementing binary search trees Nodes in a binary search tree should be designed to hold a pointer to the left child (null if no left child) a pointer to the right child (null if no right child) a way to access the key for the data item associated with the node Simple Java declaration of a BSTNode class for generic keys: class BSTNode<T> { T key; BSTNode<T> left; BSTNode<T> right; } left right key left child right child
Page 5 of 42 CSE 100, UCSD: LEC 5 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 to look in next Pseudocode for the basic iterative algorithm to Find key with value k in a BST: 1. If tree is empty (no root), return FALSE. 2. Set CurrNode = RootNode. 3. If k == CurrNode.key, return TRUE. 4. If k < CurrNode.key ... /* key must be in left subtree, if it is in the tree at all. */ If CurrNode.left == NULL, return FALSE. else set CurrNode = CurrNode.left, and go to 3. 5. else ... /* key must be in right subtree, if it is in the tree at all. */ If CurrNode.right == NULL, return FALSE. else set CurrNode = CurrNode.right, and go to 3.

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

View Full Document
Page 6 of 42 CSE 100, UCSD: LEC 5 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. It is important to maintain the BST invariants: If you start with a BST, the result after insertion must still be a BST !
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern