SlideShare une entreprise Scribd logo
1  sur  43
Binary Search Trees



                                                               ‣ basic implementations
                                                               ‣ randomized BSTs
                                                               ‣ deletion in BSTs


References:
  Algorithms in Java, Chapter 12
  Intro to Programming, Section 4.4                           Except as otherwise noted, the content of this presentation
  http://www.cs.princeton.edu/algs4                           is licensed under the Creative Commons Attribution 2.5 License.




Algorithms in Java, 4th Edition   · Robert Sedgewick and Kevin Wayne · Copyright © 2008             ·    September 2, 2009 2:44:27 AM
Elementary implementations: summary




                          worst case          average case       ordered     operations
      implementation
                                                                iteration?    on keys
                        search   insert   search hit   insert

      unordered array     N        N        N/2          N         no         equals()


      unordered list      N        N        N/2          N         no         equals()


       ordered array     lg N      N        lg N       N/2         yes       compareTo()


        ordered list      N        N        N/2        N/2         yes       compareTo()




Challenge. Efficient implementations of search and insert.


                                                                                           2
‣ binary search tree
‣ randomized BSTs
‣ deletion in BSTs




                       3
Binary search trees


Def. A BST is a binary tree in symmetric order.


                                                                       it

A binary tree is either:                                    best                                               was

•   Empty.                                                                                  the


•   A key-value pair and two disjoint binary trees.                                 of                 times




Symmetric order. Every node’s key is:
•   Larger than all keys in its left subtree.                                   BST

•   Smaller than all keys in its right subtree.           Node                key     val




                                                                        left         right



                                                      BST with smaller keys                 BST with larger keys

                                                                    Binary search tree

                                                                                                                     4
BST representation


A BST is a reference to a root node.


A Node is comprised of four fields:                                          key     val

•   A Key and a Value.                                                       left   right

•   A reference to the left and right subtree.

                           smaller keys   larger keys
                                                                      root


     private class Node                                                      it     2
     {
        private Key key;
        private Value val;                              best    1                             was      2

        private Node left;                              null   null                                   null

        private Node right;
     }                                                                                  the   1


    Key and Value are generic types;
    Key is Comparable
                                                                             of      1        times     1
                                                                             null   null       null    null

                                                                                                              5
BST implementation (skeleton)


 public class BST<Key extends Comparable<Key>, Value>
 {
     private Node root;                                 instance variable


      private class Node
      {
          private Key key;
          private Value val;
          private Node left, right;
                                                        instance variable
          public Node(Key key, Value val)
          {
              this.key = key;
              this.val = val;
          }
      }

     public void put(Key key, Value val)
     { /* see next slides */ }

     public Val get(Key key)
     { /* see next slides */    }
 }

                                                                            6
BST search


Get. Return value corresponding to given key, or null if no such key.


                     successful search
               successful search                                                 unsuccessful search
                                                                           unsuccessful search
           for a node node key the the
                 for a with with key                                  for a node node key times
                                                                            for a with with key times
                                                                                                    times is after
                                                                                                times is after it it
                it   it                                                      it   it
                                                                                                so go so go to the right
                                                                                                      to the right
    best best                             was was                best best                       was was
            the is after after it
                  the is it
            so go so go to the right the
                  to the right the                                                       the the

                          of   of                                                  of   of


                it   it                                                      it   it

    best best                             was was                best best                       was was
                                                                                                            times is before
                                                                                                        times is before was was
                                    the the                                              the the
                                                                                                          so go so go to the left
                                                                                                                to the left
                                         the is before
                                     the is before was was
                          of   of                                                  of   of
                                       so go so go to the left
                                             to the left
                it   it                                                      it   it

    best best                             was was                best best                       was was

                                    the the                                              the the        times is after after the
                                                                                                            times is the
                                                                                                    but the right rightis null null
                                                                                                          but the link link is
                          of   of                                                  of   of          so theso the has no node node
                                                                                                           BST BST has no
                                    success!
                               success!                                                                  having that key key
                                                                                                               having that

                                                     Searching in a in a BST
                                                         Searching BST
                                                                                                                                      7
BST search: Java implementation


Get. Return value corresponding to given key, or null if no such key.



               public Value get(Key key)
               {
                  Node x = root;
                  while (x != null)
                  {
                     int cmp = key.compareTo(x.key);
                     if      (cmp < 0) x = x.left;
                     else if (cmp > 0) x = x.right;
                     else if (cmp == 0) return x.val;
                     }
                  return null;
               }




Running time. Proportional to depth of node.


                                                                        8
BST insert


Put. Associate value with key.   insert times

                                                                times is after it
                                                it              so go to the right
                                   best                          was

                                                          the

                                                     of


                                                it

                                   best                          was
                                                                        times is before was
                                                          the
                                                                          so go to the left
                                                     of


                                                it

                                   best                          was

                                                          the

                                                     of             times is after the
                                                                   so it goes on the right

                                                it

                                   best                                    was

                                                          the

                                                     of         times



                                           Inserting a new node into a BST                    9
