SlideShare a Scribd company logo
1 of 20
More Searching




 Dan Barrish-Flood




     4a-Searching-More   1
Running time of Dynamic Set
            Operations
• we’ve seen that all our dynamic set operations
  can be done in Θ(h) time (worst-case) on a
  binary search tree of height h.
• a binary search tree of n nodes has height
  Θ(lgn)
• so all dynamic set operations take Θ(lgn) time
  on a binary search tree.
• right? WRONG!
• insert these numbers to form a binary search
  tree, in this order: 1, 2, 3, 4, 5
                    4a-Searching-More              2
Binary Search Tree Random
         Insertion & Deletion
• What is the average depth of a BST after n
  insertions of random values?
• The root is equally likely to be the smallest, 2nd
  smallest, ..., largest, of the n values. So with
  equal probability, the two subtrees are of sizes
   1 &   n-2 (not n-1, don’t forget the root!)
   2 &   n-3
   ...
   n-3   & 2
   n-2   & 1
Wait til you see the next slide!...

                      4a-Searching-More                3
Random Insertion/Deletion (Cont’d)
• The analysis is identical to Quicksort!
• The root key is like the pivot.
• So n insertions take O(nlgn) on average,
  thus each insertion takes O(lgn), which is
  the tree height.




                  4a-Searching-More            4
Balanced Search Trees
• Careful, I may have used BST in the past for
  “binary search tree” not “balanced search tree”,
  I will try to avoid this confusion!
• The general idea (there are exceptions)
  – do extra work during INSERT and DELETE to ensure
    the tree’s height is Θ(lgn)
  – the rest of the dynamic set operations are unchanged.
• Two examples:
  – Red-Black Trees (CLRS ch. 13)
     • elegant definition
     • wicked hairy insert/delete
  – 2-3 Trees
     • simpler to understand
     • not true binary trees
                          4a-Searching-More             5
a taste of Red-Black Trees
•    a Red-Black tree is a binary search tree where
2.   the root is BLACK
3.   each node is colored RED or BLACK
4.   every leaf is black
5.   each of a red node’s children are black
6.   every path from a node to one of its
     descendant leaves contains the same number
     of black nodes
•    (a minor twist: we consider the NIL’s as leaves,
     and all the nodes with keys as internal nodes)
•    the height of a node (for trees in general) is
     the # of edges on the longest downward path
     from that node to a leaf.
                       4a-Searching-More                6
4a-Searching-More   7
RBT, each leaf (NIL) is black




the tiny number next to a node is its “black-
height”
                   4a-Searching-More            8
Dynamic Set Operations as
       implemented with RBT’s
It’s not that hard to prove (by induction) that the
   RBT properties imply:
• a RBT with n internal nodes has
    height ≤ 2 lg(n+1)
• don’t worry about the proof, but see CLRS p.
   274 if you’re interested in the details
• The height of a RBT is O(lgn), so all dynamic
   set operations run in O(lgn) time.
• But wait! INSERT, DELETE may destroy the
   RBT property! But, it turns out, these two
   operations can indeed be supported in O(lgn)
   time, too.
                     4a-Searching-More                9
More on tree-traversal, here is pre-order traversal
          (note this is not a binary tree)
First “process” (e.g. print) the root (hence pre),
then recursively process the root’s left subtree(s),
then recursively process the root’s right
subtree(s). For this tree, we get 1,2,3,...,9




                        4a-Searching-More              10
More on tree traversal, here is post-order
     traversal (note this is not a binary tree)
First recursively process the root’s left subtree(s), then
recursively process the root’s right subtree(s), then
lastly (hence post) “process” (e.g. print) the root. For
this tree, we again get 1,2,3,...,9




                        4a-Searching-More                    11
2-3 Trees
• Another kind of Balanced Search Tree
• What are the structural requirements:
   – every non-leaf (i.e. internal) node has exactly 2 or 3 children
   – all leaves are at the same level
   – here are three 2-3 trees (not showing keys!)




                            4a-Searching-More                          12
2-3 Trees, more structural info
• the “thinnest” 2-3 tree is a complete binary tree
  (see picture on prev. slide)
• the “fattest” 2-3 tree is a complete 3-ary tree
  (again see prev. slide).
• If the tree has height h, (recall all leaves are at
  the same level), the number of leaves, l, is
  between 2h and 3h.
• A 2-3 tree of n nodes (total) has a height
  between log3n and log2n, and since log3n = log32
  x log2n (why?), the height h is guaranteed to be
  within a small constant factor of log2n.
                     4a-Searching-More              13
Where is the data in a 2-3 tree? All the records (keys,
other satellite data) are in the leaves. The records are in
sorted order. Each internal node has one or two
guides: the greatest value in its leftmost one (or two)
subtree(s).




                        4a-Searching-More              14
