Professional Documents
Culture Documents
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-
mentioned properties −
• The value of the key of the left sub-tree is less than the value of its parent (root)
node's key.
• The value of the key of the right sub-tree is greater than or equal to the value
of its parent (root) node's key.
Representation
BST is a collection of nodes arranged in a way where they maintain BST properties.
Each node has a key and an associated value. While searching, the desired key is
compared to the keys in BST and if found, the associated value is retrieved.
Following is a pictorial representation of BST −
We observe that the root node key (27) has all less-valued keys on the left sub-tree
and the higher valued keys on the right sub-tree.
Basic Operations
Following are the basic operations of a tree −
• Search − Searches an element in a tree.
• Insert − Inserts an element in a tree.
• Pre-order Traversal − Traverses a tree in a pre-order manner.
• In-order Traversal − Traverses a tree in an in-order manner.
• Post-order Traversal − Traverses a tree in a post-order manner.
K Dimensional Tree
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 K
If the root node is aligned in planeA, 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.
2. Point (2, 7) will divide the space to the left of line X = 3 into two
parts horizontally.
Draw line Y = 7 to the left of line X = 3.
3. Point (17, 15) will divide the space to the right of line X = 3 into two
parts horizontally.
Draw line Y = 15 to the right of line X = 3.
• Point (6, 12) will divide the space below line Y = 15 and to the right
of line X = 3 into two parts.
Draw line X = 6 to the right of line X = 3 and below line Y = 15.
• Point (13, 15) will divide the space below line Y = 15 and to the
right of line X = 6 into two parts.
Draw line X = 13 to the right of line X = 6 and below line Y = 15.
• Point (10, 19) will divide the space to the right of line X = 3 and
above line Y = 15 into two parts.
Draw line Y = 19 to the right of line X = 3 and above line Y = 15.