This article is reproduced from GameDev.net, only for learning and communication. Because just started to learn translation, inevitably some omissions, if there is any place to translate the incorrect, please hesitate to inform, thank you very much.
Original link: http://www.gamedev.net/page/resources/_/technical/general-programming/ data-structures-for-pre-college-programmers-choosing-the-right-structure-r2991
Many beginners on the internet are still students. Usually beginners learn by reading tutorials on the web, copying code from books, and trying something of interest.
This article is part of a basic series of data structures that novice developers need to understand. In the previous article, nonlinear data structures were analyzed. (Non-linear structures.)
This article is to help beginners understand how to choose a suitable data structure or container type.
Data
Some of the most commonly used data structures are described in the previous articles in this series, which is just a generalization.
the linear structure includes: arrays, dynamic arrays, and linked lists. They are linear because they always stay where you put them. Random access to the array is very fast, and adding and removing data at the end of the array has the proper performance. If you frequently add or delete data in the middle, the linked list will be a very good choice.
linear endpoint Data structures include: heap, queue, and so on. The way they work is very similar to the same name operation in the real world. Heap, such as a pile of planks or a data heap, you can put things on top of the heap, you can directly use a piece of wood or data from the earliest face, or you can just take out the topmost plank or the data. Queues, like a person lined up as a team or a queue of data, to join from the end of the team, to work from the way the team first removed.
non-linear data structures include: dictionary, ordered set and unordered set. These structures are internally non-linear, meaning that the order in which you insert the relevant data is fundamentally unrelated to the order in which the data is fetched. A data dictionary works very much like a real dictionary, and it has a key value (key: index) and value (valUE: data value). An ordered set (ordered set) is the same as a data dictionary sorted with only the key value (key) that does not contain the value (value) . As for the unordered set is like a data grab bag (like a lottery in the black boxes ), but the unordered set of the name is a bit misleading, actually they are also orderly, but their sort of way for our use is no use. These non-linear data structures are ideal for fast data discovery.
The impact of a good data structure
Most of the time, programmers just need to traverse the entire collection of data. Usually, when we need to access each data item from the beginning, we don't care how the data is sorted internally. In this common case, the choice of data structures is not a big problem.
when in doubt, the most common choice is the dynamic array, it can become arbitrary size, good, and in the late need can also easily converted into other data structures.
but sometimes he has some problems.
in the game, a very common problem is to find the way. You need to find a path from a to b . The most common path finding algorithm is the A- star algorithm. In the a -star algorithm you need a data structure to store part of the path. This structure should be sorted so that we can put the most desired path in front of the container for our use. If the road is detected and not a complete path, the algorithm makes him a part of a larger path and joins the equivalent of the container.
using a dynamic array as a container for the A- star algorithm will be a bad choice for the following reasons:
- Removing an array from the beginning of a dynamic array is one of the least efficient operations we can perform.
- When a new path is added, it is very inefficient to reorder dynamic arrays.
If you remember what you said before, there is the most optimized data structure for this type of access. We can remove the first data, insert the data from the back, and automatically rearrange the index to the best path. The priority queue is a good choice for the A-Star algorithm container type. It is usually built into the language inside and through perfect testing.
Write the selection according to the mode used
what data structure to use is usually based on the design pattern you are using.
Dynamic array -- default selection
when in doubt, use a dynamic array. In C + + called vector, called ArrayList in Java, is called list in C # .
dynamic arrays can often be used in accordance with the requirements. Most of its operations have good performance , and the remaining operational performance is not bad. If you find that you need other data structures, dynamic arrays are the easiest to modify.
Heap -- only one end
If you only add or remove at one end of a single side. Using heaps, which are stacks in C + + , are called Statck in Java and C # .
There are many algorithms that rely on heaps to implement. My first reaction was a double-stack calculator (two-stack calculator). Math problems like Hanoi can also be solved by heap. Of course, in game programming, you may not be able to use them.
Game tool experience analysis data. The parser relies heavily on the heap data structure to ensure that pairing items match correctly.
If you are using a variety of ai types. You can see that the heap data structure is difficult to estimate for automatic push-down in the automata family.
Queue -- FIFO
If you need to delete data from both ends, Then you can use a queue or a double-ended queue. In c++queue ( queue deque ( double-ended queue in java You can use queue deque interface, all based on linklist is implemented. In c#queue type with no built-in double-ended queue ( deque).
If you need to ensure that something important is handled first, but unless everything happens in an orderly fashion, it is done in order of priority. At this point you can use the priority queue for processing ,called priority_queue in C + + . In Java , called priorityqueue.c# , you need to implement this priority queue yourself.
Nonlinear structure -- fast indexing
If you need to create a stable data structure and frequently do random lookups, then you need to use nonlinear data structures.
Some non-linear data structures hold a pair of numbers, and some hold independent data. Some are user-friendly sort, some are computer friendly sort. If you want to list all the combinations between them, it will be an article. In fact, those have been described in detail in previous articles. for these non-linear structures which meet specific requirements, you can view previous articles on nonlinear data Structures .
Linked list -- frequent and orderly modifications
If you need to frequently manipulate data (additions and deletions) in the middle of a container, you only need to iterate through the list in sequence. You can use a linked list, called a list in C+ + , in java and C # called LinkedList.
The linked list is a very good container choice when the data needs frequent additions and deletions, and must be kept in an orderly state after additions and deletions.
Conclusion
Choosing the right data structure can make the performance of the algorithm very much better.
Understanding the main data structures, including their pros and cons, can help you choose the data structure you need.
I suggest that you study these major data structures in depth. A degree course in computer science that delves into these data structures usually lasts for several weeks.
Hopefully you've learned about the main data structures, and you can choose a good data structure without having to go into the data structure for a few weeks.
This series of articles is over, thanks for reading.
This article is reproduced from GameDev.net, only for learning and communication. Because just started to learn translation, inevitably some omissions, if there is any place to translate the incorrect, please hesitate to inform, thank you very much.
Original link: http://www.gamedev.net/page/resources/_/technical/general-programming/ data-structures-for-pre-college-programmers-choosing-the-right-structure-r2991
Translation: Programmer Data Structure Basics: Choosing the right data structure