In-depth understanding of binary search tree and its algorithms
The binocular search tree (BST) is one of the most important and basic tree structures in computer science and data structure. It is not just a high frequency test point in examinations and interviews, but rather a bottom-up idea that is widely applied in practical works such as database indexes, catalogues of document systems, etc.
1. Core nature of the diagonal search tree
A standard fork-search tree must meet the following extremely strict rules:
- Left right big: The value of all nodes on the left tree of any node must be less than The value of the node.
- Right big left small: The value of all nodes on the right subtree of any node must be Greater than The value of the node.
- Recursive: Its left and right trees are also necessarily two-kick search trees.
- No repeat nodes: The standard BST does not normally allow for two equal value nodes.
Because of this "small right left" feature, we're doing it in BST.
Find, insert or delete
, and by comparing the size of the node, you can directly exclude half of the subtrees at a time. This makes BST's average time complex as amazing as it is.
O(log n)
。 But in the worst case scenario (e.g. you insert 10, 20, 30, 40, 50, trees will be degraded into a right-sided chain, and time complexity will be degraded to
O(n)
。 You can observe this by using the "Degraded Right Slash" template on the left of this page.
2. The mystery of the four-way approach
The Traversal means that each node of a tree is visited by some rule and only once. Because trees are non-linear structures, there are more than one path through. The following cross-reference is the algorithm (e.g., The basics of the LeetCode are:
-
Pre-order:
根SectionPoints → LeftSon树 → RightSon树。 It is often used when re-establishing a tree or replicating it. -
Middle Passage (In-order):
LeftSon树 → 根SectionPoints → RightSon树。 In the binary search tree, there is a super feature of the middle sequence: The result of the journey must be an incremental sequence! If you want to sort a tree out, you can go through it in a middle order. -
Post-order:
LeftSon树 → RightSon树 → 根SectionPoints。 Often used to treat a scene that requires the completion of a child before the parent, for example by deleting the whole tree or by calculating the total size of a folder in a tree. -
Sequence Process (Live-order):
FromGo, go, go!Present.Down,FromLeftPresent.Right。 Also known as the Wide Priority Search (BFS), it is often done using Queue.
3. Delete node (hard point)
Inserting and searching in BST is simple, but Delete It is a painful point, because it is important to ensure that the “small right-hand right” feature remains “at the right and left” after deletion. The deletion of nodes is usually divided into three situations:
- Leave node: There are no right or right sub-trees at this node, just delete them.
- There's only one subpoint: Let's just get it to the only child that points to it.
- There are two sub-points: It's the most complicated! You need to find the right tree at that node. Minimal Node (i.e. the leftmost point in the right subtree) or in the left subtree Max. Node , replace the value of the deleted node with it, and then delete the smallest/maximum node.
You can use the "Delete" function at the top of this tool to personally delete a node with two subnodes to observe the subtle changes in tree structure!