binary search tree - national tsing hua universitywkhon/algo08-tutorials/tutorial-bst.pdf ·...
TRANSCRIPT
![Page 1: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/1.jpg)
Binary Search Tree
Speaker 邱聖元
![Page 2: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/2.jpg)
Outline
IntroductionOperations Conclusion
![Page 3: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/3.jpg)
Introduction
What’s a binary search tree?– It’s a binary tree !– For each node in a BST, the left subtree is
smaller than it; and the right subtree isgreater than it.
![Page 4: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/4.jpg)
Introduction
This is a simple BST
10
7
96
13
12 14
![Page 5: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/5.jpg)
Introduction
Is this a BST ?
25
19
228
27
24 31
NO! Because 24 isless than 25
![Page 6: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/6.jpg)
Introduction
Node structure
RightLeft
Key
Parent
![Page 7: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/7.jpg)
Operations
There are 3 common operations– QUERY– INSERT– DELETE
![Page 8: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/8.jpg)
Operation - Query
The QUERY operation can be further spitinto– Search– Max/Min– Successor/Predecessor
![Page 9: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/9.jpg)
Operation - Search
Search(T,k)– search the BST T for a value k
![Page 10: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/10.jpg)
Operation - Search
10
7
96
13
12 14
K=12
?
greatersmaller
![Page 11: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/11.jpg)
Operation - Search
10
7
96
13
12 14
K=12
?
greatersmaller
![Page 12: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/12.jpg)
Operation - Search
10
7
96
13
12 14
K=12
![Page 13: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/13.jpg)
Operation - Search
Search operation takes time O(h), where his the height of a BST
![Page 14: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/14.jpg)
Operation –Min/Max
For Min, we simply follow the left pointeruntil we find a null node
Why? Because if it’s not the minimum node,then the real min node must reside atsome node’s right subtree. By the propertyof BST, it’s a contradiction
Similar for Max Time complexity: O(h)
![Page 15: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/15.jpg)
Operation Predecessor/Successor
Successor(x)– If we sort all elements in a BST to a sequence,
return the element just after x– Time complexity: O(h)
![Page 16: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/16.jpg)
Operation Predecessor/Successor
Find Successor
if Right(x) exists, // Step 1
then return Min( Right(x) ) ;else // Step 2
Find the first ancestor of x whose leftsubtree contains x ;
![Page 17: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/17.jpg)
Operation Predecessor/Successor
Step1: X
Min
![Page 18: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/18.jpg)
Operation Predecessor/Successor
Finding theancestor whoseleft subtreecontains X
X
Step2:
![Page 19: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/19.jpg)
Operation - Insert
Insert(T,z)– Insert a node with KEY=z into BST T– Time complexity: O(h)
![Page 20: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/20.jpg)
Operation - Insert
Step1: if the tree is empty,then Root(T)=z
Step2: Pretending we are searching for zin BST T, until we meet a null node
Step3: Insert z
![Page 21: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/21.jpg)
Operation - Insert
The light nodesare comparedwith k
30
17 52
2412
20 26
44 55
26
![Page 22: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/22.jpg)
Operation - Delete
Delete(T,z)– Delete a node with key=z from BST T– Time complexity: O(h)
![Page 23: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/23.jpg)
Operation - Delete
Case 1: z has no child
55
32
25 48
57
65
![Page 24: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/24.jpg)
Operation - Delete
55
32
25 48
57
65
We can simplyremove it fromthe tree
![Page 25: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/25.jpg)
Operation - Delete
Case 2: z has one child
55
32
25 48
57
65
58 78
![Page 26: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/26.jpg)
Operation - Delete
55
32
25 48
57
65
After removingit, connect it’ssubtree to it’sparent node
58 78
![Page 27: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/27.jpg)
Operation - Delete
Case 3: z has two child
55
32
25 48
61
6559
42
46
z
![Page 28: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/28.jpg)
Operation - Delete
55
32
25 48
61
6559
42successor
Find it’s successor
46
z
![Page 29: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/29.jpg)
Operation - Delete
55
32
25 48
61
6559
Pull out successor,and connect thetree with it’s child
46
42
What if successorhas two children?
z
successor
![Page 30: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/30.jpg)
Operation - Delete
What if the successor has two nodes?Not possible ! Because if it has two nodes,
at least one of them is less than it, then inthe process of finding successor, we won'tpick it !
![Page 31: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/31.jpg)
Operation - Delete
55
32
25 48
61
6559
42
Replace the keywith it’s successor
46
zsuccessor
![Page 32: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/32.jpg)
Operation - Delete
![Page 33: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/33.jpg)
Operation - Delete
![Page 34: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/34.jpg)
Operation - Delete
The original sequence is:
After deleting 32
25 32 42 46 48 55 59 61 65
25 42 46 48 55 59 61 65
![Page 35: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/35.jpg)
Conclusion
If we have a sorted sequence, and we wantto design a data structure for it
Array? Or BST?
![Page 36: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/36.jpg)
Conclusion
The Search time:
O(log n)Array
O(h)BST
![Page 37: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/37.jpg)
Conclusion
We already know that n is fixed, but hdiffers from how we insert thoseelements !
n
A skew binarysearch tree
![Page 38: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For](https://reader035.vdocuments.site/reader035/viewer/2022081323/5f091b9e7e708231d4254468/html5/thumbnails/38.jpg)
Conclusion
So why we still need BST?
Easier insertion/deletionAnd with some optimization, we can avoid
the worst case !