### Lookup, Insert and Delete Operations on a Quad-tree

• Lookup a search key in a Quad-tree

• Lookup Algorithm:

• At each internal node, select the subtree based on the relationship of the search key and the key value stored in the internal node:

• Example: find the person with age = 50 and salary = 100

• Insert Algorithm

 ``` Insert( x1, x2, ..., xn ) { Find the leaf node L (block) that is used to store (x1, x2, ..., xn); if ( leaf node L (block) has enough space ) { insert (x1, x2, ..., xn) in L; return; } else { /* ====================================================== 1. Split leaf node L into 2n parts !!! 2. Add an internal node with search key for these 2n leaf nodes ====================================================== */ Let m1 = the median of the x1 values in L; Let m2 = the median of the x2 values in L; ... Let mn = the median of the xn values in L; Split the records in L into 2n blocks according m1, m2, ..., mn Replace L by an internal node I with seacrh key (m1, m2, ..., mn); Make the 2n blocks as child nodes of I; } } ```

Example: (n = 2)

• Insert the record (age=35, salary= 600) in the following quad-tree:

• Find the leaf node L:

• Split the overflow node at the mid values (age=35 and salary=400):

• Add an internal search node containing the mid search key values:

• Delete operation:

• Same comment on delete as in the kd-tree:

 The delete operation is very complex and require many block accesses

• Work-around::

• Instead of actually deleting the record:

 the system marks the record as deleted

• From time to time, the system rebuild the kd-tree from scratch to remove the deleted records.

• Comment: Quad-tree is just a simpler kd-tree

• We can always represent a Quad-tree using a kd-tree by replacing a quad-tree node by 3 (three) kd-tree nodes as follows: