Re-parse the tree data structure (1)

Source: Internet
Author: User

{

I have done a lot of interesting work recently.Data StructureProblem

Here, we will briefly introduce the basic solutions to the data structure problems.

I have discussed several data structures in the past.

So the title isRe-SolutionData Structure

The problems encountered are basically not related to the tree structure.

These paragraphsArticleAll aroundTree Data StructureOf

}

======================== Split line of the tucao =

There's a magical place in this magical world.

Called online judge (OJ) is a place where acmer oier sends out loneliness and emptiness.

There are two magical OJ types in many OJ: spoj and sgu.

Sgu is a card memory where many questions have less memory.

Spoj is a constant of a card, which has a lot of excellent theoretical complexity.ProgramOften determined as TLE

The reason is that the speed of the measuring machine is not average.

According to the research by shiniu, the questions that can be met by the AC are not available in spoj.

It can be seen that the speed of the spoj measuring machine is not only slow but also a dynamic slow process.

The old saying goes, black hair, I don't know how to study hard, but I regret reading too late.

It's really nothing to do With spoj.

However, compared with sgu, spoj does not have a memory card.

If you use hundreds of MB of memory, the MLE error will not occur on spoj.

Instead, it's because it has so much memory.

In addition, the spoj testing machine is fast enough to determine the evaluation machine as TLE.

Compared with many foreign OJ instances, OJ instances in China are relatively harmonious.

The speed of the evaluation machine is fast, and the webpage is fast. The OJ of the socialist country is indeed different.

======================== Split line of the tucao =

Thanks to spoj's magic ......

Some interesting questions on spoj have become amazing.

It would be nice to solve a very interesting problem.

However, spoj often gets a constant of the correct program.

After a long hard time, I got a yellow TLE.It's not really fun.

But it is worth noting thatThere are indeed many questions worth doing on spoj.

So here is a little bit of a magic spoj trick... try to use it as much as possible.Pointer!

========================================================== =====

I. pointers and Arrays

Generally, tree data structures cannot be left.Pointer

In computer science,PointerIs a programming language data type whose value refers directly to (or"PointsTo ") Another value stored elsewhere in the computer memory using its address.

(From: http://en.wikipedia.org/wiki/Pointer_%28computing%29)

The pointer variable stores a memory address.

The role of a pointer variable is like a pointer in the direction of guidance.

With pointers, we can organize data in memory very freely.

This undoubtedly makes it easy for us to implement various data structures.

WhileArrayIs another common data type.

In computer science,Array Data StructureOr simplyArrayIs a data structure consisting of a collection of elements (values orVariables), Each identified by at least oneIndex. An array is stored so that the position of each element can be computed from its index tuple by a mathematical formula. 

(From: http://en.wikipedia.org/wiki/Array_data_structure)

Using arrays to store a piece of data allows us to conveniently retrieve the k Data

In addition, as a basic data type supported by almost all languages, the array program can also be easily debugged.

========================================================== =====

2. Use arrays and pointers to implement data structures

Using the binary sorting tree as an example, two basic implementation methods are introduced.

One is the most harmonious pointer implementation method, which is usually introduced in the textbooks.

The node of a binary sorting tree usually has three fields, that is, the left son and right son keys.

Make sure that the key value of all nodes in the left subtree is smaller than that of the root node. The key value of all nodes in the right subtree is greater than that of the root node.

 
Typebst = ^ bstnode; bstnode = recordl, R: BST; N: longint; end; varroot: BST; beginnew (Root); root ^. l: = nil; root ^. r: = nil; root ^. n: = 1; end.

The pointer variable is a memory address, which makes debugging programs more difficult.

It is because the debugging of pointer variables is not convenient when you are not familiar with some data structures.

We can use a compromise method.Array analog pointer

NotesArray subscriptAndMemory AddressEquivalent, we can use arrays to simulate pointer operations.

So the aboveCodeYou can rewrite it like this.

 
Typebstnode = recordl, R: longint; N: longint; end; varpool: array [1 .. 100] of bstnode; TT: longint; begintt: = 0; Inc (TT); root: = tt; pool [root]. l: = 0; pool [root]. r: = 0; pool [root]. n: = 1; end.

To facilitate writing, we usually do not use the record type for definition.

It is written as follows. Although the structure is loose, it is moreConvenient

 
Varl, R, N: array [1 .. 100] of longint; begintt: = 0; Inc (TT); root: = tt; L [root]: = 0; R [root]: = 0; N [root]: = 1; end;

========================================================== =====

3. Quick writing

In fact, the above two writing methods are not very fast. Next we will introduce a method that I can write at a common speed.

First, there is a conclusion.Referencing a pointer variable is faster than referencing an array variable.

This is because the array is a continuous segment in the memory. Generally, the computer stores an array header address.

Then, the array element is referenced to obtain the element address by using the subscript as the offset and adding the offset to the array header address.

Therefore, referencing an array element is more efficient than directly referencing a pointer variable.Multiple Addition

Addition is a basic operation that cannot be ignored, especially when multiple calls are performed.

The implementation of the data structure usually references the node information multiple times.Millions more or even tens of millions more

Therefore, we should try to use pointers instead of array simulation to significantly improve the program speed.

However, pointers are also used.BottleneckIt is the new function that assigns an address to a node each time.

Using the above array simulation pointer implementation, we can get a better solution.

Allocating array space is usually faster than allocating many nodes dynamically by allocating a continuous memory.

You only need to first open a memory pool and then use the tail pointer ++You can get new nodes.

And the speed is very fast, so we can get a perfect universal data structure implementation method.

The basic code is as follows:

 
Typebst = ^ bstnodebstnode = recordl, R: BST; N: longint; end; varbstpool: array [0 .. 100] of bstnode; TT, root, null: BST; beginnull: = @ bstpool [0]; null ^. l: = NULL; null ^. r: = NULL; TT: = NULL; Inc (TT); root: = tt; root ^. l: = NULL; root ^. r: = NULL; root ^. n: = 1; end.

Null isSentinel nodeYesReduce special NULL pointer judgment

@ Is the first node in the memory pool used as the sentinel node for the Operation to retrieve the variable address.

The initial tail pointer is the first node in the memory pool.

========================================================== =====

4. Practice testing of pointer and array simulated pointer

To better prove the efficiency gap between the pointer and the array simulation pointer

I did a practical test. The test code is the same as the two statements, but the pointer and the array are used to simulate the pointer's Balance Tree respectively.

The test results are as follows:

WhereThe input stream is omitted by the pseudo-random function in the program.

Therefore, the test directly targets two balanced binary trees with different implementations.

The speed at which the balanced binary tree ratio pointer is achieved by the array simulation pointer in the actual measurementAbout 1/2 slower

This section describes some constant optimization methods.

Some interesting data structure problems will be introduced later.

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.