Two Pointer Problems (Part 2) - Sliding Window
Two pointers are used to scan in two directions during traversal, achieving the desired algorithmic goals.
Binary Search Tree (BST), also known as a binary sort tree or binary ordered tree, is either an empty tree or a binary tree with the following properties:
As a classical data structure, binary search trees offer fast insertion and deletion operations, along with efficient searching capabilities. Hence, they find wide applications, such as in file systems and database systems for efficient sorting and retrieval operations.
Binary search trees possess the following properties:
A BST is constructed using binary trees. In addition to key and positional data, each node also contains attributes lchild
and rchild
. If a child node does not exist, the corresponding attribute value is null.
typedef struct Node {
int data; // Data
struct Node *lchild, *rchild; // Left and Right Child
} * BST, BSTNode;
The construction and destruction of a BST are similar to those of a regular binary tree. Here, we focus on additional operations like search, insertion, and deletion compared to regular binary trees.
The find()
function searches whether a node with a known data value exists in the tree and returns the node. The code is as follows:
BST find(BST tree, int data){
BST p = tree;
while(p) {
if (p->data == data) return p;
if (data < p->data) {
p = p->lchild;
} else {
p = p->rchild;
}
}
return NULL;
}
The insert()
function inserts a node into the tree. All nodes in a BST are inserted as leaf nodes. Hence, after creating node
bool insert(BST& tree, int data) {
BST p = tree, f;
while(p) {
if (p->data == data) return false;
f = p; // Record f as the parent node of p
if (data < p->data) {
p = p->lchild;
} else {
p = p->rchild;
}
} // Check if data is already in the tree
p = (BST) malloc(sizeof(BSTNode));
p->lchild = p->rchild = NULL;
p->data = data; // Create node
if(!tree) tree = p;
else if(data < f->data) f->lchild = p;
else f->rchild = p;
return true;
}
After deleting a node in a binary search tree, its sorting order must still be maintained. Therefore, deletion is divided into three cases:
The code is as follows:
bool delete(BST& tree, int data) {
if(!tree) return false;
BST p = tree, f;
while(p) {
if (p->data == data) break;
f = p; // Record f as the parent node of p
if (data < p->data) {
p = p->lchild;
} else {
p = p->rchild;
}
} // Find the position of data
if(!p) return false; // If not found, return false
if(!p->lchild) { // Case 1
if(tree == p) tree = p->rchild;
else if(f->lchild == p) f->lchild = p->rchild;
else if(f->rchild == p) f->rchild = p->rchild;
free(p);
}
else if(!p->rchild){ // Case 2
if(tree == p) tree = p->lchild;
else if(f->lchild == p) f->lchild = p->lchild;
else if(f->rchild == p) f->rchild = p->lchild;
free(p);
}
else{ // Case 3
BST tmp = p->lchild, fl = p;
while(tmp->rchild) fl = tmp,tmp = tmp->rchild;
p->data = tmp->data;
if(fl->lchild == tmp) fl->lchild = NULL;
else if(fl->rchild == tmp) fl->rchild = tmp->lchild;
free(tmp);
}
return true;
}