lec22 - CSE 12 Binary Search Trees Remove in a BST BST best...

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

View Full Document Right Arrow Icon
21 Remove in a BST BST best and worst case time costs Balanced BST’s AVL trees and AVL rotations CSE 12 Binary Search Trees
Background image of page 1

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

View Full DocumentRight Arrow Icon
Implementing remove() in a BST Removing a node is a little trickier than add() or contains() Need to keep the tree connected, and Need to maintain the BST invariants One way to do it is to consider 3 separate cases: Case 1 The node to remove is a leaf Case 2 The node to remove is an internal node with one child Case 3 The node to remove is an internal node with two children
Background image of page 2
remove(): Case 1 removing a leaf Removing a leaf node just requires updating its parent’s child link to be null: “snip off” the leaf
Background image of page 3

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

View Full DocumentRight Arrow Icon
remove(): Case 2 target has 1 Child Removing a node with one– move the single child into its parent’s position
Background image of page 4
remove(): Case 3 target has 2 Children Removing a node with two children from a binary search tree – replace the value in the target node with the value in its successor in the tree, then delete that successor 15’s successor in the tree is 20 The “successor” of a node in a BST cannot have 2 children (why?) so either Case 1 or Case 2 apply to it
Background image of page 5

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

View Full DocumentRight Arrow Icon
The Need for Balance A Binary Search Tree can be badly “unbalanced”, giving a time cost in the worst case Same elements in each tree, but very different search times (in the worst case) Good tree! Bad tree! ( ) n
Background image of page 6
The Need for a Balanced Search Tree A badly unbalanced BST is no better than a linked list! A complete binary tree, for example a heap, is much better But in a BST, maintaining completeness as a structural invariant can have high time cost: An add() or remove() of one node can take n steps to restore an n -node binary search tree as a complete binary tree When the costs outweigh the benefits, we must consider alternatives. ..
Background image of page 7

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

View Full DocumentRight Arrow Icon
15-8/43 AVL Trees For every node X, the height of X’s left and right subtrees differ by no more than 1 An AVL tree is a BST with the following additional structural balance property : It turns out that this additional invariant implies that the height of an AVL tree with n nodes is about 1.4 log 2 n in the
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

Page1 / 26

lec22 - CSE 12 Binary Search Trees Remove in a BST BST best...

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

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