BST insert: Java implementation


Put. Associate value with key.




    public void put(Key key, Value val)
    { root = put(root, key, val); }

    private Node put(Node x, Key key, Value val)
    {
       if (x == null) return new Node(key, val);
       int cmp = key.compareTo(x.key);
       if      (cmp < 0) x.left = put(x.left, key, val);     concise, but tricky,
       else if (cmp > 0) x.right = put(x.right, key, val);   recursive code;
                                                             read carefully!
       else if (cmp == 0) x.val = val;
       return x;
    }




Running time. Proportional to depth of node.
                                                                              10
the

BST construction example                                               of              it


                                                                              best                                   was


                                                                                                            the


           key                                                                                    of
         inserted
           it                      it                                 times            it

           was                it                                            best                                       was

                                        was                                                            the

           the           it
                                                                                                 of           times

                                              was
                                                                      worst                 it
                                   the
                                                                       best                                          was
          best                     it
                                                                                                      the                  worst
                     best                                 was
                                                                                             of              times
                                              the
                                                                                     Constructing a BST
           of                 it


                  best                                          was


                                                    the


                                         of



          times               it
                                                                                                                                   11
BST insertion: visualization


Ex. Insert keys in random order.




                                   12
BST insertion: visualization


Ex. Insert keys in random order.




                                   12
Correspondence between BSTs and quicksort partitioning




                                                             K
                                                 E                                       S

                                             C           I                       Q           T

                                         A           E               M               R               U

                                                                 L           P                   U

                                                                         O




Remark. Correspondence is 1-1 if no duplicate keys.
                                                                                                         13
Tree shape


 •     Many BSTs correspond to same input data.
 •     Cost of search/insert is proportional to depth of node.



                best case                                  typical case                    worst case

                   the                                         of                               best


         it                      was                  it                  times                    it


best          of         times         worst   best                 the           was                       of

                                                                                        worst                    the

                                                                                                                   times

                                                                                                                       was

                                                                                                                           worst



                                         Typical BSTs constructed from randomly ordered keys
Typical BSTs constructed from randomly ordered keys                                                                        worst

                                                                                                                       was


 Remark. Tree shape depends on order of insertion.                                                                 times

                                                                                                                 the

                                                                                                            of

                                                                                                                                   14
                                                                                                       it
BSTs: mathematical analysis


Proposition. If keys are inserted in random order, the expected number of
compares for a search/insert is ~ 2 ln N.


Pf. 1-1 correspondence with quicksort partitioning.



Proposition. [Reed, 2003] If keys are inserted in random order,
expected height of tree is ~ 4.311 ln N – 1.953 ln ln N.



But… Worst-case for search/insert/height is N (but occurs with
exponentially small chance when keys are inserted in random order).




                                                                            15
ST implementations: summary




                      guarantee           average case
                                                               ordered      operations
  implementation
                                                              iteration?     on keys
                   search   insert   search hit    insert

 unordered array     N        N        N/2           N           no         equals()


  unordered list     N        N        N/2           N           no         equals()


  ordered array     lg N      N        lg N         N/2          yes       compareTo()


   ordered list      N        N        N/2          N/2          yes       compareTo()


       BST           N        N      1.38 lg N    1.38 lg N       ?        compareTo()




Next challenge. Ordered iteration.



                                                                                         16
Inorder traversal


Traversing the tree inorder yields keys in ascending order.




 public void show()
 { return show(root);    }                                                      BST

                                                                              key     val

 private void show(Node x)
 {                                                                       left        right
    if (x == null) return;
    show(x.left);                                     BST with smaller keys                 BST with larger keys

    StdOut.println(x.key + " " + x.val);
                                                     smaller keys, in order         key      larger keys, in order
    show(x.right);
}                                                                                                       all keys, in order

                                                  Recursive inorder traversal of a binary search tree




To implement an iterator: need a non-recursive version.


                                                                                                                             17
Non-recursive inorder traversal


To process a node:
•   Follow left links until empty (pushing onto stack).
•   Pop and process.
•   Follow right link (push onto stack).


     visit(E)                              E
       visit(B)                            E   B
         visit(A)                          E   B A                      E
           print A                A        E   B
         print B                  B        E
                                                                B                   S
         visit(C)                          E   C
           print C                C        E
       print E                    E        -                A       C       I
       visit(S)                            S
         visit(I)                          S   I
           visit(H)                        S   I H                      H       N
              print H             H        S   I
           print I                I        S
           visit(N)                        S   N
              print N             N        S
         print S                  S        -

           recursive calls      output     stack contents
                                                                                        18
