Typical data structures - stack/queue/linked list, hash search, binary tree (BT), clue binary tree, binary sort tree (BST tree), balanced binary tree (AVL tree), red-black tree (RB tree)

Table of contents

List of typical data structures

stack/queue/linked list

Tree

Binary tree

clue binary tree

Binary sorting tree

Balanced binary tree (AVL tree)

red black tree

Introduction to other tree species and applications


List of typical data structures

stack/queue/linked list

Description omitted.

Some basic simple implementations /数据结构简单实现/are in the reference folder.

Tree

The following are the basic concepts of trees (definition, basic operations, properties, storage structure, etc.), binary trees (definition, basic operations, storage, traversal, etc.), balanced binary trees, red-black trees, etc.

Quoted from: Basic concepts of trees and binary trees_Qingpingzhimo's blog-CSDN blog .

A tree is composed of one or more nodes. There is a special node called the root, which is a finite set of n (n>=0) nodes. When n=0, it is called an empty tree. When n>0, the elements of the finite set form a hierarchical data structure.

assets/70.jpeg

Some concepts of trees

  • Node degree: the number of subtrees a node has. For example, the degree of A is 2, the degree of B is 1, and the degree of C is 3.
  • Tree height: also called tree depth, the maximum level of nodes in the tree.
  • Ordered tree: The order between the subtrees of nodes in the tree is important, and positions cannot be exchanged at will.
  • Unordered tree: The order among the subtrees of the tree node is not important. You can swap positions at will.
  • Forest: A collection of 0 or more disjoint trees.

Quoted from: "Data Structure Tutorial".

Some properties of trees

  1. The total number of nodes in a non-empty tree is equal to the sum of the degrees of all nodes in the tree plus 1.
  2. The i-th level of a non-empty tree with degree k has at most k^(i-1) nodes (i ≥ 1).
  3. A k-ary tree with depth h has at most (k^h - 1)/(k - 1) nodes.
  4. The minimum depth of a k-ary tree with n nodes is log_k(n*(k - 1)) + 1. The height of a binary tree containing n nodes is at least log_2(n) + 1.

Basic tree operations

  1. Create an empty tree T.
  2. Find the root node of the tree where node x is located. Or find the root node of tree T.
  3. Find the parent nodes of node x in tree T.
  4. Find the ith child node of node x in tree T.
  5. Find the sibling nodes on the right side of node x in tree T.
  6. Insert the tree with S as the root node into the i-th child node position of node x of tree T.
  7. Delete the i-th tree of node x in tree T.
  8. Traverse a tree, traverse all the nodes of the tree in a certain order and obtain a sequence composed of all nodes.

tree storage

Chain storage is mostly used. In addition to storing the data information of the node itself, the connection relationship between the nodes in the tree must also be reflected in the storage structure.

  • Multi-linked list representation: divided into fixed-length link number and variable-length link number.

    former:

    1

    the latter:

    2

  • Triple linked list representation:

    3

Binary tree

Basic concepts of trees and binary trees_Qingpingzhimo's blog-CSDN blog .

Quoted from: "Data Structure Tutorial".

Binary tree structures are widely used to solve various practical problems in the computer field. For example, binary trees provide strong and effective support in many aspects such as sorting, retrieval, database management systems, and artificial intelligence.

Each node has at most two subtrees. In a binary tree, the left and right subtrees of a node are strictly distinguished, and their order cannot be reversed at will. Therefore a binary tree is an ordered tree.

Binary trees can be divided into full binary trees and complete binary trees.

Basic operations of binary trees

4

Binary tree storage structure

  • Sequential storage structure: There are some inherent defects in the sequential storage structure, which makes the insertion and deletion of binary trees inconvenient and relatively low-efficiency (the inherent shortcomings of linear tables).

    5

  • Chain storage structure: more suitable and wider. Two types: binary linked list structure and three-pronged linked list structure.

    Binary linked list structure: Each link point in the linked list consists of three fields, namely a data field and two pointer fields, the latter respectively giving the storage addresses of the left and right nodes of the node.

    6

    Trident linked list structure: Compared with the binary linked list structure, an additional pointer field is added to point to the parent node, so that when searching for the parent node of a node in the binary tree, there is no need to traverse the entire binary tree. It is space for time (such as search time, etc.).

Binary trees and tree traversal

Many operations on binary trees are almost based on binary tree traversal. Binary tree is a non-linear structure, so it is necessary to find a rule so that all nodes in the binary tree can be arranged in a linear sequence, which is called traversal.

If the symbols D, L, and R are used to represent the three processes of accessing the root node, traversing the left subtree of the root node, and traversing the right subtree of the root node respectively, and limiting the order of first left and then right, then three traversals are usually used. Methods: DLR, LDR, and LRD, which are called pre-order traversal, in-order traversal, and subsequent traversal respectively. There is also a hierarchical traversal order.

Traversal can be done recursively (stack overflow is easy for large trees). Non-recursive methods usually utilize a stack structure.

