The soul of the program - data structure

1. Data structure


program = data structure + algorithm

1.1. What is a data structure?

A data structure is the underlying way a computer stores and organizes data . Refers to how the data are arranged with each other

The data structure is for more convenient management and use of data, which needs to be selected in combination with specific business scenarios

Often, well-chosen data structures can lead to higher operational or storage efficiency

1.2. Common data structures

  • the stack
  • queue
  • array
  • linked list
  • binary tree
  • binary search tree
  • balanced binary tree
  • red black tree

the stack

The execution characteristics of the stack data structure: last in first out, first in last out (similar to magazines)

  • The process of data entering the stack model is called: push/push
  • The process of data leaving the stack model is called: pop/pop

Opening only at the top (cup)

  • Open at one end (stack top)
  • Closed at one end (bottom of the stack)

queue

The execution characteristics of the queue data structure: first in first out, last in last out (similar to queuing to buy things)

insert image description here

  • The process of data entering the queue model from the back end is called: enqueuing
  • The process of data leaving the queue model from the front end is called: dequeue

Both ends are open (hand warmer)

  • Open at one end (rear end)
  • Open at one end (front)

array

Array is a continuous area of ​​memory, fast query, slow addition and deletion model

  • Fast query speed: Query data is located by address value and index, and it takes the same time to query any data. (elements are stored contiguously in memory)
  • Low deletion efficiency: To delete the original data, each subsequent data needs to be moved forward
  • The addition efficiency is extremely low: each data after adding the position also needs to be moved backwards, and then add elements

linked list

The nodes in the linked list are independent objects, which are stored discontinuously in memory . Each node contains the data value and the address value of the next node.

  • Linked list queries are slow . No matter which data is queried, it must be found from the beginning
  • Linked list additions and deletions are relatively fast ((compared to arrays)

Types of Linked Lists

  • singly linked list
  • Doubly linked list

insert image description here

binary tree

Binary tree: There is always only one root node, and each node has no more than 2 child nodes.

Tree

insert image description here

  • Degree: the number of child nodes of each node
  • In a binary tree, the degree of any node is <= 2
  • Tree height: the total number of layers of the tree
  • root node: the topmost node
  • Brother nodes: Nodes with a common parent node are called sibling nodes
  • left child node: the node at the bottom left
  • Right child node: the node at the bottom right

binary search tree

Binary search tree, also known as binary sorting tree or binary search tree, is small on the left and large on the right , but the tree may be very high and the query performance will deteriorate

insert image description here

Features:

  • Each node has at most two child nodes
  • The value on the left subtree of any node is smaller than the current node
  • The value on the right subtree of any node is greater than the current node

rule:

  • The small ones are stored on the left, the large ones are stored on the right, and the same ones are not stored

Purpose: To improve the performance of retrieving data

Disadvantages: There is a lame phenomenon, which causes the performance of the query to be the same as that of a single-linked list, and the query speed becomes slower!

balanced binary tree

The balanced binary tree is to make the tree as short as possible under the size rule of finding the binary tree, so as to improve the performance of querying data

Rule: The height difference between the left and right subtrees of any node does not exceed 1 , and the left and right subtrees of any node are a balanced binary tree

Balanced binary tree can lead to unbalance after adding elements

The basic strategy is to turn left, or turn right to ensure balance

Balanced binary tree - four cases of rotation

  • left left
  • about
  • right right
  • right left

red black tree

A red-black tree is a self-balancing binary search tree , a data structure used in computer science

It appeared in 1972 and was called a balanced binary B tree at that time. In 1978, it was modified into today's "red-black tree"

Each node can be red or black; the red-black tree is not balanced by height , its balance is achieved by the "red-black rule"

Red-black tree (that is, a sorted binary tree that realizes self-balancing based on red-black rules)

red and black rules

  • Each node is either red or black, the root node must be black
  • If a node is red, its child nodes must be black (two red nodes cannot be connected)
  • For each node, the simple path from the node to all its descendant leaf nodes contains the same number of black nodes

insert image description here

Guess you like

Origin blog.csdn.net/zhang_0202/article/details/130655038