C ++ Data Structure Binary Search Tree (BST)

Source: Internet
Author: User

The binary search method belongs to the division and control method in the family of algorithms. The process of binary search is relatively simple. For the code, see my other log and click here! Because the ordered table involved in the binary search is a vector, if there is an operation to insert or delete nodes, the cost for maintaining the order of the table is O (n ).

In terms of query performance, the binary search tree and the binary search are not uncommon. However, to maintain the order of the table, the binary search tree does not need to move nodes. You only need to modify the pointer to complete the insert and delete operations, and its average execution time is O (lgn), so it is more effective. Binary Search Tree, as its name implies, is a tree data structure that can be used for Binary Search. Its left child is smaller than its parent node, and its right child is larger than its parent node, another feature is that "sequential traversal" allows nodes to be ordered. When searching for keywords, compare the results with the keywords of the root node and choose to continue the comparison with the left or right subtree, exit if the search keyword is found or the left and right subtree of the Access Node does not exist (the keyword is not found!

The main operations of the binary search tree are:Insert keywords,Search for keywords,Delete keywords. The three steps and algorithm implementation are described in detail below.

For convenience, I implemented the binary search tree as a class with the following structure:

typedef  Node_ * Node_ * Node_ *~**

1. The process of inserting keywords is as follows:

* X = NULL, * current, * = (compEQ (data, current-> data) = compLT (data, current-> data )? -> Left: current-> (x = (Node *) malloc (* x ))) = <-> data =-> parent =-> left =-> right = (compLT (x-> data, parent-> left =-> right =

CompEQ and compLT are two macro definitions, which are used to compare the size of two keywords.

2. The process of searching for keywords is relatively simple. It is similar to the binary search method. The Code is as follows:

Node**current =(current !=(compEQ(data, current->= compLT(data, current->data) ?->left : current->

3. The process of deleting keywords is divided into two types of discussion: single-child and left-right children.

1> single-child analysis:

If the deleted node has a left child, the left child is taken to the top. If there is a right child, the Right child is taken to the top. so easy! :

* PNode = (pNode = <* x, * (pNode-> left = NULL | pNode-> right = pNode-> (y-> left! = NULL) y = y-> (y-> left! = Y-> (x) x-> parent = y-> (y = y-> parent-> left =-> parent-> right = (y! =-> Left = pNode-> (y-> left) y-> left-> parent =-> right = pNode-> (y-> right) y-> right-> parent = pNode-> (pNode = pNode-> parent-> left =-> parent-> right =

Okay, the code for the binary search tree has come to an end. Let's analyze the Insert Process of the Binary Search Tree. If there are the following sequences: <4, 17, 16, 20, 37, 38, 43>, the following binary tree is generated:

This has completely degraded into a single-chain table, which will inevitably affect the search process of keywords. However, there will always be a solution. I will continue this topic in the next blog and rotate the common binary tree, that is, using the balanced binary tree to keep its worst complexity at O (logN ).

Thank you for reading this article. I hope it will help you! PS: some of the images in this article use the illustrations of another article in the blog Park (Click here )!

Published by Windows Live Write!

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.