Searching in a 2-3 Tree: Suppose I’m searching the above
2-3 tree (the one with the guides 8, 29 in the root) for the key 27.
Start at the root. 27 ≤ 8? No, so forget about the root’s leftmost
subtree. 27 ≤ 29? Yes, so we know our target (if it exists) is in the
root’s middle subtree. Thus proceed to the node directly below the
root? 27 ≤ 11? No. So forget about this node’s leftmost subtree.
27 ≤ 21? No, so we know our target (if it exists) must be in this
node’s rightmost subtree. So follow our current node’s rightmost
pointer down. We’re at a leaf (the good stuff is in here!), and we
find our target 27 stored in this leaf. We use this method to
always reach the “right” leaf, where we will either find our target in
that leaf, or find that our target does not exist.
SEARCH(2-3_tree, key) clearly, because of the height of the tree,
runs in worst-case time O(lg n).
I don’t have any good pseudo-code (maybe I’ll make or find some)
                          4a-Searching-More                 15
for this routine.
2-3 Tree Insert (let’s insert 15 into the prev. tree)




                                         tree has increased
                                         in height!

                     4a-Searching-More                        16
2-3 Tree Delete; let’s delete 10
from the 1st tree to get the 2nd one
          (one method).




         here we stole 11 from a sibling

                4a-Searching-More          17
2-3 Tree Delete; let’s delete 10
from the 1st tree to get the 2nd one
         (other method).




       here we merged nodes

                4a-Searching-More      18
2-3 Tree Delete; let’s delete 3 from the 1st
 tree to get the 3rd, there’s just one way).




                       again, we stole
                       from a sibling
                 4a-Searching-More             19
2-3 Tree Delete; let’s delete 5




          tree loses
          a level!
            4a-Searching-More     20

More Related Content

What's hot

Binary tree traversal ppt
Binary tree traversal pptBinary tree traversal ppt
Binary tree traversal pptPREEYANKAV
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Treeleminhvuong
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVLKatang Isip
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data StructureOm Prakash
 
Binary search trees
Binary search treesBinary search trees
Binary search treesDwight Sabio
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAbhishek L.R
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Treecinterviews
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap treezia eagle
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeZafar Ayub
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)Trupti Agrawal
 
Difference between complete,ordered,full,strict,perfect and balanced binary tree
Difference between complete,ordered,full,strict,perfect and balanced binary treeDifference between complete,ordered,full,strict,perfect and balanced binary tree
Difference between complete,ordered,full,strict,perfect and balanced binary treeAjay Chimmani
 

What's hot (20)

BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Binary tree traversal ppt
Binary tree traversal pptBinary tree traversal ppt
Binary tree traversal ppt
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Tree
TreeTree
Tree
 
binary tree
binary treebinary tree
binary tree
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
Trees
TreesTrees
Trees
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Difference between complete,ordered,full,strict,perfect and balanced binary tree
Difference between complete,ordered,full,strict,perfect and balanced binary treeDifference between complete,ordered,full,strict,perfect and balanced binary tree
Difference between complete,ordered,full,strict,perfect and balanced binary tree
 

Similar to 4a searching-more

Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms CourseHunt
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptxRaaviKapoor
 
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNratnapatil14
 
trees in data structure
trees in data structure trees in data structure
trees in data structure shameen khan
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graphRai University
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graphRai University
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdfssuser50179b
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 

Similar to 4a searching-more (20)

Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
 
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
 
Splay tree
Splay treeSplay tree
Splay tree
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Treesandgraphs
TreesandgraphsTreesandgraphs
Treesandgraphs
 
Trees
TreesTrees
Trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Splay tree
Splay treeSplay tree
Splay tree
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
 
Tree
TreeTree
Tree
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Binary tree
Binary treeBinary tree
Binary tree
 
Trees
TreesTrees
Trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 

Recently uploaded

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 

Recently uploaded (20)

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 

