2. Index
● Introduction
○ Tree
○ Terminology
○ Efficient Search
○ Application of Tree
● Tries
○ Search Problem 1
○ Solution: Creating and Searching in Trie
○ Application of Tries
● K-d Trees
○ Search Problem 2
○ Solution : Creating a kd Tree and range Searching in kD-Tree
○ Application of K-dTree
3. Introduction: Tree
1. Non Linear Data structure
2. Hierarchical Structure
3. Each node of tree has links to its siblings
5. Introduction:Efficient Search
Consider an array = [1,4,5,2,3,9,10]
● Linear Search takes time = (0)n
● Binary Search :
needs to be sorted((0)n logn) + (logn)
● Binary Search Tree : (0)n
● Balanced binary search tree (0) logn
Tree can reduce the search time efficiently.
6. Introduction: Application of Binary Trees
● Hierarchical Data Storage eg. File System
● Paging in Memory /Indexing in Books
● Compiler: Expression trees/ Parse trees
● Sorted data representation
● Indexing in Databases
7. Search Problem 1
How do we search efficiently a dictionary for a particular word ?
Given Dictionary: bear, bell, bid, bull, buy, sell, stock, stop
Search Word: Stop
Can we do faster than Binary Search tree Balanced Binary search tree : (0)log n
Here n is the size of dictionary
10. Tries : Create
insert(TrieNode root, const String key){
int level;
int index;
TrieNode current = root;
for (level = 0; level < key.length; level++)
{
index = CHAR_TO_INDEX(key[level]);
if (current->children[index])
current->children[index] =new TrieNode();
current = current->children[index];
}
// mark last node as leaf
temp->isLeaf = true;
}
11. Tries: Search
bool search(TrieNode root, String key){
int level;
int index;
struct TrieNode current = root;
for (level = 0; level < key.length; level++)
{
index = CHAR_TO_INDEX(key[level]);
if (!current->children[index])
return false;
current = current->children[index];
}
return (current != NULL && current->isLeaf);
}
14. Tries : Applications
● Auto complete functionality eg in Google search
● Phone Contacts
● Automatic Command Completion in linux terminal
● Spell Checkers in Word or gmail
● …. etc
15. Search Problem 2
1. From a list of Restaurants find all the restaurants where X lies between 30-40 && Y between
40-50 ?
Name X Y
R1 30 40
R2 5 25
R3 10 12
R4 50 30
R5 35 45
16. K-d Trees
1. A K-d Tree(also called as K-Dimensional Tree) is a binary search tree where
data in each node is a K-Dimensional point in space.
2. K is a dimension can be 2-d Tree, 3-d Tree etc.
3. Each level has cutting dimension
4. Cutting dimensions cycle as we go down the tree
5. Each node has a Point
17. K-d Trees: K-d Tree Operations
List of points : (30,40), (5,25), (10,12), (70,70), (50,30), (35,45)
Different type of Queries:
Search Query: search a point (10,12)
Range Query: Range Query in 2 D tree is like list all points where x>30 and y> 40
19. K-d Trees : Create
insert(Point x, KDNode t, int cd) {
if t == null t = new KDNode(x)
else if (x == t.data)
// error! duplicate
else if (x[cd] < t.data[cd])
t.left = insert(x, t.left, (cd+1) % DIM)
else
t.right = insert(x, t.right, (cd+1) % DIM)
return t
}
KDNode
{
// To store k dimensional point
int point[k];
Node *left, *right;
};
20. K-d Trees : Range Search
Search
X is in range 30-40
Y is in range 40-50
24. References
1. de la Briandais, René (1959). File searching using variable length keys. Proc. Western J.
Computer Conf. pp. 295–298.Cited by Brass.
2. Bentley, J. L. (1975). "Multidimensional binary search trees used for associative searching".
Communications of the ACM. 18 (9): 509. doi:10.1145/361002.361007.
3. https://link.springer.com/chapter/10.1007/978-3-642-72617-0_17
4. http://goanna.cs.rmit.edu.au/~jz/fulltext/acsc03sz.pdf
5. http://www.cs.cmu.edu/~ckingsf/bioinfo-lectures/kdtrees.pdf