The following is an example of a program that is traversed in inorder traversal order.

7

Traversal by level (or breadth-first traversal). That is, if the binary tree being traversed is not empty, the first level, second level... until the last level of the binary tree is visited in sequence. The access to each level is from left to right. Proceed in order. This method is usually implemented using a queue. The following is an example program.

8

Restore binary tree from traversal sequence

Three steps:

assets/9.jpg

clue binary tree

A binary tree in which clues are added to the nodes of the binary tree is called a clued binary tree. For a binary tree with n nodes, there are n+1 empty link fields in the binary chain storage structure. These empty link fields are used to store the pointers of the node's predecessor node and successor node in a certain traversal order. These pointers are called clues, and the binary tree plus clues is called a clue binary tree.

Binary sorting tree

Quoted from: "Data Structure Tutorial".

Binary sorting tree is used for sorting, search/retrieval, which can greatly improve the time efficiency of search (in general, the query efficiency is higher than the linked list structure). Binary sorting tree is also called binary search tree. Some people say that when the functions that need to be completed are insertion, deletion and retrieval, the binary sort tree has better performance than any data structure studied so far.

Quoted from: Binary sorting tree (binary search tree) and C language implementation (biancheng.net) .

A binary sorting tree is either an empty binary tree or has the following characteristics:

  • In a binary sorted tree, if its root node has a left subtree, then the values ​​of all nodes on the left subtree are less than the value of the root node;
  • In a binary sorted tree, if its root node has a right subtree, then the values ​​of all nodes on the right subtree are greater than the value of the root node;
  • The left and right subtrees of a binary sorting tree are also required to be binary sorting trees;

As shown in the figure below, it is a binary sorting tree.

assets/103FJ439-0.png

Quoted from: "Data Structure Tutorial".

Inserting data into a binary sort tree also needs to follow the principles of a binary sort tree. Every time a new element is inserted into the binary sorted tree, the node corresponding to the element is inserted at the leaf node position, and other nodes in the binary tree are not moved during the insertion process. A sequence of data elements is not necessarily arranged according to the size of the value, but after it is constructed into a binary sorting tree, the sequence obtained by performing in-order traversal of the binary sorting tree is a sequence arranged according to the size of the value.

Balanced binary tree (AVL tree)

Quoted from: Balanced binary tree (AVL tree) and C language implementation (biancheng.net) .

Balanced binary tree, also known as AVL tree. In fact, it is a binary tree that follows the following two characteristics:

  • The depth difference between the left subtree and the right subtree in each subtree cannot exceed 1;
  • Each subtree in a binary tree is required to be a balanced binary tree;

In fact, based on the binary tree, if each subtree in the tree satisfies that the depth difference between its left subtree and right subtree does not exceed 1, then this binary tree is a balanced binary tree.

The difference between the depth of the left subtree and the depth of the right subtree of each node in the binary tree is defined as the balance factor of the node. Therefore, the balance factor of each node in the balanced binary tree can only be 1, 0 or -1.

Quoted from: "Data Structure Tutorial".

The shape of the binary sorting tree is unpredictable in advance and is very arbitrary. What is often obtained is a very unbalanced binary tree. The greater the depth difference, the longer the calculation time, and the advantage is lost. In order to overcome this defect of the binary sorting tree, it is necessary to make necessary adjustments to the morphological structure of the binary tree while inserting and deleting nodes, so that the binary sorting tree is always in a balanced state.

It has been theoretically proven that the depth of a balanced tree with n nodes will in no case be more than 45% higher than the depth of an ideal balanced tree with the same number of nodes. Therefore, although the search operation on the rebalanced tree is slower than the ideal balanced tree, it is usually much faster than the search operation on the arbitrarily generated binary sorting tree, and its time complexity is still of the order of O(Log_2(n)) .

red black tree

Quoted from  Red-Black Tree (RB-Tree)_Qingpingzhimo’s blog-CSDN blog .

A red-black tree is a binary search tree.

Comparison of red-black trees and AVL trees

  • If inserting a node causes an imbalance in the tree, both AVL and RB-Tree only require at most 2 rotation operations, that is, both are O(1); but when deleting a node causes an imbalance in the tree, the worst case scenario AVL needs to maintain the balance of all nodes on the path from the deleted node to the root node, so the magnitude of rotation required is O(logN), while RB-Tree only needs 3 rotations at most, which only requires O(1) the complexity;
  • However, because the red-black tree is not as highly balanced as the AVL tree, the search performance of the red-black tree is worse than that of the AVL tree. This poor performance in search is worth the rotation performance when inserting and deleting data. This This is why red-black trees are used in many situations instead of AVL trees, such as map and set in STL. Therefore, RB-Tree is more efficient in scenarios that require a large number of nodes to be inserted and deleted . Naturally, since AVL is highly balanced, AVL's search efficiency is higher .
Introduction to other tree species and applications

Guess you like

Origin blog.csdn.net/Staokgo/article/details/132922474