2015-Ali C + + development additional questions first question

Source: Internet
Author: User

Similar to the implementation of a Cartesian tree:

Http://baike.baidu.com/link?url=nDGlKta6rvBzJ4_xm1TSp-Px05bU6gzgqWx7LnWwnXm5brtOCPJXXXRXeq9ZpIvsfrWkhua4D76oWrt2iQq2WK

From the topic can be seen, according to a, is a binary sort of tree, according to B, is a big pile.

Then you need to know a property: A two-fork sorting tree in the middle sequence traversal, you can get an ascending sequence.

(1) Build:

Solution 1: pair[] In accordance with the size B to the small sort, so that the first, must be the root node of the tree, continue to traverse pair[], according to the value of a to allocate the location of this node.

For example T->a > insert.a, so that the T right subtree to continue to find, if T->a < insert.a, go to T right subtree continue to find, each time t from the root node.

This efficiency is Nlog (N);

Solution 2: pair[] According to a small to large sort, and then traverse pair[] to build the tree, because the order is over, so with insert inserts must be in the current tree the largest one,

This can happen in two cases: 1. insert.b > t.b, then insert replaces the position of T, then insert.left = t;

2.insert.b < t.b, then, continue to compare with t.right.b, because, INSERT.A is the current largest, definitely go to the right, until a 1 situation occurs, or to a single empty tree, then insert.

This efficiency is also for Nlog (N);

Solution 3: Improved on the basis of solution 2, because each time it is from the root node to compare, it will take a little more time, and then we will use a stack to save the root node, the root node of the right sub-tree nodes (bottom of the root node) .... Since the inserted a is the largest, it must first be compared to the right, 1. If insert.b < stack.top.b, then Stack.top.right = INSERT, and then a right node, into the stack; 2. If insert.b > stack.t OP.B, it is necessary to backtrack, and then the stack--. top.b comparison, if there are 1 of the situation, then, before that right subtree, become insert.left. Insert into the stack, or appear, all stacks go out, indicating that the insert is about to become a new root. Using space to change time, efficiency is O (N);

(2) Insert ();

Solution: First according to the nature of the binary sorting tree, according to the size of a is inserted into a leaf node, after insertion, b judgment, if it is Zuozi inserted, and the inserted B is larger, then the parent node to a right rotation, conversely, if the right subtree is inserted, and the inserted B is larger, A left rotation is performed with his parent node, and back to the root junction.

   

voidTreap_insert (Treap_node*&a,intLabelintp) {if(!a) {a=NewTreap_node; A->label=label; A->p=p; }        Else if(label<a->label) {Treap_insert (a-left,label,p); if(a->left->p>a->p) treap_right_rotate (a); }        Else{Treap_insert (a-right,label,p); if(a->right->p>a->p) treap_left_rotate (a); }    }

2015-Ali C + + development additional questions first question

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.