Search Trees - Motivation
description
Transcript of Search Trees - Motivation
![Page 1: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/1.jpg)
1
Search Trees - Motivation• Assume you would like to store several (key, value) pairs in a data structure that
would support the following operations efficiently
– Insert(key, value)– Delete(key, value)– Find(key) – Min()– Max()
• What are your alternatives?– Use an Array– Use a Linked List
![Page 2: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/2.jpg)
2
Search Trees - MotivationExample: Store the following keys: 3, 9, 1, 7, 4
3 9 1 7 4head
3 9 1 7 4
0 1 2 3 4 5 N-1
A
Can we make Find/Insert/Delete all O(logN)?
Operation
Find (Search)
Insert
Delete
UnsortedArray
O(N)
O(1)
O(1)
SortedArray
O(logN)
O(N)
O(N)
UnsortedList
O(N)
O(1)
O(1)
SortedList
O(N)
O(N)
O(1)
![Page 3: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/3.jpg)
3
Search Trees for Efficient Search• Idea: Organize the data in a search tree
structure that supports efficient search operation
1. Binary search tree (BST)2. AVL Tree3. Splay Tree4. Red-Black Tree5. B Tree and B+ Tree
![Page 4: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/4.jpg)
4
Binary Search Trees
5
3 7
8
Root
2 4
<5 >5
LSTRST
• A Binary Search Tree (BST) is a binary tree in which the value in every node is:> all values in the node’s left subtree< all values in the node’s right subtree
2
Root
3
7
85
4
>2
RST
![Page 5: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/5.jpg)
5
BST ADT Declarations
left key right
xstruct BSTNode {
BSTNode left;
int key;
BSTNode right;
};
BST Node
3
2 4
9
/* BST ADT */
class BST {
private:
BSTNode root;
public:
BST(){root=null;}
void Insert(int key);
void Delete(int key);
BSTNode Find(int key);
BSTNode Min();
BSTNode Max();
};
7
![Page 6: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/6.jpg)
6
BST Operations - Find• Find the node containing the key and
return a pointer to this node
K
LST RST
<K >K
1. Start at the root2. If (key == root.key) return root;3. If (key < root.key) Search LST4. Otherwise Search RST
root
15
root
<15 >15
Search forKey=13
![Page 7: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/7.jpg)
7
BST Operations - Find
BSTNode DoFind(BSTNode root,
int key){
if (root == null) return null;
if (key == root.key)
return root;
else if (key < root.key)
return DoFind(root.left, key);
else /* key > root.key */
return DoFind(root.right, key);
} //end-DoFind
• Nodes visited during a search for 13 are colored with “blue”
• Notice that the running time of the algorithm is O(d), where d is the depth of the tree
Root
15
6 18
303 7
2 4 13
9
Search forKey=13
BSTNode Find(int key){
return DoFind(root, key);
} //end-Find
![Page 8: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/8.jpg)
8
Iterative BST Find• The same algorithm can be written iteratively
by “unrolling” the recursion into a while loop
• Iterative version is more efficient than the recursive version
BSTNode Find(int key){
BSTNode p = root;
while (p != null){
if (key == p.key) return p;
else if (key < p.key) p = p.left;
else /* key > p.key */ p = p.right;
} /* end-while */
return null;
} //end-Find
![Page 9: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/9.jpg)
9
BST Operations - Min• Returns a pointer to the node that contains
the minimum element in the tree– Notice that the node with the minimum element
can be found by following left child pointers from the root until a NULL is encountered
BSTNode Min(){ if (root == null) return null;
BSTNode p = root; while (p.left != null){ p = p.left; } //end-while
return p;} //end-Min
Root
15
6 18
303 7
2 4 13
9
![Page 10: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/10.jpg)
10
BST Operations - Max• Returns a pointer to the node that contains
the maximum element in the tree– Notice that the node with the maximum element
can be found by following right child pointers from the root until a NULL is encountered
Root
15
6 18
303 7
2 4 13
9
BSTNode Max(){ if (root == null) return null;
BSTNode p = root; while (p.right != null){ p = p.right; } //end-while
return p;} //end-Max
![Page 11: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/11.jpg)
11
BST Operations – Insert(int key)• Create a new node “z”
and initialize it with the key to insert• E.g.: Insert 14
• Then, begin at the root and trace a path down the tree as if we are searching for the node that contains the key
• The new node must be a child of the node where we stop the search
Root
15
6 18
303 7
2 4 13
9
14
z
Before Insertion
NULL 14
z
Node “z” to be insertedz->key = 14
NULL
14
After Insertion
![Page 12: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/12.jpg)
12
BST Operations – Insert(int key)void Insert(int key){
BSTNode pp = null; /* pp is the parent of p */
BSTNode p = root; /* Start at the root and go down */
while (p != null){
pp = p;
if (key == p.key) return; /* Already exists */
else if (key < p.key) p = p.left;
else /* key > p.key */ p = p.right;
} /* end-while */
BSTNode z = new BSTNode(); /* New node to store the key */
z.key = key; z.left = z.right = null;
if (root == null) root = z; /* Inserting into empty tree */
else if (key < pp.key) pp.left = z;
else pp.right = z;
} //end-Insert
![Page 13: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/13.jpg)
13
BST Operations – Delete(int key)
• Delete is a bit trickier. 3 cases exist1. Node to be deleted has
no children (leaf node)– Delete 9
2. Node to be deleted has a single child– Delete 7
3. Node to be deleted has 2 children– Delete 6
Root
15
6 18
303 7
2 4 13
9 14
![Page 14: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/14.jpg)
14
Deletion: Case 1 – Deleting a leaf Node
Deleting 9: Simply remove the node and adjust the pointers
Root
15
6 18
303 7
2 4 13
9
Root
15
6 18
303 7
2 4 13
After 9 is deleted
![Page 15: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/15.jpg)
15
Deletion: Case 2 – A node with one child
Deleting 7: “Splice out” the nodeBy making a link between
its child and its parent
Root
15
6 18
303 7
2 4 13
9
Root
15
6 18
303 13
2 4 9
After 7 is deleted
![Page 16: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/16.jpg)
16
Deletion: Case 3 – Node with 2 children
Deleting 6: “Splice out” 6’s successor 7,which has no left child, and
replace the contents of 6 with the contents of the successor 7
Root
17
6 18
303 14
2 4 1610
7 13
8After 6 is deleted
Root
17
7 18
303 14
2 4 1610
8 13
Note: Instead of z’s successor,
we could have spliced out z’s predecessor
![Page 17: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/17.jpg)
17
Sorting by inorder traversal of a BST
5
3 7
8
Root
2 4
• BST property allows us to print out all the keys in a BST in sorted order by an inorder traversal
Inorder traversal results2 3 4 5 7 8
• Correctness of this claim follows by induction in BST property
![Page 18: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/18.jpg)
18
Proof of the Claim by Induction• Base: One node 5 Sorted• Induction Hypothesis: Assume that the claim is true for all
tree with < n nodes.• Claim Proof: Consider the following tree with n nodes
1. Recall Inorder Traversal: LST – R – RST2. LST is sorted by the Induction hypothesis since it has < n
nodes3. RST is sorted by the Induction hypothesis since it has < n
nodes4. All values in LST < R by the BST property5. All values in RST > R by the property6. This completes the proof.
R
Root
RST < n nodes
LST < n nodes
![Page 19: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/19.jpg)
19
Handling Duplicates in BSTs
5
3 7
8
Root
2 4
• Handling Duplicates:– Increment a counter stored in item’s node
• Or– Use a linked list at item’s node
2
1 4
3 2 6
5
3 7
Root
![Page 20: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/20.jpg)
20
Threaded BSTs
5
3 7
8
root
2 4
• A BST is threaded if– all right child pointers, that would normally be null,
point to the inorder successor of the node– all left child pointers, that would normally be null,
point to the inorder predecessor of the node
NULL NULL
![Page 21: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/21.jpg)
21
Threaded BSTs - More
5
3 7
8
root
2 4
• A threaded BST makes it possible – to traverse the values in the BST via a linear
traversal (iterative) that is more rapid than a recursive inorder traversal
– to find the predecessor or successor of a node easily
NULL NULL
![Page 22: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/22.jpg)
22
Laziness in Data StructuresA “lazy” operation is one that puts off work as much aspossible in the hope that a future operation will make thecurrent operation unnecessary
![Page 23: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/23.jpg)
23
Lazy Deletion• Idea: Mark node as deleted; no need to reorganize
tree– Skip marked nodes during Find or Insert– Reorganize tree only when number of marked nodes
exceeds a percentage of real nodes (e.g. 50%)– Constant time penalty only due to marked nodes – depth
increases only by a constant amount if 50% are marked undeleted nodes (N nodes max N/2 marked)
– Modify Insert to make use of marked nodes whenever possible e.g. when deleted value is re-inserted
• Gain:– Makes deletion more efficient (Consider deleting the root)– Reinsertion of a key does not require reallocation of
space
• Can also use lazy deletion for Linked Lists
![Page 24: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/24.jpg)
24
Application of BSTs (1)• BST is used as “Map” a.k.a. “Dictionary”,
i.e., a “Look-up” table– That is, BST maintains (key, value) pairs
– E.g.: Academic records systems:• Given SSN, return student record (SSN, StudentRecord)
– E.g.: City Information System• Given zip code, return city/state (zip, city/state)
– E.g.: Telephone Directories• Given name, return address/phone (name,
Address/Phone)• Can use dictionary order for strings – lexicographical
order
![Page 25: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/25.jpg)
25
Application of BSTs (2)• BST is used as “Map” a.k.a. “Dictionary”,
i.e., a “Look-up” table– E.g.: Dictionary
• Given a word, return its meaning (word, meaning)
– E.g.: Information Retrieval Systems• Given a word, show where it occurs in a document
(word, document/line)
![Page 26: Search Trees - Motivation](https://reader036.fdocuments.us/reader036/viewer/2022062315/56815152550346895dbf7749/html5/thumbnails/26.jpg)
26
Taxonomy of BSTs• O(d) search, FindMin, FindMax, Insert, Delete• BUT depth “d” depends upon the order of
insertion/deletion• Ex: Insert the numbers 1 2 3 4 5 6 in this order.
The resulting tree will degenerate to a linked list-> All operations will take O(n)!
1
2
3
4
5
6
root • Can we do better? Can we guarantee an upper bound on the height of the tree?
1. AVL-trees2. Splay trees3. Red-Black trees4. B trees, B+ trees