Introduction
Introduction to Binary Search Tree¶
Binary search tree is a tree-shape data structure of binary tree, which is defined as follows:
-
An empty tree is a binary search tree.
-
If the left subtree of the binary search tree is not empty, the weight sum of all nodes on the left subtree are less than the value of its root node.
-
If the right subtree of the binary search tree is not empty, the weight of all nodes on the right subtree are greater than the value of its root node.
-
The left and right subtrees of a binary search tree are both binary search trees.
The time spent on basic operations on a binary search tree is proportional to the height of the tree. For a binary search tree with
Basic operations¶
In the following code, we assume that val[x]
is the value stored at node cnt[x]
is the number of occurrences of the value stored in lc[x]
and rc[x]
are the left and right child nodes of node
Traverse binary search tree¶
According to the recursive definition of the binary search tree, the sequence of the in-order traversal of the binary search tree is a non-decreasing sequence. The time complexity is
The code to traverse a binary search tree is shown below:
void print(int o) {
// traverse the binary search tree with root o
if (!o) return; // encounter empty tree, return
print(lc[o]); // recursively traverse the left subtree
for (int i = 1; i <= cnt[o]; i++) printf("%d\n", val[o]); // output root node information
print(rc[o]); // recursively traverse the right subtree
}
Find min/max value¶
Based on the property of the binary search tree, the minimum value on the binary search tree is the leftmost vertex of the left chain in the binary search tree, and the maximum value is the rightmost vertex of the right chain in the binary search tree. The time complexity is
The findmin and findmax functions respectively return the node number val[o]
to get the corresponding minimum/maximum values.
int findmin(int o) {
if (!lc[o]) return o;
return findmin(lc[o]); // always go to left child
}
int findmax(int o) {
if (!rc[o]) return o;
return findmax(rc[o]); // always go to right child
}
Insert an element¶
Define insert(o,v)
as the operation to insert a new node with value
It can be classified as follows:
If
If the weight of
If the weight of
If the weight of
The time complexity is
void insert(int& o, int v) {
if (!o) {
val[o = ++sum] = v;
cnt[o] = siz[o] = 1;
return;
}
siz[o]++;
if (val[o] == v) {
cnt[o]++;
return;
}
if (val[o] > v) insert(lc[o], v);
if (val[o] < v) insert(rc[o], v);
}
Delete an element¶
Define del(o,v)
as the operation to delete a node with a value of
First find a node with a weight of
If the additional
If the additional
If
If
If
Time complexity is
int deletemin(int& o) {
if (!lc[o]) {
int u = o;
o = rc[o];
return u;
} else {
int u = deletemin(lc[o]);
siz[o] -= cnt[u];
return u;
}
}
void del(int& o, int v) {
// Note that o may be modified
siz[o]--;
if (val[o] == v) {
if (cnt[o] > 1) {
cnt[o]--;
return;
}
if (lc[o] && rc[o]) o = deletemin(rc[o]);
// Here is an example of finding the minimum value of the right subtree
else
o = lc[o] + rc[o];
return;
}
if (val[o] > v) del(lc[o], v);
if (val[o] < v) del(rc[o], v);
}
Find the ranking of elements¶
The ranking is defined as the number of numbers before the first identical element after sorting the array elements
Maintain the subtree size
Time complexity is
int queryrnk(int o, int v) {
if (val[o] == v) return siz[lc[o]] + 1;
if (val[o] > v) return queryrnk(lc[o], v);
if (val[o] < v) return queryrnk(rc[o], v) + siz[lc[o]] + cnt[o];
}
Find the k-th element¶
In a subtree, the ranking of the root node depends on the size of its left subtree.
If the size of the left subtree is greater than or equal to
If the size of the left subtree is in the interval
If the size of the left subtree is less than
Time complexity is
int querykth(int o, int k) {
if (siz[lc[o]] >= k) return querykth(lc[o], k);
if (siz[lc[o]] < k - cnt[o])
return querykth(rc[o], k - siz[lc[o]] - cnt[o] + 1);
return val[o];
// To find the node corresponding to the k-th element, just return o directly
}
buildLast update and/or translate time of this article,Check the history
editFound smelly bugs? Translation outdated? Wanna contribute with us? Edit this Page on Github
peopleContributor of this article OI-wiki
translateTranslator of this article Visit the original article!
copyrightThe article is available under CC BY-SA 4.0 & SATA ; additional terms may apply.