binary search trees - csa – iisc bangalore › ... › ue101 › binary-search-trees.pdf ·...
TRANSCRIPT
![Page 1: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/1.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Binary Search Trees
Deepak D’Souza
Department of Computer Science and AutomationIndian Institute of Science, Bangalore.
6 Nov, 2017
![Page 2: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/2.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Outline
1 Motivation
2 Binary Search Trees
3 Implementing a BST
4 Operations on BST
![Page 3: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/3.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
A motivating example
Google may want to store all taken email ids, and quickly tell anew user whether her choice of email id is available or not. Thereare around 500 million user ids.
Need to support both quick additions as well as membershipqueries.
Naive algorithm: Store entries in an array. What is the runningtime of the queries “add” and “is-member”?
![Page 4: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/4.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Term frequency in a document [Kernighan and Ritchie]
Given a text document, find the set of words that occur in thedocument and the count of the number of times each word occurs.
Now is the time for all good men to come to the aid oftheir party ...
Using an array to store words as they are encountered in the text,takes time quadratic in the size of (number of words in) thedoucment.
![Page 5: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/5.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Binary Search Trees
Can support add and is-member in O(log n) worst-case time.
Add and search is proportional to the height of the tree.
Uses idea of keeping the tree balanced, so that height is log n.
log(500 million) is about 29.
![Page 6: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/6.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Document search using BST
Now is the time for all good men to come to the aid oftheir party ...
Now
the
time
is
for men of
goodall to
comeaid
theirparty
Document indexing would take O(n · log n) time rather thanO(n2).
![Page 7: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/7.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Binary Search Tree
A binary search tree data-structure is abinary tree in which each node has a“key” value associated with it
The tree satisfies the “search treeproperty:”
The key values in the left subtree of anode are at most the key value of thenode, which in turn is at most the keyvalues of the nodes in the rightsubtree.
The height of a tree T , denoted h(T ),is the number of edges on the longestpath from root to leaf.
![Page 8: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/8.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Implementing a BST
struct node {
int key; // key value
struct node *left; // left child
struct node *right; // right child
struct node *p; // parent
}
struct bst {
struct node *root;
} *T; // BST T
8
10
14
key:
left:
right:
3
61
4 7 13
T:
![Page 9: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/9.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Exercise
Write a C program that creates theBST alongside.
void main() {
struct bst T;
struct node n1, n2, ..., n9;
...
}
![Page 10: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/10.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Operations supported by a BST
A binary search tree supports the following operations inworst-case time of O(log h(T )):
Search (retrieve a given key from the tree).
Insert (insert a given key value in the tree).
Delete (insert a given key value in the tree).
Additional operations include
Print-Keys (Print out keys in sorted order).
Min (Return minimum key value in the tree).
Max (Return maximum key value in the tree).
Successor (successor key value of a given key).
Predecessor (predecessor key value of a given key).
![Page 11: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/11.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Search – Iterative version
Given a pointer x to a node in a BST, and a key value k , return apointer to a node with key k if it exists in the subtree of x , elseNULL.
Search(x, k) {
while (x != NULL and x.key != k) {
if (k < x.key)
x := x.left;
else
x := x.right;
}
return x;
}
![Page 12: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/12.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Search – Recursive version
Given a pointer x to a node in a BST, and a key value k , return apointer to a node with key k if it exists in the subtree of x , elseNULL.
Search(x, k) { // x is ptr to a node
if (x = NULL or x.key = k)
return x;
if (k < x.key)
return Search(x.left, k);
else
return Search(x.right, k);
}
![Page 13: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/13.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Exercise
Write a function
struct node* minimum(struct node *x) {
...
}
which returns a pointer to a node with the smallest key value inthe tree rooted at x .
![Page 14: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/14.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Printing in ascending order
Print(T) {
Inorder-Print(T.root);
}
Inorder-Print(x) {
if (x != NULL) {
Inorder-Print(x.left);
print(x.key);
Inorder-Print(x.right);
}
}
![Page 15: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/15.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Insert
Insert(T, z) { // z is pointer to node to be inserted
x := T.root;
y := NULL;
while (x != NULL) {
y := x;
if (z.key < x.key)
x := x.left;
else
x := x.right;
}
if (y = NULL)
T.root := z;
elseif (z.key < y.key)
y.left := z;
else
y.right := z;
z.p := y;
}
![Page 16: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/16.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Successor
Given a node x , return (a pointer to) the node u such thatx .key ≤ u.key and u.key is the smallest such value.
Successor(x) { // x is the node whose succ is to be returned
if (x.right != NULL)
return Minimum(x.right);
y := x.p;
while (y != NULL and x = y.right) {
x := y;
y := y.p;
}
return y;
}
![Page 17: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/17.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Correctness of Successor
We assume that keys in the given tree are distinct.
Claim 1: If x has a non-empty right subtree, then minimum node yin this right subtree is the successor of x .Argue that:
If v is an ancestor of x , it cannot be successor.
If v is not an ancestor of x , consider the least commonancestor of x and v , say z , and argue that v cannot besuccessor of x .
r
y
xv
z
r
y
xv
z
r
y
x
v
![Page 18: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/18.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Correctness of Successor
Similarly:Claim 2: If x has an empty right subtree, then the smallestancestor u of x such that x lies in u’s left subtree, is the successorof x .Argue that:
If v is an ancestor of u, it cannot be successor.
If v is an ancestor of x but not of u, it cannot be successor.
If v is not an ancestor of x , consider the least commonancestor of x and u, say z , and argue that v cannot besuccessor of x .
![Page 19: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/19.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Delete operation: cases
(b)
(a)
NULL
NULL
NULL
NULL
(c)
(d)
lz
q q
l
r
r
z
q q
yz
q q
l
yz
q q
l r
y
x
rl
x
y
x
l x
![Page 20: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/20.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Delete operation: cases
NULL
(d)yz
q q
l r
y
x
rl
x
![Page 21: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/21.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Delete
Delete(T, z) { // z is pointer to node to be deleted
if (z.left = NULL)
Transplant(T, z, z.right);
elseif (z.right = NULL);
Transplant(T, z, z.left);
else {
y := Minimum(z.right);
if (y.p != z) {
// y not child of z
Transplant(T, y, y.right);
y.right = z.right;
(y.right).p = y
}
Transplant(T, z, y)
y.left = z.left;
(y.left).p = y;
}
}
Transplant(T, u, v) {
// transplant subtree at u
// by subtree at v
if (u.p = NULL)
T.root = v;
elseif (u = (u.p).left);
(u.p).left = v;
else
(u.p).right = v;
if (v != NULL)
v.p = u.p;
}
![Page 22: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/22.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST
Balancing BST’s
In general the BST may not be “balanced”, leading to heightthat could be O(n) in the worst case.
There are some popular schemes for maintaining the BST in abalanced form:
Red-Black trees: Nodes have either red or black colour, rootand leaves are black, red nodes have black children, and pathsfrom root to leaf have same number of black nodes.AVL trees: for each node, height of its left and right subtreesdiffer by at most 1.
![Page 23: Binary Search Trees - CSA – IISc Bangalore › ... › UE101 › binary-search-trees.pdf · 2019-07-16 · MotivationBinary Search TreesImplementing a BSTOperations on BST Binary](https://reader036.vdocuments.us/reader036/viewer/2022081406/5f11aa31b92d905d933eeb7e/html5/thumbnails/23.jpg)
Motivation Binary Search Trees Implementing a BST Operations on BST