4a searching-more

  • 1. More Searching Dan Barrish-Flood 4a-Searching-More 1
  • 2. Running time of Dynamic Set Operations • we’ve seen that all our dynamic set operations can be done in Θ(h) time (worst-case) on a binary search tree of height h. • a binary search tree of n nodes has height Θ(lgn) • so all dynamic set operations take Θ(lgn) time on a binary search tree. • right? WRONG! • insert these numbers to form a binary search tree, in this order: 1, 2, 3, 4, 5 4a-Searching-More 2
  • 3. Binary Search Tree Random Insertion & Deletion • What is the average depth of a BST after n insertions of random values? • The root is equally likely to be the smallest, 2nd smallest, ..., largest, of the n values. So with equal probability, the two subtrees are of sizes 1 & n-2 (not n-1, don’t forget the root!) 2 & n-3 ... n-3 & 2 n-2 & 1 Wait til you see the next slide!... 4a-Searching-More 3
  • 4. Random Insertion/Deletion (Cont’d) • The analysis is identical to Quicksort! • The root key is like the pivot. • So n insertions take O(nlgn) on average, thus each insertion takes O(lgn), which is the tree height. 4a-Searching-More 4
  • 5. Balanced Search Trees • Careful, I may have used BST in the past for “binary search tree” not “balanced search tree”, I will try to avoid this confusion! • The general idea (there are exceptions) – do extra work during INSERT and DELETE to ensure the tree’s height is Θ(lgn) – the rest of the dynamic set operations are unchanged. • Two examples: – Red-Black Trees (CLRS ch. 13) • elegant definition • wicked hairy insert/delete – 2-3 Trees • simpler to understand • not true binary trees 4a-Searching-More 5
  • 6. a taste of Red-Black Trees • a Red-Black tree is a binary search tree where 2. the root is BLACK 3. each node is colored RED or BLACK 4. every leaf is black 5. each of a red node’s children are black 6. every path from a node to one of its descendant leaves contains the same number of black nodes • (a minor twist: we consider the NIL’s as leaves, and all the nodes with keys as internal nodes) • the height of a node (for trees in general) is the # of edges on the longest downward path from that node to a leaf. 4a-Searching-More 6
  • 8. RBT, each leaf (NIL) is black the tiny number next to a node is its “black- height” 4a-Searching-More 8
  • 9. Dynamic Set Operations as implemented with RBT’s It’s not that hard to prove (by induction) that the RBT properties imply: • a RBT with n internal nodes has height ≤ 2 lg(n+1) • don’t worry about the proof, but see CLRS p. 274 if you’re interested in the details • The height of a RBT is O(lgn), so all dynamic set operations run in O(lgn) time. • But wait! INSERT, DELETE may destroy the RBT property! But, it turns out, these two operations can indeed be supported in O(lgn) time, too. 4a-Searching-More 9
  • 10. More on tree-traversal, here is pre-order traversal (note this is not a binary tree) First “process” (e.g. print) the root (hence pre), then recursively process the root’s left subtree(s), then recursively process the root’s right subtree(s). For this tree, we get 1,2,3,...,9 4a-Searching-More 10
  • 11. More on tree traversal, here is post-order traversal (note this is not a binary tree) First recursively process the root’s left subtree(s), then recursively process the root’s right subtree(s), then lastly (hence post) “process” (e.g. print) the root. For this tree, we again get 1,2,3,...,9 4a-Searching-More 11
  • 12. 2-3 Trees • Another kind of Balanced Search Tree • What are the structural requirements: – every non-leaf (i.e. internal) node has exactly 2 or 3 children – all leaves are at the same level – here are three 2-3 trees (not showing keys!) 4a-Searching-More 12
  • 13. 2-3 Trees, more structural info • the “thinnest” 2-3 tree is a complete binary tree (see picture on prev. slide) • the “fattest” 2-3 tree is a complete 3-ary tree (again see prev. slide). • If the tree has height h, (recall all leaves are at the same level), the number of leaves, l, is between 2h and 3h. • A 2-3 tree of n nodes (total) has a height between log3n and log2n, and since log3n = log32 x log2n (why?), the height h is guaranteed to be within a small constant factor of log2n. 4a-Searching-More 13
  • 14. Where is the data in a 2-3 tree? All the records (keys, other satellite data) are in the leaves. The records are in sorted order. Each internal node has one or two guides: the greatest value in its leftmost one (or two) subtree(s). 4a-Searching-More 14
  • 15. Searching in a 2-3 Tree: Suppose I’m searching the above 2-3 tree (the one with the guides 8, 29 in the root) for the key 27. Start at the root. 27 ≤ 8? No, so forget about the root’s leftmost subtree. 27 ≤ 29? Yes, so we know our target (if it exists) is in the root’s middle subtree. Thus proceed to the node directly below the root? 27 ≤ 11? No. So forget about this node’s leftmost subtree. 27 ≤ 21? No, so we know our target (if it exists) must be in this node’s rightmost subtree. So follow our current node’s rightmost pointer down. We’re at a leaf (the good stuff is in here!), and we find our target 27 stored in this leaf. We use this method to always reach the “right” leaf, where we will either find our target in that leaf, or find that our target does not exist. SEARCH(2-3_tree, key) clearly, because of the height of the tree, runs in worst-case time O(lg n). I don’t have any good pseudo-code (maybe I’ll make or find some) 4a-Searching-More 15 for this routine.
  • 16. 2-3 Tree Insert (let’s insert 15 into the prev. tree) tree has increased in height! 4a-Searching-More 16
  • 17. 2-3 Tree Delete; let’s delete 10 from the 1st tree to get the 2nd one (one method). here we stole 11 from a sibling 4a-Searching-More 17
  • 18. 2-3 Tree Delete; let’s delete 10 from the 1st tree to get the 2nd one (other method). here we merged nodes 4a-Searching-More 18
  • 19. 2-3 Tree Delete; let’s delete 3 from the 1st tree to get the 3rd, there’s just one way). again, we stole from a sibling 4a-Searching-More 19
  • 20. 2-3 Tree Delete; let’s delete 5 tree loses a level! 4a-Searching-More 20