We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
e ere
search ree where data in each nod
Ing(details below) data structure for
{es the space into two parts, called as half-spaces.
ts to the right of the space
space are represented by the left subtree of that node and po!
the right subtree’ We vil socn be explaining the concept on how the space is divided and tree
ke of simplicity let us understand a 2-D Tree with an example.
root would have an x-aligned plane, the root's children would both have y-aligned planes, the root's
andchildren would all have x-aligned planes, and the root's great-grandchildren would all have y-aligned planes
d soon.
Generalization:
Let us number the planes as 0, 1, 2, ...(K — 1). From the above example, it is quite clear that a point (node) at
depth D will have A aligned plane where A is calculated as:
A=D mod Kk
How to determine if a point will lie in the left subtree or in right subtree?
If the root node is aligned in plane, then the left subtree will contain all points whose coordinates in that plane are
‘smaller than that of root node. Similarly, the right subtree will contain all points whose coordinates in that plane are
greater-equal to that of root node.
Creation of a 2-D Tree:xConsider following points in a 2-D plane:
(3, 6), (17, 15), (13, 1), (6, 12), (9, 1), (2, 7), (10, 19)
4. 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 right subtree or in the left 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 ie n 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, 18) (Why?). Since, 12 < 15,
this point will lie in the left subtree of (17, 1). This point will be X-aligned.
Insert (9, 1):Similarly, this point will lie in the right of (6, 12).
Insert (2, 7):Similarly, this point will lie in the left of (3, 6)
Insert (10, 19): Similarly, this point will lie in the left of (13, 15)
Now
aa ——| 3,6.
17,15 }——- y
27
6,12
10,19
9,1
ned?
in the X-Y plane as follows:Deena” 16 | ig
Ine X = 3 into two parts horizontally.
tGees 6 8 2 18 18
Point (6, 12) will divide
Draw line X = 6 to the ri
20
the space below line Y = 15 and to the right of line X = 3 into two parts.
ght of line X = 3 and below line Y = 15.
+
6
—T
10eno zt
1) will divide the space between ines X = 9, X= 6 and Y = 18 into two parts.
line ¥ = 1 between lines X = 3 and X = 13.
20
Ey
=
Point (10, 18) vill divide the space tothe ight of line x = 3 and ab =
Fe te ye ato te git fine X= 3 and above ine Y= 15. ln aa
weensional Tree | Set 2 (Find Minimum)
strongly recommend to refer below post as a prerequisite of ths,
i
{hn this post find minimum is discussed. The of dimension. This is especially
eee cate en: eration is to find minimum in the given
For example, consider below KD Tree, if given dimension is x, then output should be § an
y, then output should be 12.
dif given dimensions is.
In KD tree, points are divided dimension by dimension. For example, root divides keys by dimension 0, level next
to root divides by dimension 1, next level by dimension 2 if k is more then 2 (else by dimension 0), and so on.
To find minimum we traverse nodes starting from root. If dimension of current level is same as given
dimension, then required minimum fies on left side if there is left child. This is same as Binary Search Tree
Minimum,
‘Above is simple, what to do when current level's dimension is different. When dimension of current level is
different, minimum may be either in left subtree or right subtree or current node may also be minimum. So
we take minimum of three and return. This is different from Binary Search tree
K Dimensional Tree | Set 3 (Delete)
‘The operation is to delete a given point from K D Tree.
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.
1) If current node contains the point to be deleted
41. If node to be deleted is a leaf node, simply delete it (Same as BST Delete)
2. If node to be deleted has right child as not NULL (Different from BST)
4. Find minimum of current node's dimension in right subtree.
2. Replace the node with above found minimum and recursively delete minimum in right subtree.
3. Else If node to be deleted has left child as not NULL (Different from BST)
4, Find minimum of current node's dimension in left subtree.
2. Replace the node with above found minimum and recursively delete minimum in left subtree.
3, Make new left subtree as right child of current node.
2) if current doesn’t contain the point to be deleted
4, {fnode to be deleted is smaller than current node on current dimension, recur for left subtree.
2. Else recur for right subtree.
Why 4b and 1.¢ are different from BST?
In BST delete, if a node's left child is empty and right is not empty, we replace the node with right child. In K D
Tree, doing this would violate the KD tree property as dimension of right child of node is different from node's
dimension. For example, if node divides point by x axis values. then its children divide by y axis, so we can't
simply replace node with right child. Same is true for the case when right child is not empty and left child is empty,
Why Ss fr max in left subtree and recur for max like 1.b?
Doing this vi ie property that all equal values are in right subtree. For if i
Bee oss fe Dro perty I ig example, if we delete (!0, 10) in below
Way (Equal key in left subtree after deletion)
iz (5, 6) (4, 10)Right way (Equal key in right subtree after deletion)
(5, 6) (4, 10)
/ Delete(5, 6) i
(4, 18) => (4, 20)
is
5 (4, 20)
Example of Delete:
Delete (30, 40): Since right child is not NULL and dimension of node is x, we find the node wi
¢ i i jth minimum x value in
right child. The node is (35, 45), we replace (30, 40) with (35, 45) and delete (30, 40)
: 7 ==) =)
. CS] see Cc
-_o G2 Ey
: =)
Delete (70, 70): Dimension of node is y. Since right child is NULL, we find the node with minimum y value it left
ote (re nioge is (50, 30), we replace (70, 70) with (50, 30) and recursively delete (50, 30) in left subtree. Finally
Wwe make the modified left subtree as right subtree of (50, 30)