Académique Documents
Professionnel Documents
Culture Documents
A binary search tree (BST), which may sometimes also be called ordered or sorted binary tree, is a node-based binary tree data structure which
has the following properties: The left subtree of a node contains only nodes with keys less than the node's key.
• The right subtree of a node contains only nodes with keys greater than the node's key.
• Both the left and right subtrees must also be binary search trees.
Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are
compared according to their keys rather than any part of their associated records.
The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order
traversal can be very efficient.
Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets, multisets, and associative arrays.
Deletion
• Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply remove it from the tree.
• Deleting a node with one child: Remove the node and replace it with its child.
• Deleting a node with two children: Call the node to be deleted N. Do not delete N. Instead, choose either its in-order successor node or its
in-order predecessor node, R. Replace the value of N with the value of R, then delete R.
Now, let's see more detailed description of a remove algorithm. First stage is identical to algorithm for lookup, except we should track the parent
of the current node. Second part is more tricky. There are three cases, which are described below.
This case is quite simple. Algorithm sets corresponding link of the parent to NULL and disposes the node.
Example. Remove -4 from a BST.
It this case, node is cut from the tree and algorithm links single child (with it's subtree) directly to the parent of the removed node.
This is the most complex case. To solve it, let us see one useful BST property first. We are going to use the idea, that the same set of values
may be represented as different binary-search trees. For example those BSTs:
contains the same values {5, 19, 21, 25}. To transform first tree into second one, we can do following:
o choose minimum element from the right subtree (19 in the example);
o replace 5 by 19;
o hang 5 as a left child.
The same approach can be utilized to remove a node, which has two children:
Notice, that the node with minimum value has no left child and, therefore, it's removal may result in first or second cases only.
Replace 12 with 19. Notice, that only values are replaced, not nodes. Now we have two nodes with the same value.
Remove 19 from the left subtree.
CODE
First, check first if root exists. If not, tree is empty, and, therefore, value, that should be removed, doesn't exist in the tree. Then, check if root
value is the one to be removed. It's a special case and there are several approaches to solve it. We propose the dummy root method, when dummy
root node is created and real root hanged to it as a left child. When remove is done, set root link to the link to the left child of the dummy root.
C++
bool BinarySearchTree::remove(int value) {
if (root == NULL)
return false;
else {
if (root->getValue() == value) {
BSTNode auxRoot(0);
auxRoot.setLeftChild(root);
root = auxRoot.getLeft();
if (removedNode != NULL) {
delete removedNode;
return true;
} else
return false;
} else {
delete removedNode;
return true;
} else
return false;
if (left != NULL)
else
return NULL;
if (right != NULL)
else
return NULL;
} else {
if (left != NULL && right != NULL) {
this->value = right->minValue();
return this;
return this;
int BSTNode::minValue() {
if (left == NULL)
return value;
else
return left->minValue();
}
Structures
Structures are the C equivalent of records. A structure type is defined by
struct struct-name {
type field-name;
type field-name;
...
}
e.g.
struct student_type {
char name[20];
int ID;
}
Elements of that type are defined by
fred.ID = 891234;
strcpy(fred.name, "fred");
It is common to have pointers to structures. The straightforward notation is clumsy, so a shorthand is available
(*student_ptr).ID = ...
student_ptr->ID = ...