The outcome of any serious research can only be to make two questions grow where only one grew befor - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

The outcome of any serious research can only be to make two questions grow where only one grew befor

Description:

BST Queries ... T(n) = O(h) where h is the height of the BST ... BST structure allows successor to be found without comparing keys ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 13
Provided by: davidedwa
Category:

less

Transcript and Presenter's Notes

Title: The outcome of any serious research can only be to make two questions grow where only one grew befor


1
  • The outcome of any serious research can only be
    to make two questions grow where only one grew
    before.
  • Thorstein Veblen (1857 - 1929)

2
CSE 502NFundamentals of Computer Science
  • Fall 2004
  • Lecture 10
  • Binary Search Trees (CLRS 12)
  • Introduction to Red-Black Trees (CLRS 13)

3
Binary Search Trees
  • A binary search tree is a data structure that
    supports the dynamic set operations Insert,
    Search, Delete, Minimum, Maximum, Predecessor,
    and Successor
  • Used both as a dictionary and a priority queue
  • Binary-search-tree property let x be a node in a
    BST if y is a node in the left subtree of x,
    then keyy keyx if y is a node in the right
    subtree of x, then keyx keyy

4
Tree walking
  • Inorder tree walk is a recursive algorithm that
    prints the keys stored in the BST in sorted order
  • Prints the root of a subtree after visiting the
    left subtree and prior to visiting the right
    subtree
  • T(n) Q(n)
  • Preorder tree walk prints the subtree root prior
    to visiting the left and right subtrees
  • Postorder tree walk prints the subtree root after
    visiting the left and right subtrees

5
BST Queries
  • Given a pointer to the root node and a key k, a
    BST query returns a pointer to the node
    containing the key k or NIL if the BST does not
    contain the key
  • Tree-Search is a recursive query routine
  • T(n) O(h) where h is the height of the BST
  • Iterative-Tree-Search unrolls the recursion
    using a while loop
  • More efficient on most computers why?
  • T(n) O(h) where h is the height of the BST

6
Minimum Maximum
  • The leftmost node in the tree is the minimum
  • The rightmost node in the tree is the maximum
  • Both properties guaranteed by the binary search
    tree property
  • Both run in O(h) where h is the height of the BST

7
Successor Predecessor
  • Successor of x is the next node in the sorted
    order
  • Returns next node printed in an Inorder Tree Walk
  • BST structure allows successor to be found
    without comparing keys
  • T(n) O(h) where h is the height of the BST
  • Predecessor is a symmetric operation

8
Insertion
  • Tree-Insert must maintain binary search tree
    property
  • T(n) O(h) where h is the height of the BST

9
Deletion
  • Tree-Delete must maintain binary search tree
    property
  • Considers three cases
  • z has no children
  • Modify parents child pointer
  • z has one child
  • Modify childs parent pointer
  • Modify parents child pointer
  • z has two children
  • Replace z with its successor
  • Modify pointers
  • T(n) O(h) where h is the height of the BST

10
Running Time
  • Search, Minimum, Maximum, Successor, Predecessor,
    Insert, and Delete run in O(h) time where h is
    the height of the BST
  • Worst-case can be Q(n) for linear chain of nodes
  • Tree height is primary determinant of running
    time
  • What determines tree height?
  • Can we make any claims about the expected height
    of a tree containing n keys?
  • Randomly built binary search trees have expected
    height O(lg n)
  • Can we bound the worst-case tree height to O(lg
    n)?

11
Red-Black Trees
  • Red-black trees are one of many balanced search
    tree data structures
  • For n nodes a red-black tree has height at most
    2(lg n 1) O(lg n)
  • Assigns each node a color (red or black)
  • Red-black properties
  • Every node is either red or black
  • The root node is black
  • Every leaf (NIL) is black
  • If a node is red, then both its children are
    black
  • For each node, all paths from the node to
    descendant leaves contain the same number of
    black nodes
  • Insert and delete operations must be modified in
    order to maintain red-black properties (maximum
    height, balance condition)
  • Use rotations to modify structure of tree while
    maintaining binary search tree property

12
Rotations
Left-Rotate(T,x)
x
y
g
y
a
x
Right-Rotate(T,y)
b
g
a
b
Write a Comment
User Comments (0)
About PowerShow.com