Data structure understanding study notes

the list

A "list" is a data structure that can store multiple values.

index 

The characteristic is how the data is paired with the "indicator", and the memory location that indicates the next piece of data.

Memory

 In a list, data is stored in discrete locations in memory.

sequential access

Since the data is stored in non-passive locations, each data can only be accessed through the indicator that precedes it.

When a policeman in a Hong Kong movie does a dangerous spy mission, only his superiors know that he is a policeman. This is for protection, but once one of the policemen’s superiors has an accident, then the policeman has no one to prove that he is a policeman.

The addition of data needs only to be performed by replacing the pointers on either side of the addition.

array 

An "array" is a data structure that can store multiple values.

Each element can be accessed by an index (a number indicating the amount of data).

 Memory : Data is stored sequentially in memory in consecutive locations .

Random access : Since they are stored in contiguous locations, their indices can be used to calculate memory addresses for random access to data.

Another characteristic of data is that adding or removing data at a specific location is expensive compared to lists.

First we secure extra space at the end of the array.

 

To make room to recommend love, move data one by one.

 

"Green" is added to the blank, completing the addition.

 

First we remove elements,  filling empty space by moving data one by one.

Finally, the deletion is done by removing the extra space. 

the stack

 A "stack" is a data structure.

The structure of a stack can easily be imagined as a bunch of items stacked vertically.

When taking items out of a stacked mountain, we take items sequentially from the top.

When adding data to the stack, the data is added last.

The operation of adding data to the stack is called " push ".

 When fetching data from the stack, it will start fetching from the most recently added data.

The operator that removes data from the stack is called " popping ".

This method of fetching the most recently added data first is called " Last In First Out " (Last In First Out), or "LIFO" for short.

queue

"Queue" is a data structure.

Queues are also known as "waiting lines" and, as the name suggests, they can easily be imagined as groups of people waiting in line.

When queuing, the earlier the queue, the higher the priority.

When data is added to the queue, the data is placed last.

The operation of adding data to a queue is called " enqueue ".

When fetching data from the queue, it will be fetched from the earliest added data .

The operation of taking data out of the queue is called " dequeue ". 

The method of taking out the data that was originally added first is called " First In First Out" (First In First Out), or "FIFO" for short.

hash table

A "hash table" is a data structure.

It is good at storing data as a collection of "keys" and "values".

 

At this point, the name is the key, and the gender is the value.

As an example, imagine the data in the graph as an array.

We prepared 6 boxes for the array and stored data in them. 

To find Ally's gender:

We don't know which box Ally is stored in the array.

From this, we need to start the search from scratch . This operation is called a " linear search ". 

The key of the data stored in box 0 is Joe, not Ally.

Box 1 is not Ally either.

Box 2 is not Ally either.

 Box 3 is not Ally either.

The Key and Ally of the data stored in the 4th box match. By taking out the corresponding Value, we find that Ally's gender is female (F).

In this way, the cost of a linear search operation is proportional to the size of the data.

When storing data in an array, it takes some time to search through the data, making it an unsuitable choice.

Hash tables solve this problem.

We will prepare an array to store some data, for corruption, there are 5 boxes in the array.

Now to store some data.

 

When storing "Joe"'s data, a hash value for the key is calculated using a hash function (a function that converts data into a fixed-length value) . Annotation results in 4928. The hash of the lookup is divided by the number of boxes in the array, 5, to find the remainder . The operation to find the remainder of the division is called the "mod" operation. The result of the mod operation is 3. Joe's data is stored in the 3rd box of the array, the same number that was found.

 

This operation will be repeated to store other data.

Find the hash of the key and mod it with the box number 5 in the array.

All data is stored and the hash table is completed.

At this time, look up Dan's gender again. 

To know which box Dan is stored in the array, we find the hash of the key and mod it with the box number 5 in the array. The result is 4.

The key stored in the 4th box of the array matches "Dan".

By taking out the corresponding Value, we know that Dan's gender is male (M).

Find other data and so on.

 

A hash table provides fast access to data in an array by using a hash function.

Lists are used when hashes overlap , allowing flexibility for indeterminate amounts of data.

If the size of the array used for hashing is too small , duplication will increase and linear searches will be more likely to occur .

On the contrary, if the size of the array is too large , there will be many data boxes that do not store data, wasting memory , so you need to be cautious.

Hash values, which enable flexible storage of data and fast lookups, are used in associative arrays in programming languages.

heap

A "heap" is a tree structure and is used when implementing a "priority queue".

A priority queue is a data structure.

In a priority queue, data can be added in any order .

Instead, when fetching data , the smallest value is chosen first . (according to the order of data from small to large)

The ability to freely add data and take it out starting from the smallest value is defined as a priority queue.

 

As a rule of the heap, the child class number is always greater than its parent class number.

 

Added numbers will be placed first at the end.

If the parent class number is higher, the child class is swapped with the parent class.

 Since parent class 6 > child class 5, replace the number.

Repeat this until no replacement occurs.

Parent class 1 < child class 5, because the number of the parent class is smaller, no replacement will occur. => Finish adding numbers in the heap.

Take numbers from the pile, taking the number from the top.

 

In the heap, the smallest value is kept at the top position.

Since the numbers at the top are taken out, we need to organize the structure of the heap.

 Move the ending number to the top.

When the child number is smaller than the parent number, the smaller number of adjacent child numbers is exchanged with the parent number.

Since the parent class 6 > right child class 5 > left child class 3, the left child class is exchanged with the parent class.

 

Repeat this until no replacement occurs.

 

Using the heap in this way can quickly fetch the smallest data.

However, operations that fetch data in the middle of the tree cannot be performed.

Heaps are used for things like priority queues and Dykstra's algorithm.

binary search tree

 A data structure.

The numbered points are called "nodes".

A binary search tree has two properties.

The first property is that all nodes are larger than the nodes in the left subtree.

 

 

Instead, their second property is that all nodes are smaller than the nodes in their right subtree .

 

First, the smallest binary search tree node is at the end of the leftmost subtree row of the topmost node.

 

Reciprocally, the largest node of a binary search tree is at the end of the rightmost subtree row of the topmost node.

 

 

Start with the topmost node of the binary search tree in order to find the correct position for additional nodes.

Since 1 < 15, go left.

 

 Since 1 < 9, keep going left.

 Since 1 < 3, continue walking left, but since there is no node ahead, we add it as a new node.

 Add other numbers, and so on.

When the node has no subclasses, you only need to delete the target node and you are done.

 

The target node is deleted, just move the child node to the position of the deleted node.

 

 

When deleting a node with two subclasses, the target node is deleted first.

And find the largest node from the left subtree of the deleted node and move it to the position of the deleted node.

 

If the node is moved and has children of its own, repeat the same process recursively.

And, although we used the largest node on the left, the same goes for using the smallest node in the right subtree.

Efficient searches can be achieved using a binary search tree.

However, if the tree is close to forming a straight line, its search efficiency will be very poor, such as linear search.

On the other hand, a binary search tree that has always maintained a good balance is called a "self-balancing binary search tree", which can preserve search efficiency.

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_62110645/article/details/129840468