Aha! Algorithm Chapter 1 Magic Tree

Source: Internet
Author: User
Section 3 heap sorting

Create a heap of n elements. First, encode the N nodes from 1 to n in the top-down and left-right mode, in this way, N nodes can be converted into a Complete Binary Tree.

Then, from the last non-leaf node (node number is n/2) to the root node (node number is 1), scan all nodes one by one, and adjust the current node downward as needed, until the subtree with the current node as the root node meets the heap characteristics.

# Include <iostream> # include <vector> # include <algorithm> using namespace STD; // The Void shiftdown function (vector <int> & A, int N, int index) {While (Index * 2 <= N) {int T = index; if (a [T]> A [2 * Index]) t = 2 * index; if (2 * index + 1 <= N & A [T]> A [2 * index + 1]) t = 2 * index + 1; if (T! = Index) {swap (A [T], a [Index]); Index = T;} else break ;}} // Delete the largest element int deletemax (vector <int> & A, Int & N) {int res = A [1]; swap (A [1], A [n]); n --; shiftdown (A, N, 1); Return res;} // The Void shiftup (vector <int> & A, int N, int index) {If (Index = 1) return; while (index! = 1) {if (a [Index] <A [INDEX/2]) Swap (A [Index], a [INDEX/2]); else break; index/= 2 ;}/// create heap void make_heap (vector <int> & A, int N) {// note that the index starts from 0 for (INT I = n/2; I> 0; -- I) {shiftdown (A, N, I );}} void heap_sort (vector <int> & A) {int n =. size ()-1; while (n> 1) {swap (A [1], a [n]); n --; shiftdown (A, N, 1 );}} int main () {int N; CIN> N; vector <int> A (n + 1, 0); For (INT I = 1; I <= N; ++ I) {CIN> A [I] ;}// heap building make_heap (A, n); heap_sort (a); For (INT I = 1; I <= N; ++ I) {cout <A [I] <"" ;}cout <Endl; int num = N; For (INT I = 1; I <= N; ++ I) {cout <deletemax (A, num) <Endl ;}}
Heap sorting section 4th and querying Sets

There are two types of query Set Optimization: path compression and rank merge. For details, see Introduction to algorithms.

/** Query set operations */# include <iostream> # include <vector> # include <algorithm> using namespace STD; int f [1000] = {0 }; void make_set (INT size) {for (INT I = 1; I <= size; ++ I) f [I] = I ;} // use the path compression method to find the element int find_set (int x) {If (F [x] = x) return X; else {f [x] = find_set (F [x]); // query the set where the X element is located. The compressed path return f [x] During backtracing.} void union_set (int x, int y) {int T1 = find_set (x), T2 = find_set (y); If (T1! = T2) {f [T2] = T1 ;}int main () {int n, m; CIN >>n> m; make_set (N ); for (INT I = 1; I <= m; ++ I) {int X, Y; CIN >>x> Y; union_set (x, y );} int sum = 0; For (INT I = 1; I <= N; ++ I) {If (F [I] = I) sum ++ ;} cout <sum <Endl; return 0 ;}
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.