|
| 1 | +# Binary Search Tree |
| 2 | + |
| 3 | +## Overview |
| 4 | +A Binary Search Tree (BST) is a tree-based data structure in which each node has at most two children, referred to as |
| 5 | +the left child and the right child. Each node in a BST contains a unique key and an associated value. The tree is |
| 6 | +structured so that, for any given node: |
| 7 | + |
| 8 | +1. The left subtree contains nodes with keys less than the node's key. |
| 9 | +2. The right subtree contains nodes with keys greater than the node's key. |
| 10 | + |
| 11 | +This property makes BSTs efficient for operations like searching, as the average time complexity for many operations is |
| 12 | +proportional to the tree's height. |
| 13 | + |
| 14 | +Note: in the following explanation a "smaller" node refers to a node with a smaller key and a "larger" node refers to a |
| 15 | +node with a larger key. |
| 16 | + |
| 17 | +## Implementation |
| 18 | +### BinarySearchTree Class |
| 19 | +The BinarySearchTree class is a generic implementation of a BST. It supports a variety of operations that allow |
| 20 | +interaction with the tree: |
| 21 | + |
| 22 | +- root(): Retrieve the root node of the tree. |
| 23 | +- insert(T key, V value): Insert a key-value pair into the tree. |
| 24 | +- delete(T key): Remove a key and its associated value from the tree. |
| 25 | +- search(T key): Find a node with a specified key. |
| 26 | +- predecessor(T key): Find the key of the predecessor of a specified key. |
| 27 | +- successor(T key): Find the key of the successor of a specified key. |
| 28 | +- searchMin(): Find the node with the minimum key in the tree. |
| 29 | +- searchMax(): Find the node with the maximum key in the tree. |
| 30 | +- getInorder(): Return an in-order traversal of the tree. |
| 31 | +- getPreorder(): Return a pre-order traversal of the tree. |
| 32 | +- getPostorder(): Return a post-order traversal of the tree. |
| 33 | +- getLevelorder(): Return a level-order traversal of the tree. |
| 34 | + |
| 35 | +We will expand on the delete implementation due to its relative complexity. |
| 36 | + |
| 37 | +#### Delete Implementation Details |
| 38 | +The delete operation is split into three different cases - when the node to be deleted has no children, one child or |
| 39 | +two children. |
| 40 | + |
| 41 | +**No children:** Simply delete the node. |
| 42 | + |
| 43 | +**One child:** Reassign the parent attribute of the child to the parent of the node to be deleted. This will not violate |
| 44 | +the binary search tree property as the right child will definitely be smaller than the parent of the deleted node. |
| 45 | + |
| 46 | +**Two children:** Replace the deleted node with its successor. This works because the binary search tree property is |
| 47 | +maintained: |
| 48 | +1. the entire left subtree will definitely be smaller than the successor as the successor is larger than the deleted |
| 49 | +node |
| 50 | +2. the entire right subtree will definitely be larger than the successor as the successor will be the smallest node in |
| 51 | +the right subtree |
| 52 | + |
| 53 | +### Node |
| 54 | +The Node class represents the nodes within the BinarySearchTree. Each Node instance contains: |
| 55 | + |
| 56 | +- key: The unique key associated with the node. |
| 57 | +- value: The value associated with the key. |
| 58 | +- left: Reference to the left child. |
| 59 | +- right: Reference to the right child. |
| 60 | +- parent: Reference to the parent node. |
| 61 | + |
| 62 | +## Complexity Analysis |
| 63 | +**Time Complexity:** For a balanced tree, most operations (insert, delete, search) can be performed in O(log n) time, |
| 64 | +except tree traversal operations which can be performed in O(n) time. However, in the worst case (an unbalanced tree), |
| 65 | +these operations may degrade to O(n). |
| 66 | + |
| 67 | +**Space Complexity:** O(n), where n is the number of elements in the tree. |
0 commit comments