Binary Search Tree-BST-search algorithm-insert algorithm-delete Algorithm

Source: Internet
Author: User

1. Brief Introduction

I am going to review several typical Tree structures recently. This article mainly focuses on Binary Search trees. The English name is Binary Search Tree (BST ).
This article mainly summarizes the search algorithm, insertion algorithm, and deletion algorithm of the Binary Search Tree.

2. Search Algorithms

This is relatively simple, either found, left, or right.

BSTNode * bst_search (BSTNode * node, int value ){
While (node! = NULL ){
If (value <node-> value) // left
Node = node-> left;
Else if (value> node-> value) // right
Node = node-> right;
Else // find
Return node;
}
Return NULL; // failed
}

3. insert algorithm

This is not difficult. First, locate the Insertion Location, either left or right, until an empty node is found, that is, the insertion location. If a node with the same value is found, the insertion fails.

Bool bst_insert (BSTNode * & root, int value ){
BSTNode * pre = NULL; BSTNode * curr = root;
While (curr! = NULL ){
If (value <curr-> value) {// left
Pre = curr;
Curr = curr-> left ;}
Else if (value> curr-> value) {// right
Pre = curr;
Curr = curr-> right ;}
Else // failed
Return false;
}
Curr = new BSTNode; // insert
Curr-> value = value;
Curr-> left = curr-> right = NULL;
If (pre = NULL) root = curr;
Else
Curr-> value <pre-> value? Pre-> left = curr: Pre-> right = curr;
Return true;
}

4. delete an algorithm

It is a little more complex than searching and inserting. Based on the situation of children who want to delete nodes, there are three situations: no children, only one child, and two children.
If no child exists, its parent node points to an empty node to delete it.
If there is a child, its parent node points to its child and deletes it.
If there are two children, the current node is exchanged with the largest element in the left subtree, and then the current node is deleted. The biggest element of the Left subtree must be the leaf node. After the switch, the current node is the leaf node. for deletion, see the absence of children. Another method is to swap the current node with the smallest element in the right subtree and then delete the current node.
Three scenarios are considered for code implementation, and each case considers whether the node to be deleted is the root node. Although the implementation is too long, the situation is well considered. It is enough for beginners.

Bool bst_delete (bstnode * & node, int value ){
Bstnode * parent = NULL;
Bstnode * TMP;
While (node! = NULL ){
If (value <node-> value) {// left
Parent = node;
Node = node-> left;
}
Else if (value> node-> value) {// right
Parent = node;
Node = node-> right;
}
Else {// found
If (NULL = node-> left & NULL = node-right) {// leaf node
If (parent = NULL) {// Root Node
Delete node;
Node = NULL;
}
Else {// non-Root Node
(Parent-> left = node )? (Parent-> left = NULL) :( parent-> right = NULL );
Delete node;
Node = NULL;
}
}
Else if (NULL! = Node-> left & NULL = node-> right) {// only the left child
If (parent = NULL) {// Root Node
Tmp = node;
Node = node-> left;
Delete TMP;
}
Else {// non-Root Node
(Parent-> left = node )? (Parent-> left = node-> left) :( parent-> right = node-> left );
Delete node;
}
}
Else if (null! = Node-> Right & null = node-> left) {// only the right child
If (parent = NULL) {// Root Node
TMP = node;
Node = node-> right;
Delete TMP;
}
Else {// non-Root Node
(Parent-> left = node )? (Parent-> left = node-> right) :( parent-> right = node-> right );
Delete node;
}
}
Else {// both left and right children
BSTNode * leftNode = node;
While (leftNode-> right! = NULL ){
Parent = leftNode;
LeftNode = leftNode-> right;
}
// Switch leftNode and node
Int swapValue = leftNode-> value;
LeftNode-> value = node-> value;
Node-> value = swapValue;
// Delete the leftNode. The parent is certainly not empty.
(Parent-> left = node )? (Parent-> left = NULL) :( parent-> right = NULL );
Delete node;
}
}
}
Return false; // failed
}

5. Reference

Baidu encyclopedia _ Binary Search Tree http://baike.baidu.com/view/389453.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.