The use of heaps in games

Source: Internet
Author: User

* The application and principle of the timer:

In the game, the activity state, the game data save and the load, the boss refreshes the time and so on, may use the timer. And the heap is very important for the realization of the timer.

the operation of the timer is not difficult, that is, the internal preservation of multiple time and its callback function, when the system time to achieve our saved time value, the execution of the callback function, so as to achieve the effect of timed work. at the same time, when deciding whether to reach the specified times, you only need to determine the earliest time (the earliest time is not reached, the later time will definitely not be reached), so the time list needs to be sorted.

At first, I imagined that the data structure used to hold the time inside the timer is a list, the reason is easy to understand, the implementation is simple, and the time complexity of deleting the node is O (1) (for the timer to say, the delete timeout time, and the timeout time is definitely at the head node, so time complexity is O (1)).

However, this approach has obvious shortcomings because the save time list needs to be sorted, so the time complexity of the insertion time node is O (n).

Later, the discovery heap in the timer has very good efficiency, but the heap sort time complexity is O (nlog2n), "XXX, that is not even slower than the list!" ”。 In fact, we don't need to order the whole sequence. As I said earlier, "when you decide whether to reach a specified time, you only need to determine the earliest time." That means we just need to know the earliest time of the entire sequence. And the heap sort just has such a noun "small top heap". In other words, each time node is inserted and the node is deleted, the sequence is maintained in the state of the small top heap.

For the small top heap, Insert the node, readjust the heap structure , its time complexity is O (log2n), delete the node, readjust the heap structure, its time is also complex O (log2n). Therefore, the entire time of the timer implemented with the small top heap is a complex O (log2n). The time complexity of O (n), which is implemented by the list, is significantly more efficient.

* Small Top heap Explanation:

The heap's preservation structure is an array, while the heap is divided into large top heaps and small top heaps, satisfying key[i]>=key[2i+1]&&key[i]>=key[2i+2] called large top heaps, satisfying key[i]<=key[2i+1]& &KEY[I]<=KEY[2I+2] called the small top heap. In other words, each node of the big top heap is larger than the child node, and each node of the small top heap is smaller than the child node. So by the above-mentioned nature, the key word of the heap top of the big top heap is definitely the largest of all the keywords, the top of the heap of the small top heap is the smallest of all the keywords.

The heap is a completely binary tree. Below is a small top pile in the construction.

To insert a node:

First, the new node is inserted at the end of the tree, and the new node and the parent node do not need to change if the new node is greater than or equal to the parent node;

Delete Heap Top:

Deleting a heap is the same as deleting a node, and deleting a node is just the opposite of inserting a node, the deleted node looks down to the smallest node A in two sub-nodes to replace its location, and Node A is the equivalent of the deleted node to continue looking down until the leaf node.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The use of heaps in games

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.