Lecture 17 Search Trees I - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Lecture 17 Search Trees I

Description:

BST Insertion. A new node is always inserted at the leaves of the tree. ... BST insertion. if you insert the keys 22,33,44,55,66,77,88,99 , what will happen? 13 ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 32
Provided by: wwwcore
Category:
Tags: bst | lecture | search | trees

less

Transcript and Presenter's Notes

Title: Lecture 17 Search Trees I


1
Lecture 17 Search Trees I
  • April 22, 2008
  • Prof. Kyu Ho Park


2
Keys and Comparable Types
  • ?Comparable Interface
  • public int compareTo(Object object)
  • ? It returns an integer c that is assumed to
    signal one of three possibilities
  • If clt0, then this object is less than the
    given object
  • If c0, then this object is equal to the
    given object
  • If cgt0, then this object is greater than
    the given object.

3
Binary Search Trees
  • As an implementation of a dictionary for example
    we study several data structures.
  • When ordered access is needed, search trees are
    the best data structures to use.

4
compareTo( )
  • ? Example
  • String s1 ACE, s2 ADB
  • if (s1.compareTo(s2) lt 0) System.out.println(
    s1lts2)
  • if (s2.compareTo(s1) gt 0) System.out.println(
    s2gts1)

5
Node Class
  • ? class Node
  • int iData// data used as key value
  • double dataToStore
  • node leftChild
  • node rightChild

6
Inorder Traversing
  • private void inOrder(node localRoot)
  • if( localRoot ! null)
  • inOrder( localRoot.leftChild)
  • System.out.print(localRoot.iData )
  • inOrder(localRoot.rightChild)

Traversing Visit all the nodes in some specified
order.
7
Expression Tree and Infix, Prefixand Postfix
representation
  • Infix A (B C)
  • Prefix ABC
  • Postfix ABC


3 representations
A

B
C
Expression tree
8
Binary Search Trees(BST)
  • ? BST
  • It is a binary tree that contains a key-address
    pair in each node which satisfies this BST
    property
  • For each key in the tree, all the keys in its
    left subtree are less than it, and all the keys
    in its right subtree are greater than it.

lt
gt
9
BST
  • ? BST traversal
  • An inorder traversal of a BST visits the keys in
    increasing order

10
BST Retrieve
  • ? Input a BST T and a key.
  • Output the data address for that key , or null
    if that key is not in T.
  • 1. If T is empty, return null.
  • 2. If key lt root.key, return the value returned
    by recursively searching left for key.
  • 3. If key gt root.key return the value returned
    by recursively searching right for key.
  • 4. Return root.address

11
BST Insertion
  • ?A new node is always inserted at the leaves of
    the tree.
  • Input a BST T and a key-address pair.
  • Output false if key is already in T otherwise
    true.
  • PostCondition the key-address pair is in T
  • 1. If T is empty, make it a singleton
    containing the key-address pair, and return
    true.
  • 2. If key lt root.key, return the value returned
    by recursively inserting the key-address pair
    in the left subtree.
  • 3. If key gt root.key, return the value returned
    by recursively inserting the key-address pair
    in the right subtree.
  • 4. Return false.

12
BST insertion
  • if you insert the keys 22,33,44,55,66,77,88,99 ,
    what will happen?

13
Building a BST
? BSTs are built by repeated calls to the insert
algorithm.
14
The Best-case and Worst-case BST
15
BST Delete
16
BST Delete

17
BST Deletion
  • ? How to find the inorder successor y of a BST
    node x. There are three possibilities
  • 1. If x has a right subtree, then y is the
    left-most node in that subtree.
  • 2. Otherwise, if x has a right ancestor, then y
    is its closest right ancestor.
  • 3. Otherwise, x is the right-most node in tree
    and therefore has no inorder successor.

18
Inorder successor of a node
19
BST Minimum
? BST minimum algorithm Input a nonempty BST
T. Output the minimum node in T. 1.Let x be
the root of T. 2.While x.left is not empty, set
x x.left. 3.Return x.
20
BST Successor
  • Input a nonempty BST T and a node x.
  • Output the inorder successor of x ,
  • or nil if x is the maximum element of T.
  • Precondition x is not the maximum node of T.
  • 1. If the right subtree of x is not empty,
    return its minimum.
  • 2.Whle x is a right child, set x x.parent.
  • 3. Return x.parent(which may be NIL).

21
BST Delete
Input a BST T and a key Output false if key is
not found in T otherwise true. PostCondition
the key is not in T. 1. If T is empty, return
false. 2. If key lt root.key, return the value
returned by recursively deleting the key from
the left subtree. 3. If key gt root.key, return
the value returned by recursively deleting the
key from the right subtree. 4. If T is a
singleton, make it the empty tree and return
true. 5. If the left subtree is empty, copy the
root, left, and right fields of the right
subtree of T into T inself, and return true. 6.
If the right subtree is empty, copy the root,
left, and right fields of the left subtree of T
into T itself, and return true. 7. Replace the
root with the node returned by applying
deleteMinimum to the right subtree, and return
true.
22
Case 1 of Deletion
23
Case 2
24
Case 3
25
BST Performance
  • ? BST Insert and Search
  • B(n) T(1)
  • A(n) T(lg n)
  • W(n) T(n)
  • ?Average Time Complexity of the BST Search and
    Insertion

lg n log2 n
26
AVL Tree
?AVL (Adelson-Velskii-Landis) Tree It is a
binary search tree that maintains its balance by
forcing the two subtrees at any node to have
nearly the same height. This is done by rotating
subtrees whenever an imbalance occurs. ?AVL
Rotation ? Simple Rotate Left ? Simple
Rotate Right ? Compound Right-Left ? Compound
Left-Right
27
Rotate Left (1)
28
Rotate Left (2)
29
Double Rotation (1)
30
Double Rotation (2)
31
AVL Rotation Patterns
Write a Comment
User Comments (0)
About PowerShow.com