Chapter 12 Binary Search Trees - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Chapter 12 Binary Search Trees

Description:

Binary search tree (BST) Binary tree ... Can print out all the keys in a BST in sorted order ... Randomly Built BST on n distinct keys ... – PowerPoint PPT presentation

Number of Views:345
Avg rating:3.0/5.0
Slides: 23
Provided by: ccNct
Category:
Tags: binary | bst | chapter | search | trees

less

Transcript and Presenter's Notes

Title: Chapter 12 Binary Search Trees


1
Chapter 12 Binary Search Trees
2
Introduction
  • Search trees support SEARCH, MINIMUM, MAXIMUM,
    PREDECESSOR, SUCCESSOR, INSERT, AND DELETE
  • Dictionary and priority queue
  • Basic operations take time proportional to the
    height of the tree
  • Complete binary tree ?(lg n)
  • Linear-chain tree ?(n)
  • Expected height of a randomly built binary search
    tree O(lg n)

3
What Is A Binary Search Tree?
4
Definition
  • Binary search tree (BST)
  • Binary tree
  • Represented by a linked data structure ? each
    node is an object
  • Key satellite data
  • Pointers left ? left child, right ? right child,
    p ? parent
  • 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
    keyy ? keyx

5
BST Example
6
Tree Traversal
  • Inorder tree walk
  • The key of the root of a subtree is printed
    between the values in its left subtree and those
    in its right subtree
  • Can print out all the keys in a BST in sorted
    order
  • (a) 2, 3, 5, 5, 7, 8 (b) 2, 3, 5, 5, 7, 8
  • Preorder tree walk
  • The key of the root of a subtree is printed
    before the values in its left subtree and those
    in its right subtree
  • (a) 5, 3, 2, 5, 7, 8 (b) 2, 3, 7, 5, 5, 8
  • Postorder tree walk
  • The key of the root of a subtree is printed after
    the values in its left subtree and those in its
    right subtree
  • (a) 2, 5, 3, 8, 7, 5 (b) 5, 5, 8, 7, 3, 2

7
InOrder-Tree-Walk
8
Computation Time of INORDER-TREE-WALK(x)
  • Theorem 12.1. If x is the root of an n-node
    subtree, then the call INORDER-TREE-WALK(x) takes
    ?(n) time
  • Use the substitution method
  • T(n) T(k) T(n-k-1) d T(0)c
  • Show that T(n) ?(n) by proving that T(n)
    (cd)nc
  • For n0 ? (cd) 0 c c
  • For n gt 0
  • T(n) T(k) T(n-k-1) d ((cd)kc)((cd)(n
    -k-1)c) d (cd)nc-(cd)cd (cd)n c

9
Query A Binary Search Tree
10
Searching
11
Searching Example
12
Minimum and Maximum
  • The element with the minimum/maximum key can be
    found by following left/right child pointers from
    the root until NIL is encountered

13
Successor And Predecessor
  • Assume distinct keys
  • The successor of a node x is the node with the
    smallest key greater than keyx
  • Right subtree nonempty ? successor is the
    leftmost node in the right subtree (Line 2)
  • Otherwise, the successor is the lowest ancestor
    of x whose left child is also an ancestor of x ?
    go up the tree until we encounter a node that is
    the left child of its parent (Lines 3-7)

14
TREE-SUCCESSOR(X)
15
Insertion and Deletion
16
Insertion
Should be inserted in the left subtree
Should be inserted in the right subtree
17
Insertion Example
12
12
Insert an item with key 13into a BST
18
5
18
19
15
9
2
15
17
13
18
Deletion
  • Consider three cases for the node z to be deleted
  • z has no children modify pz to replace z with
    NIL as its child
  • z has only one child splice out z by making a
    new link between its child and its parent
  • z has two children splice zs successor y, which
    has no left child and replace zs key and
    satellite data with ys key and satellite

19
TREE-DELETE Algorithm
  • TREE-DELETE Algorithm
  • Lines 1-3 determines a node y to splice out (z
    itself or zs successor)
  • Lines 4-6 x is set to the non-NIL child of y, or
    to NIL if y has no children
  • Lines 7-13 splice out y by modifying pointers in
    py and x
  • Special care for when xNIL or when y is the root
  • Lines 14-16 if the successor of z was the node
    spliced out, ys key and satellite data are moved
    to z, overwriting the previous key and satellite
    data
  • Line 17 return node y for recycle it via the
    free list

20
(No Transcript)
21
(No Transcript)
22
Randomly Built BST
  • All the basic operations on a BST run in O(h)
    time
  • If the elements are inserted in strictly
    increasing order height n-1
  • Exercise B.5-4 h ? ?lg n?
  • The average case is much closer to the best case
  • Complicated if both insertion and deletion are
    used to create a BST
  • Randomly Built BST on n distinct keys
  • A BST arises from inserting the keys in random
    order into an initially empty tree, where each of
    the n! permutations of the input keys is equally
    likely
  • Expected height of a randomly built BST on n
    distinct keys is O(lg n)
  • Section 12.4 (Self-Study)
Write a Comment
User Comments (0)
About PowerShow.com