Binary Search Tree and query set

Source: Internet
Author: User
Tags element groups
BST: the feature of the Binary Search Tree: The left child <root node <right child binary search tree can effectively manage the number of sets (1) Search for the value, greater than the root node to the right, if the value is smaller than the root node, perform a search. recursive search until it is found or cannot be found. (2) Insert is similar to search. After finding the corresponding location, insert the location into a new node. (3) deletion is a little more complex: If there is no left child in the vertex to be deleted, the left child who directly raises the right child to the vertex to be deleted does not have the right child, directly raise the left child to the largest node of the left child. Code implementation:
Struct node {int data; node * LCH, * RCH;}; node * insert (node * P, int X) {If (P = NULL) {node * q = new node; q-> DATA = x; q-> LCH = Q-> RCH = NULL; return Q ;} else {If (x <p-data) P-> LCH = (p-> LCH, x); else p-> RCH = insert (p-> RCH, X ); return P ;}} bool find (node * P, int X) {If (P = NULL) return false; If (x <p-> data) return find (p-> LCH, x); else if (x = p-> data) return true; else return find (p-> RCH, x );} node * remove (node * P, int X) {// If (P = NULL) return NULL; else if (x <p-> data) p-> LCH = remove (p-> LCH, x); else if (x> P-> data) P-> RCH = remove (p-> RCH, X ); else if (p-> LCH = NULL) {node * q = p-> RCH; Delete P; return Q ;} else if (p-> LCH-> RCH = NULL) {node * q = p-> LCH; // directly raise the left child, the right child of the new node is the right child of the original deleted node. Q-> RCH = p-> RCH; Delete P; return Q;} else {node * q; For (q = p-> LCH; q-> RCH! = NULL; q = Q-> RCH); node * r = Q-> RCH; q-> RCH = r-> LCH; r-> LCH = p-> LCH; r-> RCH = p-> RCH; Delete P; return r ;}}

Querying and querying sets is a data structure for efficient management of element groups. The query sets can be merged, but cannot be split. Each group in the query set represents a tree. Operation: query whether the element is in the same group, and merge the elements in different groups. (1) initialization: At the beginning, prepare N points to represent n elements. At the beginning, there is no edge. (2) combine a group of root nodes to the root edge of another group (record the height of the tree (rank), rank small nodes to the large edge) (3) to query whether two elements are in the same group, you can check whether the root of the two elements is the same. Same: A group; different: not a group of code implementations:
# Include <iostream> using namespace STD; const int max = 100; int par [Max], rank [Max]; void Init (int n) {for (INT I = 0; I <n; I ++) {Par [I] = I; // No edge rank [I] = 0 ;}} int find (INT X) {If (par [x] = x) return X; else return par [x] = find (par [x]);} void unite (int x, int y) {x = find (x); y = find (y); If (x = y) return; If (rank [x] <rank [y]) {Par [x] = y;} else {Par [y] = x; If (rank [x] = rank [y]) {rank [x] ++ ;}} bool same (int x, int y) {return find (x) = find (y );}



Binary Search Tree and query set

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.