![post-thumbnail](https://velog.velcdn.com/images/shs808/post/9983dd13-bd44-4288-bc49-264ae7edf5e9/image.jpg)
Binary Search Tree
![](https://velog.velcdn.com/images/shs808/post/3009b00e-7851-4a46-b911-f93e4240f6f1/image.png)
- A binary search tree is a tree in which the left side has a small value and the right side has a large value based on a specific node.
- The depth of the tree is log(n), so the search ends in O(log(n)) time in Big O notation.
![](https://velog.velcdn.com/images/shs808/post/88b0d04e-5e9d-4907-a233-b1ea20bb4af4/image.png)
K-D tree
- KD-Tree is a concept that extends Binary Search Tree, and uses a key consisting of k (k>=2) fields for operation.
- Ideal for indexing small, multidimensional point data.
![](https://velog.velcdn.com/images/shs808/post/93175176-69d5-4365-ae2f-ca0573dad2b4/image.png)
Algorithm
![](https://velog.velcdn.com/images/shs808/post/b7b8fc53-3e42-48fe-8217-2d18577545ea/image.png)
- insert (a) as root
![](https://velog.velcdn.com/images/shs808/post/ab986025-f23c-46a3-8a1a-87c7d804e75c/image.png)
- compare (b) x axis to (a) x axis and insert left side of left branch
![](https://velog.velcdn.com/images/shs808/post/265c8927-3584-47b3-8678-cc2e0a14bcf3/image.png)
- compare (c) x axis to (a) x axis and insert right side of left branch
![](https://velog.velcdn.com/images/shs808/post/60a2e124-0c97-44fe-abfd-12a10449352b/image.png)
- compare (d) x axis to (a) axis and then compare (d) y axis to (b) insert left side of left branch
![](https://velog.velcdn.com/images/shs808/post/fcf1bcec-6a41-4ceb-b9fa-31f5339e43ce/image.png)
Limitation
![](https://velog.velcdn.com/images/shs808/post/d2a6def6-a1e1-4b88-86d6-bf2e48ed77ac/image.png)
- Not a balanced tree
- Can be skewed according to data entry order and distribution
Insert Operation
- Basically, it is the same as insertion operation in Binary Search Tree.
- When a leaf node is encountered while going down the tree, it decides whether to insert into the left child or the right child based on the key value.
Deletion Operation
- In KD-Tree, one node can have up to two children, and the deletion method according to the number of children is as follows.
- # of Child = 0 - Just remove the target node to be removed.
(There is no child, so there is nothing to worry about.)
- # of Child = 2 - Among the nodes in the right subtree of the node to be removed,
Remove the node with the smallest Field value used by the node to be removed and put it in the position of the node to be removed.
- If the newly located node has a child, it recursively finds a replacement node for this node.
- # of Child = 1- If there is only the right child, from among the nodes in the right subtree, the node with the smallest field value of the node to be removed is removed and placed in the position of the node to be removed.
- If there is only a left child, from among the nodes in the left subtree, the node with the largest field value of the node to be removed is removed and placed in the position of the node to be removed.
![](https://velog.velcdn.com/images/shs808/post/dd7fe247-0399-468e-9ece-495671e58977/image.png)
1) Delete Node A
![](https://velog.velcdn.com/images/shs808/post/8a614c33-7f96-4feb-b941-f23e9375640c/image.png)
- Detect Substitution
![](https://velog.velcdn.com/images/shs808/post/4dc5ad08-a1cc-43ac-a9e0-080133364305/image.png)
- Remove A & Relocating C
![](https://velog.velcdn.com/images/shs808/post/a4f139c5-a659-49a4-8346-fe47ca5aeaa1/image.png)
- Remove C
![](https://velog.velcdn.com/images/shs808/post/83f6bee4-c502-4957-8fed-4ee93ee5bea3/image.png)
- Relocating L
NN search
![](https://velog.velcdn.com/images/shs808/post/cc16f44d-9e64-4e5d-8b61-847b71a0cc3c/image.png)
![](https://velog.velcdn.com/images/shs808/post/f0229684-c4c2-4cff-831b-5b7ba6245c8b/image.png)
![](https://velog.velcdn.com/images/shs808/post/de36718a-7e57-43a9-a9d0-3954194f317b/image.png)
![](https://velog.velcdn.com/images/shs808/post/f9dc472d-1a9b-4b8f-970a-efcc80133291/image.png)
![](https://velog.velcdn.com/images/shs808/post/dae01380-c4ec-4708-9795-871f4e7b85fc/image.png)
![](https://velog.velcdn.com/images/shs808/post/c8249eb3-628a-4662-bd9e-0f6fa7ee2547/image.png)
![](https://velog.velcdn.com/images/shs808/post/650b8d6f-7c35-441c-95d4-d61f90f7c131/image.png)
![](https://velog.velcdn.com/images/shs808/post/f122aa7d-7ea7-42f5-a025-3dc76cc3a58f/image.png)
![](https://velog.velcdn.com/images/shs808/post/d272eefc-cd9b-4c29-86c9-c85c3cbb417c/image.png)