Inorder iteartor: Java implementation


        public Iterator<Key> iterator()
        { return new Inorder(); }

        private class Inorder implements Iterator<Key>
        {
           private Stack<Node> stack = new Stack<Node>();

            private void pushLeft(Node x)
            {                                               go down left
                while (x != null)                           spine and push
                { stack.push(x); x = x.left;   }            all keys onto stack
            }

            BSTIterator()
            { pushLeft(root);   }

            public boolean hasNext()
            { return !stack.isEmpty();    }

            public Key next()
            {
               Node x = stack.pop();
               pushLeft(x.right);
               return x.key;
            }
        }

                                                                                  19
ST implementations: summary




                      guarantee           average case
                                                               ordered      operations
  implementation
                                                              iteration?     on keys
                   search   insert   search hit    insert

 unordered array     N        N        N/2           N           no         equals()


  unordered list     N        N        N/2           N           no         equals()


  ordered array     lg N      N        lg N         N/2          yes       compareTo()


   ordered list      N        N        N/2          N/2          yes       compareTo()


       BST           N        N      1.38 lg N    1.38 lg N      yes       compareTo()




Next challenge. Guaranteed efficiency for search and insert.



                                                                                         20
Searching challenge 3 (revisited):


Problem. Frequency counts in “Tale of Two Cities”
Assumptions. Book has 135,000+ words; about 10,000 distinct words.



Which searching method to use?
1) Unordered array.
2) Unordered linked list
3) Ordered array with binary search.
4) Need better method, all too slow.
5) Doesn’t matter much, all fast enough.
6) BSTs.

                  insertion cost < 10000 * 1.38 * lg 10000 < .2 million
                  lookup cost < 135000 * 1.38 * lg 10000 < 2.5 million




                                                                          21
‣ basic implementations
‣ randomized BSTs
‣ deletion in BSTs




                          22
Rotation in BSTs


Two fundamental operations to rearrange nodes in a tree.
•   Maintain symmetric order.
•   Local transformations (change just 3 pointers).
•   Basis for advanced BST algorithms.


Strategy. Use rotations on insert to adjust tree shape to be more balanced.
       h
                 u                                              h
                                                                        v


                                    h = rotL(u)
                     v
                                                           u


       A                            h = rotR(v)                          C



             B           C                                 A        B




Key point. No change to BST search code (!)
                                                                              23
Rotation in BSTs


 Two fundamental operations to rearrange nodes in a tree.
 •   Easier done than said.
 •   Raise some nodes, lowers some others.




root = rotL(A)                                              a.right = rotR(S)
                              private Node rotL(Node h)
                              {
                                 Node v = h.right;
                                 h.right = v.left;
                                 v.left = h;
                                 return v;
                              }



                              private Node rotR(Node h)
                              {
                                 Node u = h.left;
                                 h.left = u.right;
                                 u.right = h;
                                 return u;
                              }



                                                                                24
BST root insertion

                                                                         insert G
Insert a node and make it the new root.
•   Insert node at bottom, as in standard BST.
•   Rotate inserted node to the root.
•   Compact recursive implementation.


     private Node putRoot(Node x, Key key, Val val)
     {
        if (x == null) return new Node(key, val);
        int cmp = key.compareTo(x.key);
        if (cmp < 0)
        {
           x.left = putRoot(x.left, key, val);
           x = rotR(x);
        }                                             tricky recursive
        else if (cmp > 0)                             code; read very
        {                                             carefully!
           x.right = putRoot(x.right, key, val);
           x = rotL(x);
        }
        else if (cmp == 0) x.val = val;
        return x;
    }

                                                                                    25
BST root insertion: construction


Ex. A S E R C H I N G X M P L




Why bother?
•   Recently inserted keys are near the top (better for some clients).
•   Basis for randomized BST.


                                                                         26
Randomized BSTs (Roura, 1996)


Intuition. If tree is random, height is logarithmic.
Fact. Each node in a random tree is equally likely to be the root.


