Experiment 1 Basic Type of Data Structure

1. Introduction

1.1. The purpose of the experiment

This experiment mainly introduces the basic types, characteristics and basic representation methods of data structures. After studying, you can understand the concepts and representations of arrays, stacks, linked lists, queues, trees, graphs, heaps, hash tables, etc.

1.2. Knowledge points

  • Array
  • Stack
  • Linked list
  • queue
  • tree
  • Figure
  • heap
  • Hash table

2. Data structure classification

Data structure refers to a collection of data elements that have one or more relationships with each other and the relationship composition between data elements in the collection  .

Commonly used data structures are: array, stack, linked list, queue, tree, graph, heap, hash table, etc.

as the picture shows:

Each data structure has a unique data storage method. The structure, advantages and disadvantages of them are introduced below.

3. Overview of array structure

An array is a structure that can store multiple elements continuously in the memory, and the allocation in the memory is also continuous. The elements in the array are accessed through the array subscript, which starts from 0 .

For example, the following code is to assign 1 to the first element of the array.

int[] data = new int[100]; data[0] = 1;

advantage:

  • Quickly query elements according to the index;
  • It is convenient to traverse the array according to the index.

Disadvantages:

  • After the size of the array is fixed, it cannot be expanded;
  • The array can only store one type of data;
  • Add and delete operations are slow, because other elements need to be moved.

Applicable scene:

  • Frequent queries, little storage space requirements, rarely increase and delete situations.

4. Stack structure overview

The stack is a special linear table , which can only be operated on one end of the linear table . Operation is allowed at the top of the stack, but not allowed at the bottom of the stack .

The stack is characterized by first-in-last-out, or "last-in, first-out". The operation of putting elements from the top of the stack is called pushing into the stack, and removing elements is called out of the stack.

As shown below:

The structure of the stack is like a container. The first thing you put in, the later you can take it out. Therefore, the stack is often used in scenarios that implement recursive functions.

5. Overview of queue structure

The queue, like the stack, is also a linear list.

The queue can add elements at one end and take out elements at the other end, that is, "first in, first out". The operation of putting elements in from one end is called enqueue, and taking out elements is dequeue.

The sample picture is as follows:

Usage scenario: Because of the first-in-first-out characteristics of queues , it is very suitable for multi-threaded blocking queue management .

6. Overview of Linked List Structure

A linked list is a non-contiguous, non-sequential storage structure on a physical storage unit .

The logical sequence of data elements is realized by the pointer address of the linked list;

Each element contains two nodes, one is the data field (memory space) where the element is stored, and the other is the pointer field that points to the address of the next node;

According to the direction of the pointer, the linked list can form different structures, such as singly linked list, doubly linked list, circular linked list and so on.

Advantages of linked lists:

  • Linked list is a very commonly used data structure, no need to initialize capacity, you can add or subtract elements at will;
  • When adding or deleting elements, you only need to change the pointer fields of the two element nodes before and after to point to the address, so adding and deleting are quick.

Disadvantages:

  • Because it contains a large number of pointer fields, it takes up a lot of space;
  • Finding an element requires traversing the linked list to find it, which is very time-consuming.

Applicable scene:

  • The data volume is small and needs to be frequently increased and deleted.

7. Overview of the tree structure

Tree is a data structure that is a "n (1 n> =) " finite node composition having a set of hierarchical relationships .

It is called a "tree" because it looks like an upside-down tree, which means it has its roots facing up and its leaves facing down.

The tree has the following characteristics:

  • Each node has zero or more child nodes;
  • A node without a parent node is called the root node;
  • Every non-root node has one and only one parent node;
  • Except for the root node, each child node can be divided into multiple disjoint subtrees.

In daily applications, we discuss and use more of one of the tree structures, the binary tree , as shown in the figure below.

 

Binary tree is a special kind of tree, with the following characteristics :

  • Each node has at most two subtrees, and the maximum degree of the node is 2.
  • The left subtree and the right subtree are in order, and the order cannot be reversed.
  • Even if a node has only one subtree, the left and right subtrees must be distinguished.

Binary tree is a useful compromise .

Binary tree addition and deletion of elements are fast, and there are many algorithm optimizations in search;

Binary tree has the benefits of linked lists and arrays. It is an optimization scheme of the two and is very useful in processing large quantities of dynamic data.

8. Overview of hash table structure

A hash table, also called a hash table, is a data structure that is directly accessed based on key codes and values ​​(key and value).

Map to a position in the collection by key and value, so that the corresponding element in the collection can be found quickly.

The recorded "storage location=f(key)".

The corresponding relationship f here becomes a hash function, also known as hash (hash function);

The hash table is to convert the "Key" into an integer number through a fixed algorithm function, the so-called hash function;

This number takes the remainder of the length of the array, and the result of the remainder is used as the subscript of the array;

Store the value in the array space with the number as the subscript.

This kind of storage space can make full use of the search advantage of the array to find elements, so the search speed is very fast. The structure of the hash table is as follows:

As can be seen from the figure, the left side is obviously an array, and each member of the array includes a pointer to the head of a linked list .

Of course, this linked list may be empty or there may be many elements.

We assign the elements to different linked lists according to some characteristics of the elements.

According to these characteristics, find the correct linked list, and then find the element from the linked list.

9. Overview of heap structure

Heap is a special data structure, which can be regarded as an array object of a tree . It has the following properties:

The value of a node in the heap is always no greater than or no less than the value of its parent node; the
heap is always a complete binary tree.

The root node called the largest heap max heap or stack large root, root smallest heap heap called the minimum or small root heap. Common heaps are binary heaps and so on.

The definition of the heap is as follows:

The sequence "{k1,k2,ki,...,kn}" of "n" elements is called a heap if and only if the following relationship is satisfied.

"(ki <= k2i,ki <= k2i+1)" or "(ki >= k2i,ki >= k2i+1), (i = 1,2,3,4…n/2)" to satisfy the former The expression of becomes the small top pile, and the one that satisfies the latter expression is the big top pile.

The two structure diagrams can be arranged in a complete binary tree.

The sample picture is as follows:

10. Overview of graph structure

The graph is composed of the finite set "V" of nodes  and the set "R"  of edges .

In order to distinguish it from a tree structure, nodes are often called vertices in graph structures, and edges are ordered pairs of vertices. If there is an edge between two vertices, it means that the two vertices have an adjacent relationship.

The graph structure is as follows:

The graph is a relatively complex data structure. There are more complex and efficient algorithms for storing data, including adjacency matrix  , adjacency list , cross-linked list , adjacency multi-table , edge set array and other storage structures.

We will introduce these contents in the advanced tutorial of the picture.

11. Study Summary

After studying in this experiment, everyone has a basic understanding of the basic types of data structures, including groups, stacks, linked lists, queues, trees, graphs, heaps, etc. In addition, I have learned the characteristics and basic composition of each data structure, have a certain understanding of the theoretical introduction of data structure, and can clearly define and distinguish the different modes of data structure.

The text is not intuitive enough. We uploaded the relevant experiment video on the WeChat official account. If you don’t understand it thoroughly, you can follow our WeChat official account to watch the video.

If you want to get more content, please follow the official account of Haidata.

Share this issue here. We will continue to update the rest of the experiment. See you next time and look forward to your visit again. If you have any suggestions, such as the knowledge you want to know, the problems in the content, the materials you want, the content to be shared next time, and the problems encountered in learning, please leave a message below. Please pay attention if you like it.

Guess you like

Origin blog.csdn.net/qq_40433634/article/details/108759035