Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
35 views5 pages

KD Tree and Trie Data Structures

Uploaded by

Amol Sawant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views5 pages

KD Tree and Trie Data Structures

Uploaded by

Amol Sawant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

KD Tree:-

A KD-tree is a generalization of a binary search tree that stores points in k-dimensional space. Every
non-leaf node can be thought of as implicitly generating a splitting hyperplane that divides the space
into two parts, known as half-spaces. Points to the left of this hyperplane are represented by the left
subtree of that node and points to the right of the hyperplane are represented by the right subtree. KD-
tree could be used to store a collection of points in the Cartesian plane, in three-dimensional space, etc.
KD-tree could be used to store multidimensional data, for example, by representing the data as an
ordered tuple, perhaps (height, weight, blood pressure, cholesterol). However, a KD tree cannot be used
to store collections of other data types, such as strings. Also note that while it's possible to build a KD-
tree to hold data of any dimension, all of the data stored in a KD-tree must have the same dimension.
That is, you can't store points in two-dimensional space in the same KD-tree as points in four-
dimensional space.
Typical algorithms construct KD-trees by partitioning point sets. Each node in the tree is defined by a
plane through one of the dimensions that partitions the set of points into left/right (or up/down) sets,
each with half the points of the parent node. These children are again partitioned into equal halves,
using planes through a different dimension. Partitioning stops after log n levels, with each point in its
own leaf cell. Alternate kd-tree construction algorithms insert points incrementally and divide the
appropriate cell, although such trees can become seriously unbalanced.
Creation of a 2-D Tree:
Consider following points in a 2-D plane:
(3, 6), (17, 15), (13, 15), (6, 12), (9, 1), (2, 7), (10, 19)
1. Insert (3, 6): Since tree is empty, make it the root node.
2. Insert (17, 15): Compare it with root node point. Since root node is X-aligned, the X-coordinate
value will be compared to determine if it lies in the rightsubtree or in the right subtree. This point
will be Y-aligned.
3. Insert (13, 15): X-value of this point is greater than X-value of point in root node. So, this will lie
in the right subtree of (3, 6). Again Compare Y-value of this point with the Y-value of point (17,
15) (Why?). Since, they are equal, this point will lie in the right subtree of (17, 15). This point
will be X-aligned.
4. Insert (6, 12): X-value of this point is greater than X-value of point in root node. So, this will lie
in the right subtree of (3, 6). Again Compare Y-value of this point with the Y-value of point (17,
15) (Why?). Since, 12 < 15, this point will lie in the left subtree of (17, 15). This point will be X-
aligned.
5. Insert (9, 1):Similarly, this point will lie in the right of (6, 12).
6. Insert (2, 7):Similarly, this point will lie in the left of (3, 6).
7. Insert (10, 19): Similarly, this point will lie in the left of (13, 15).
k-d tree decomposition for the point set (2,3), (5,4), (9,6), (4,7), (8,1), (7,2).

KD-Tree representation
KD Tree Deletion:-
Like Binary Search Tree Delete, we recursively traverse down and search for the point to be deleted.
Below are steps are followed for every node visited.
If current node contains the point to be deleted
a. If node to be deleted is a leaf node, simply delete it (Same as BST Delete)
b. If node to be deleted has right child as not NULL (Different from BST)
i. Find minimum of current node’s dimension in right subtree.
ii. Replace the node with above found minimum and recursively delete minimum in right
subtree.
c. Else If node to be deleted has left child as not NULL (Different from BST)
. Find minimum of current node’s dimension in left subtree.
i. Replace the node with above found minimum and recursively delete minimum in left
subtree.
ii. Make new left subtree as right child of current node.

Example of Delete:
Delete (30, 40): Since right child is not NULL and dimension of node is x, we find the node with
minimum x value in right child. The node is (35, 45), we replace (30, 40) with (35, 45) and delete (35,
45).

Delete (70, 70): Dimension of node is y. Since right child is NULL, we find the node with minimum y
value in left child. The node is (50, 30), we replace (70, 70) with (50, 30) and recursively delete (50,
30) in left subtree. Finally we make the modified left subtree as right subtree of (50, 30).
The complexity of KD tree is guaranteed log2 n depth where n is the number of points in the set. –
Traditionally, k-d trees store points in d dimensional space which are equivalent to vectors in d-
dimensional space.
KD Tree is used to store spatial data for nearest neighbour search, Range queries, and Fast look-up.
Trie
Trie is a data structure to maintain the collection of strings. Trie is an efficient information reTrieval
data structure. Using Trie, search complexities can be brought to optimal limit (key length). If we store
keys in binary search tree, a well balanced BST will need time proportional to M * log N, where M is
maximum string length and N is number of keys in tree. Using Trie, we can search the key in O(M)
time.
Create trie for given set of words { bear, bull, bid, bell, buy, sell, stock, stop}

Every node of Trie consists of multiple branches. Each branch represents a possible character of keys.
We need to mark the last node of every key as end of word node. A Trie node field isEndOfWord is
used to distinguish the node as end of word node. A simple structure to represent nodes of the English
alphabet can be as following,
// Trie node
struct TrieNode
{
struct TrieNode *children[ALPHABET_SIZE];
// isEndOfWord is true if the node
// represents end of a word
bool isEndOfWord;
};

Inserting a key into Trie is a simple approach. Every character of the input key is inserted as an
individual Trie node. Note that the children is an array of pointers (or references) to next level trie
nodes. The key character acts as an index into the array children. If the input key is new or an extension
of the existing key, we need to construct non-existing nodes of the key, and mark end of the word for
the last node. If the input key is a prefix of the existing key in Trie, we simply mark the last node of the
key as the end of a word. The key length determines Trie depth.
Searching for a key is similar to insert operation, however, we only compare the characters and move
down. The search can terminate due to the end of a string or lack of key in the trie. In the former case,
if the isEndofWord field of the last node is true, then the key exists in the trie. In the second case, the
search terminates without examining all the characters of the key, since the key is not present in the trie.

Trie Delete

During delete operation we delete the key in bottom up manner using recursion. The following are
possible conditions when deleting key from trie,
1. Key may not be there in trie. Delete operation should not modify trie.
2. Key present as unique key (no part of key contains another key (prefix), nor the key itself is prefix
of another key in trie). Delete all the nodes.
3. Key is prefix key of another long key in trie. Unmark the leaf node.
4. Key present in trie, having atleast one other key as prefix key. Delete nodes from end of key until
first leaf node of longest prefix key.

Trie data structure is normally kept in main memory, but the occurrence list may be huge so it will be
stored on the disk. It is efficiently used in information retrieval for indexing web pages. Trie is used in
pattern matching

You might also like