Idea. Since new node should be the root with probability 1/(N+1),
make it the root (via root insertion) with probability 1/(N+1).

                                                                     and apply idea recursively
     private Node put(Node x, Key key, Value val)
     {
        if (x == null) return new Node(key, val);
        int cmp = key.compareTo(x.key);

         if (cmp == 0) { x.val = val; return x; }                 no rotations if key in table


         if (StdRandom.bernoulli(1.0 / (x.N + 1.0))               root insert with the right
            return putRoot(h, key, val);                          probability


         if      (cmp < 0) x.left = put(x.left, key, val);
         else if (cmp > 0) x.right = put(x.right, key, val);

                                                                  maintain count of nodes in
         x.N++;
                                                                  subtree rooted at x
         return x;
     }

                                                                                                  27
Randomized BST: construction


Ex. Insert 15 keys in ascending order.




                                         28
Randomized BST construction: visualization


Ex. Insert 500 keys in random order.




                                             29
Randomized BST construction: visualization


Ex. Insert 500 keys in random order.




                                             29
Randomized BST: analysis


Proposition. Randomized BSTs have the same distribution as BSTs under
                              of


random insertion order, no matter in what order keys are inserted.
                           it           times


                        best               the          was


                                                              worst




                   Typical BSTs constructed from randomly ordered keys



•   Expected height is ~ 4.31107 ln N.
•   Average search cost is ~ 2 ln N.
•   Exponentially small chance of bad balance.



Implementation cost. Need to maintain subtree size in each node.
                                                                         30
ST implementations: summary



                       guarantee           average case
                                                                ordered            operations
   implementation
                                                               iteration?           on keys
                    search   insert   search hit    insert

  unordered array     N        N        N/2           N            no              equals()


   unordered list     N        N        N/2           N            no              equals()


   ordered array     lg N      N        lg N         N/2           yes           compareTo()


    ordered list      N        N        N/2          N/2           yes           compareTo()


        BST           N        N      1.38 lg N    1.38 lg N       yes           compareTo()


  randomized BST    3 lg N   3 lg N   1.38 lg N    1.38 lg N       yes           compareTo()




Bottom line. Randomized BSTs provide the desired guarantee.

                                                               probabilistic, with exponentially
Bonus. Randomized BSTs also support delete (!)
                                                               small chance of linear time
                                                                                                   31
‣ basic implementations
‣ randomized BSTs
‣ deletion in BSTs




                          32
BST deletion: lazy approach


To remove a node with a given key:
•   Set its value to null.
•   Leave key in tree to guide searches (but don't consider it equal to search key).


                    E                                           E


          A                      S                    A                     S

                                     delete I
              C         I                                 C         I           tombstone


                    H        R                                  H       R


                        N                                           N



Cost. O(log N') per insert, search, and delete, where N' is the number of
elements ever inserted in the BST.


Unsatisfactory solution. Tombstone overload.
                                                                                            33
BST deletion: Hibbard deletion


To remove a node from a BST:
•   Zero children: just remove it.
•   One child: pass the child up.
•   Two children: find the next largest node using right-left*, swap with
    next largest, remove as above.




            zero children             one child               two children




                                                                             34
BST deletion: Hibbard deletion


To remove a node from a BST:
•   Zero children: just remove it.
•   One child: pass the child up.
•   Two children: find the next largest node using right-left*, swap with
    next largest, remove as above.




            zero children             one child               two children




Unsatisfactory solution. Not symmetric, code is clumsy.
Surprising consequence. Trees not random (!) ⇒ sqrt(N) per op.
Longstanding open problem. Simple and efficient delete for BSTs.
                                                                             35
Randomized BST deletion


To delete a node containing a given key:
•   Find the node containing the key.
•   Remove the node.
•   Join its two subtrees to make a tree.



Ex. Delete S.


                    E

        A                               S



            C             I                 X

                    H               R

                              N


                                                36
Randomized BST deletion


To delete a node containing a given key:                    private Node remove(Node x, Key key)

•   Find the node containing the key.                       {
                                                               if (x == null) return null;
•   Remove the node.                                           int cmp = key.compareTo(x.key);

•
                                                               if (cmp < 0)
    Join its two subtrees to make a tree.                         x.left = remove(x.left, key);
                                                               else if (cmp > 0)
                                                                  x.right = remove(x.right, key);
                                                               else if (cmp == 0)
                                                                  return join(x.left, x.right);
Ex. Delete S.                                                  return x;
                                                            }


                    E
                              join these two subtrees
        A



            C             I                             X

                    H                    R

                                 N


                                                                                                    37
Randomized BST join


To join two subtrees with all keys in one less than all keys in the other:
•   Maintain counts of nodes in subtrees a and b.
•   With probability |a|/(|a|+|b|):
    - root = root of a
    - left subtree = left subtree of a
    - right subtree = join b and right subtree of a
•   With probability |a|/(|a|+|b|) do the symmetric operations.


                                               make I the root        and recursively join these
                to join these two subtrees     with probability 4/5   two subtrees to form right
                                                                      subtree of I



                                        X
            I                           X                 I                       X

        H                  R                        H                 R

                   N                                            N



                                                                                                   38
Randomized BST join


To join two subtrees with all keys in one less than all keys in the other:
•   Maintain counts of nodes in subtrees a and b.
•   With probability |a|/(|a|+|b|):
    - root = root of a
    - left subtree = left subtree of a
    - right subtree = join b and right subtree of a
•   With probability |a|/(|a|+|b|) do the symmetric operations.

                                                                   to join these two
                                                                   subtrees

private Node join(Node a, Node b)
{
   if (a == null) return b;                                                    X
   if (b == null) return a;
                                                                  R
   if (StdRandom.bernoulli((double) a.N / (a.N + b.N))
   { a.right = join(a.right, b); return a; }
   else                                                      N         make R the root
   { b.left = join(a, b.left ); return b; }                            with probability 2/3
}
                                                                              R

                                                                      N                X
                                                                                              39
Randomized BST deletion


To delete a node containing a given key:
•   Find the node containing the key.
•   Remove the node.
•   Join its two subtrees to make a tree.



              E                                            E

    A                           S                  A               I


        C           I               X                  C       H           R

               H            R
                                                                       N       X
                        N




Proposition. Tree still random after delete (!).
Bottom line. Logarithmic guarantee for search/insert/delete.
                                                                                   40
ST implementations: summary



                           guarantee                         average case
                                                                                          ordered         operations
implementation
                                                                                         iteration?        on keys
                  search    insert     delete   search hit      insert       delete

unordered array     N         N          N        N/2             N           N/2            no           equals()


unordered list      N         N          N        N/2             N           N/2            no           equals()


 ordered array     lg N       N          N        lg N           N/2          N/2            yes        compareTo()


  ordered list      N         N          N        N/2            N/2          N/2            yes        compareTo()


     BST            N         N          N      1.38 lg N     1.38 lg N        ?             yes        compareTo()


randomized BST    3 lg N    3 lg N     3 lg N   1.38 lg N     1.38 lg N     1.38 lg N        yes        compareTo()




   Bottom line. Randomized BSTs provide the desired guarantee.
   Next lecture. Can we do better?
                                                                              probabilistic, with exponentially
                                                                              small chance of linear time
                                                                                                                       41

Contenu connexe

En vedette

Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Javageeksrik
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data StructureAnuj Modi
 
Trees data structure
Trees data structureTrees data structure
Trees data structureSumit Gupta
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)Trupti Agrawal
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 

En vedette (6)

Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Trees
TreesTrees
Trees
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Plus de Sri Prasanna

Plus de Sri Prasanna (20)

Qr codes para tech radar
Qr codes para tech radarQr codes para tech radar
Qr codes para tech radar
 
Qr codes para tech radar 2
Qr codes para tech radar 2Qr codes para tech radar 2
Qr codes para tech radar 2
 
Test
TestTest
Test
 
Test
TestTest
Test
 
assds
assdsassds
assds
 
assds
assdsassds
assds
 
asdsa
asdsaasdsa
asdsa
 
dsd
dsddsd
dsd
 
About stacks
About stacksAbout stacks
About stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
Network and distributed systems
Network and distributed systemsNetwork and distributed systems
Network and distributed systems
 
Introduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clustersIntroduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clusters
 
Mapreduce: Theory and implementation
Mapreduce: Theory and implementationMapreduce: Theory and implementation
Mapreduce: Theory and implementation
 
Other distributed systems
Other distributed systemsOther distributed systems
Other distributed systems
 

Dernier

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 

Dernier (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

Binary Search Trees

  • 1. Binary Search Trees ‣ basic implementations ‣ randomized BSTs ‣ deletion in BSTs References: Algorithms in Java, Chapter 12 Intro to Programming, Section 4.4 Except as otherwise noted, the content of this presentation http://www.cs.princeton.edu/algs4 is licensed under the Creative Commons Attribution 2.5 License. Algorithms in Java, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · September 2, 2009 2:44:27 AM
  • 2. Elementary implementations: summary worst case average case ordered operations implementation iteration? on keys search insert search hit insert unordered array N N N/2 N no equals() unordered list N N N/2 N no equals() ordered array lg N N lg N N/2 yes compareTo() ordered list N N N/2 N/2 yes compareTo() Challenge. Efficient implementations of search and insert. 2
  • 3. ‣ binary search tree ‣ randomized BSTs ‣ deletion in BSTs 3
  • 4. Binary search trees Def. A BST is a binary tree in symmetric order. it A binary tree is either: best was • Empty. the • A key-value pair and two disjoint binary trees. of times Symmetric order. Every node’s key is: • Larger than all keys in its left subtree. BST • Smaller than all keys in its right subtree. Node key val left right BST with smaller keys BST with larger keys Binary search tree 4
  • 5. BST representation A BST is a reference to a root node. A Node is comprised of four fields: key val • A Key and a Value. left right • A reference to the left and right subtree. smaller keys larger keys root private class Node it 2 { private Key key; private Value val; best 1 was 2 private Node left; null null null private Node right; } the 1 Key and Value are generic types; Key is Comparable of 1 times 1 null null null null 5
  • 6. BST implementation (skeleton) public class BST<Key extends Comparable<Key>, Value> { private Node root; instance variable private class Node { private Key key; private Value val; private Node left, right; instance variable public Node(Key key, Value val) { this.key = key; this.val = val; } } public void put(Key key, Value val) { /* see next slides */ } public Val get(Key key) { /* see next slides */ } } 6
  • 7. BST search Get. Return value corresponding to given key, or null if no such key. successful search successful search unsuccessful search unsuccessful search for a node node key the the for a with with key for a node node key times for a with with key times times is after times is after it it it it it it so go so go to the right to the right best best was was best best was was the is after after it the is it so go so go to the right the to the right the the the of of of of it it it it best best was was best best was was times is before times is before was was the the the the so go so go to the left to the left the is before the is before was was of of of of so go so go to the left to the left it it it it best best was was best best was was the the the the times is after after the times is the but the right rightis null null but the link link is of of of of so theso the has no node node BST BST has no success! success! having that key key having that Searching in a in a BST Searching BST 7
  • 8. BST search: Java implementation Get. Return value corresponding to given key, or null if no such key. public Value get(Key key) { Node x = root; while (x != null) { int cmp = key.compareTo(x.key); if (cmp < 0) x = x.left; else if (cmp > 0) x = x.right; else if (cmp == 0) return x.val; } return null; } Running time. Proportional to depth of node. 8
  • 9. BST insert Put. Associate value with key. insert times times is after it it so go to the right best was the of it best was times is before was the so go to the left of it best was the of times is after the so it goes on the right it best was the of times Inserting a new node into a BST 9
  • 10. BST insert: Java implementation Put. Associate value with key. public void put(Key key, Value val) { root = put(root, key, val); } private Node put(Node x, Key key, Value val) { if (x == null) return new Node(key, val); int cmp = key.compareTo(x.key); if (cmp < 0) x.left = put(x.left, key, val); concise, but tricky, else if (cmp > 0) x.right = put(x.right, key, val); recursive code; read carefully! else if (cmp == 0) x.val = val; return x; } Running time. Proportional to depth of node. 10
  • 11. the BST construction example of it best was the key of inserted it it times it was it best was was the the it of times was worst it the best was best it the worst best was of times the Constructing a BST of it best was the of times it 11
  • 12. BST insertion: visualization Ex. Insert keys in random order. 12
  • 13. BST insertion: visualization Ex. Insert keys in random order. 12
  • 14. Correspondence between BSTs and quicksort partitioning K E S C I Q T A E M R U L P U O Remark. Correspondence is 1-1 if no duplicate keys. 13
  • 15. Tree shape • Many BSTs correspond to same input data. • Cost of search/insert is proportional to depth of node. best case typical case worst case the of best it was it times it best of times worst best the was of worst the times was worst Typical BSTs constructed from randomly ordered keys Typical BSTs constructed from randomly ordered keys worst was Remark. Tree shape depends on order of insertion. times the of 14 it
  • 16. BSTs: mathematical analysis Proposition. If keys are inserted in random order, the expected number of compares for a search/insert is ~ 2 ln N. Pf. 1-1 correspondence with quicksort partitioning. Proposition. [Reed, 2003] If keys are inserted in random order, expected height of tree is ~ 4.311 ln N – 1.953 ln ln N. But… Worst-case for search/insert/height is N (but occurs with exponentially small chance when keys are inserted in random order). 15
  • 17. ST implementations: summary guarantee average case ordered operations implementation iteration? on keys search insert search hit insert unordered array N N N/2 N no equals() unordered list N N N/2 N no equals() ordered array lg N N lg N N/2 yes compareTo() ordered list N N N/2 N/2 yes compareTo() BST N N 1.38 lg N 1.38 lg N ? compareTo() Next challenge. Ordered iteration. 16
  • 18. Inorder traversal Traversing the tree inorder yields keys in ascending order. public void show() { return show(root); } BST key val private void show(Node x) { left right if (x == null) return; show(x.left); BST with smaller keys BST with larger keys StdOut.println(x.key + " " + x.val); smaller keys, in order key larger keys, in order show(x.right); } all keys, in order Recursive inorder traversal of a binary search tree To implement an iterator: need a non-recursive version. 17
  • 19. Non-recursive inorder traversal To process a node: • Follow left links until empty (pushing onto stack). • Pop and process. • Follow right link (push onto stack). visit(E) E visit(B) E B visit(A) E B A E print A A E B print B B E B S visit(C) E C print C C E print E E - A C I visit(S) S visit(I) S I visit(H) S I H H N print H H S I print I I S visit(N) S N print N N S print S S - recursive calls output stack contents 18
  • 20. Inorder iteartor: Java implementation public Iterator<Key> iterator() { return new Inorder(); } private class Inorder implements Iterator<Key> { private Stack<Node> stack = new Stack<Node>(); private void pushLeft(Node x) { go down left while (x != null) spine and push { stack.push(x); x = x.left; } all keys onto stack } BSTIterator() { pushLeft(root); } public boolean hasNext() { return !stack.isEmpty(); } public Key next() { Node x = stack.pop(); pushLeft(x.right); return x.key; } } 19
  • 21. ST implementations: summary guarantee average case ordered operations implementation iteration? on keys search insert search hit insert unordered array N N N/2 N no equals() unordered list N N N/2 N no equals() ordered array lg N N lg N N/2 yes compareTo() ordered list N N N/2 N/2 yes compareTo() BST N N 1.38 lg N 1.38 lg N yes compareTo() Next challenge. Guaranteed efficiency for search and insert. 20
  • 22. Searching challenge 3 (revisited): Problem. Frequency counts in “Tale of Two Cities” Assumptions. Book has 135,000+ words; about 10,000 distinct words. Which searching method to use? 1) Unordered array. 2) Unordered linked list 3) Ordered array with binary search. 4) Need better method, all too slow. 5) Doesn’t matter much, all fast enough. 6) BSTs. insertion cost < 10000 * 1.38 * lg 10000 < .2 million lookup cost < 135000 * 1.38 * lg 10000 < 2.5 million 21
  • 23. ‣ basic implementations ‣ randomized BSTs ‣ deletion in BSTs 22
  • 24. Rotation in BSTs Two fundamental operations to rearrange nodes in a tree. • Maintain symmetric order. • Local transformations (change just 3 pointers). • Basis for advanced BST algorithms. Strategy. Use rotations on insert to adjust tree shape to be more balanced. h u h v h = rotL(u) v u A h = rotR(v) C B C A B Key point. No change to BST search code (!) 23
  • 25. Rotation in BSTs Two fundamental operations to rearrange nodes in a tree. • Easier done than said. • Raise some nodes, lowers some others. root = rotL(A) a.right = rotR(S) private Node rotL(Node h) { Node v = h.right; h.right = v.left; v.left = h; return v; } private Node rotR(Node h) { Node u = h.left; h.left = u.right; u.right = h; return u; } 24
  • 26. BST root insertion insert G Insert a node and make it the new root. • Insert node at bottom, as in standard BST. • Rotate inserted node to the root. • Compact recursive implementation. private Node putRoot(Node x, Key key, Val val) { if (x == null) return new Node(key, val); int cmp = key.compareTo(x.key); if (cmp < 0) { x.left = putRoot(x.left, key, val); x = rotR(x); } tricky recursive else if (cmp > 0) code; read very { carefully! x.right = putRoot(x.right, key, val); x = rotL(x); } else if (cmp == 0) x.val = val; return x; } 25
  • 27. BST root insertion: construction Ex. A S E R C H I N G X M P L Why bother? • Recently inserted keys are near the top (better for some clients). • Basis for randomized BST. 26
  • 28. Randomized BSTs (Roura, 1996) Intuition. If tree is random, height is logarithmic. Fact. Each node in a random tree is equally likely to be the root. Idea. Since new node should be the root with probability 1/(N+1), make it the root (via root insertion) with probability 1/(N+1). and apply idea recursively private Node put(Node x, Key key, Value val) { if (x == null) return new Node(key, val); int cmp = key.compareTo(x.key); if (cmp == 0) { x.val = val; return x; } no rotations if key in table if (StdRandom.bernoulli(1.0 / (x.N + 1.0)) root insert with the right return putRoot(h, key, val); probability if (cmp < 0) x.left = put(x.left, key, val); else if (cmp > 0) x.right = put(x.right, key, val); maintain count of nodes in x.N++; subtree rooted at x return x; } 27
  • 29. Randomized BST: construction Ex. Insert 15 keys in ascending order. 28
  • 30. Randomized BST construction: visualization Ex. Insert 500 keys in random order. 29
  • 31. Randomized BST construction: visualization Ex. Insert 500 keys in random order. 29
  • 32. Randomized BST: analysis Proposition. Randomized BSTs have the same distribution as BSTs under of random insertion order, no matter in what order keys are inserted. it times best the was worst Typical BSTs constructed from randomly ordered keys • Expected height is ~ 4.31107 ln N. • Average search cost is ~ 2 ln N. • Exponentially small chance of bad balance. Implementation cost. Need to maintain subtree size in each node. 30
  • 33. ST implementations: summary guarantee average case ordered operations implementation iteration? on keys search insert search hit insert unordered array N N N/2 N no equals() unordered list N N N/2 N no equals() ordered array lg N N lg N N/2 yes compareTo() ordered list N N N/2 N/2 yes compareTo() BST N N 1.38 lg N 1.38 lg N yes compareTo() randomized BST 3 lg N 3 lg N 1.38 lg N 1.38 lg N yes compareTo() Bottom line. Randomized BSTs provide the desired guarantee. probabilistic, with exponentially Bonus. Randomized BSTs also support delete (!) small chance of linear time 31
  • 34. ‣ basic implementations ‣ randomized BSTs ‣ deletion in BSTs 32
  • 35. BST deletion: lazy approach To remove a node with a given key: • Set its value to null. • Leave key in tree to guide searches (but don't consider it equal to search key). E E A S A S delete I C I C I tombstone H R H R N N Cost. O(log N') per insert, search, and delete, where N' is the number of elements ever inserted in the BST. Unsatisfactory solution. Tombstone overload. 33
  • 36. BST deletion: Hibbard deletion To remove a node from a BST: • Zero children: just remove it. • One child: pass the child up. • Two children: find the next largest node using right-left*, swap with next largest, remove as above. zero children one child two children 34
  • 37. BST deletion: Hibbard deletion To remove a node from a BST: • Zero children: just remove it. • One child: pass the child up. • Two children: find the next largest node using right-left*, swap with next largest, remove as above. zero children one child two children Unsatisfactory solution. Not symmetric, code is clumsy. Surprising consequence. Trees not random (!) ⇒ sqrt(N) per op. Longstanding open problem. Simple and efficient delete for BSTs. 35
  • 38. Randomized BST deletion To delete a node containing a given key: • Find the node containing the key. • Remove the node. • Join its two subtrees to make a tree. Ex. Delete S. E A S C I X H R N 36
  • 39. Randomized BST deletion To delete a node containing a given key: private Node remove(Node x, Key key) • Find the node containing the key. { if (x == null) return null; • Remove the node. int cmp = key.compareTo(x.key); • if (cmp < 0) Join its two subtrees to make a tree. x.left = remove(x.left, key); else if (cmp > 0) x.right = remove(x.right, key); else if (cmp == 0) return join(x.left, x.right); Ex. Delete S. return x; } E join these two subtrees A C I X H R N 37
  • 40. Randomized BST join To join two subtrees with all keys in one less than all keys in the other: • Maintain counts of nodes in subtrees a and b. • With probability |a|/(|a|+|b|): - root = root of a - left subtree = left subtree of a - right subtree = join b and right subtree of a • With probability |a|/(|a|+|b|) do the symmetric operations. make I the root and recursively join these to join these two subtrees with probability 4/5 two subtrees to form right subtree of I X I X I X H R H R N N 38
  • 41. Randomized BST join To join two subtrees with all keys in one less than all keys in the other: • Maintain counts of nodes in subtrees a and b. • With probability |a|/(|a|+|b|): - root = root of a - left subtree = left subtree of a - right subtree = join b and right subtree of a • With probability |a|/(|a|+|b|) do the symmetric operations. to join these two subtrees private Node join(Node a, Node b) { if (a == null) return b; X if (b == null) return a; R if (StdRandom.bernoulli((double) a.N / (a.N + b.N)) { a.right = join(a.right, b); return a; } else N make R the root { b.left = join(a, b.left ); return b; } with probability 2/3 } R N X 39
  • 42. Randomized BST deletion To delete a node containing a given key: • Find the node containing the key. • Remove the node. • Join its two subtrees to make a tree. E E A S A I C I X C H R H R N X N Proposition. Tree still random after delete (!). Bottom line. Logarithmic guarantee for search/insert/delete. 40
  • 43. ST implementations: summary guarantee average case ordered operations implementation iteration? on keys search insert delete search hit insert delete unordered array N N N N/2 N N/2 no equals() unordered list N N N N/2 N N/2 no equals() ordered array lg N N N lg N N/2 N/2 yes compareTo() ordered list N N N N/2 N/2 N/2 yes compareTo() BST N N N 1.38 lg N 1.38 lg N ? yes compareTo() randomized BST 3 lg N 3 lg N 3 lg N 1.38 lg N 1.38 lg N 1.38 lg N yes compareTo() Bottom line. Randomized BSTs provide the desired guarantee. Next lecture. Can we do better? probabilistic, with exponentially small chance of linear time 41

Notes de l'éditeur

  1. Key extends Comparable: not actually inheritance - means that whatever generic type Item the client specifies must implement the Comparable interface
  2. Use GrowingTree WebStart application to demo
  3. Here depth of node = depth of node containing the key (or bottom of tree where search ends)
  4. Search, then insert Use GrowingTree WebStart application to demo iterative version more cumbersome, but not difficult
  5. Here depth of node = depth of node containing the key (or bottom of tree where search ends) Iterative version possible, but more cumbersome
  6. variance = O(1) for both propositions worst case = keys inserted in ascending order Reference: The Height of a Random Binary Search Tree by Bruce Reed, JACM 2003
  7. Approach. Mimic recursive inorder traversal.
  8. The rotate methods return the resulting subtree. The third pointer changed is the pointer of the parent.
  9. exact same code as before, except for rotations
  10. Note: root inserting N keys inorder results in same tree as inserting them in reverse order
  11. The 4.33107 ln N term is actually 4.31107 ln N - 1.95 ln ln N. The second term might have an impact for moderate values of N
  12. Note 4.31107 ln N ~ 2.99 lg N
  13. right-left* means right-left-left-left-left-left until you find the next largest Hibbard = person credited with deletion strategy Conjecture (that I think is still unresolved): symmetric version of Hibbard deletion leads to O(log N) performance Note figure contains duplicate keys...
  14. right-left* means right-left-left-left-left-left until you find the next largest Hibbard = person credited with deletion strategy Conjecture (that I think is still unresolved): symmetric version of Hibbard deletion leads to O(log N) performance Note figure contains duplicate keys...
  15. Two roots have a battle. Which one should win?
  16. Just because X lost the battle with I doesn&apos;t mean that X should be at bottom of tree. Now it gets to throwdown with subtree rooted at R to see who becomes the right child
  17. an illustration created by Ellen Kim &apos;10, inspired by randomized BST lecture