当前位置:网站首页>Double non undergraduate students enter the factory, while I am still quietly climbing trees at the bottom (Part 1)
Double non undergraduate students enter the factory, while I am still quietly climbing trees at the bottom (Part 1)
2022-07-02 09:12:00 【Qigui】
Individuality signature : The most important part of the whole building is the foundation , The foundation is unstable , The earth trembled and the mountains swayed . And to learn technology, we should lay a solid foundation , Pay attention to me , Take you to firm the foundation of the neighborhood of each plate .
Blog home page : Qigui's blog
special column : data structure (C Language version )
From the south to the North , Don't miss it , Miss this article ,“ wonderful ” May miss you la
Code , There are notes !!!
Triple attack( Three strikes in a row ):Comment,Like and Collect—>Attention
List of articles
Three elements of data structure : Logical structure 、 The operation of data 、 Storage structure ( Physical structure ). The storage structure is different , The operation is implemented in different ways .
1、 Trees
1.1、 Basic concepts
Trees are n ( n ≥ 0 ) n(n\geq 0) n(n≥0) A finite set of nodes ,n=0 The number of instant settlement points is 0, It's called the empty tree .
Any non empty tree should meet :
- There is and only one specific node called root
- When n>1 when , The rest of the nodes can be divided into m(m>0) A finite set of disjoint , Each of these sets is itself a tree , And called the subtree of the root node .
The characteristics of non empty trees :
- There is only one root node
- Nodes without successors are called leaf nodes
- A node with a successor is called a branch node
- Except for the root node , Any node has only one precursor
- Each node can have 0 One or more successors
Logical representation of tree :
- Tree representation
- Venn diagram representation
- Indentation notation
- Bracket notation
1.2、 Basic terminology
1.2.1、 Description of the relationship between nodes
- Ancestral node
- Children's node
- Parents node ( Parent node )
- Child node
- Brother nodes
- Cousin node
- route : The path between two nodes can only be from top to bottom
- The length of the path : Pass by several sides
1.2.2、 Attribute description of nodes and trees
attribute :
- The hierarchy of nodes ( depth )—— Count from top to bottom ( The default from the 1 Start )
- The height of the node —— Count from the bottom up
- The height of the tree ( depth )—— How many floors in total
- The degree of node —— There are several children ( Number of branches ) notes : Degree of non leaf node >0, Degree of leaf node =0
- The degree of a tree : The maximum value of the degree of each node
1.2.3、 Ordered trees 、 Disordered trees
Logically , Whether the sub trees are in order , Whether the positions are interchangeable
Judge : It depends on what is stored in the tree , Whether it is necessary to use the left and right positions of nodes to reflect some logical relations
- Ordered trees : Logically , The subtrees of the nodes in the tree are ordered from left to right , Not interchangeable
- Disordered trees : Logically , The subtrees of the nodes in the tree are unordered from left to right , interchangeable
1.2.4、 The forest
The forest is m ( m ≥ 0 ) m(m\geq 0) m(m≥0) A collection of disjoint trees .
1.3、 Properties of trees
1、 Number of nodes = Total degree +1
The degree of node —— How many children do you have ( Number of branches )2、 Degree is m Number of numbers 、m The difference between fork trees
- Degree is m The tree of
- At least one node degree =m
- It must be a non empty tree
- m Fork tree
- The degree of all nodes is allowed to be <m
- It could be an empty tree
- Degree is m The tree of
3、 Degree is m The tree of i Layer up to have a m i − 1 m^{i-1} mi−1 Nodes ( i ≥ 1 i\geq 1 i≥1)
m Fork tree —— The first i Layer up to have a m i − 1 m^{i-1} mi−1 individual junction spot ( i ≥ 1 Nodes (i\geq 1 individual junction spot (i≥1)4、 The height is h Of m A fork tree has at most m h − 1 m − 1 \frac{m^{h}-1}{m-1} m−1mh−1 Nodes
5、 The height is h Of m Fork trees have at least m*h-1 Nodes
The height is h、 Degree is m The trees of at least h+m-1 Nodes6、 have n Of nodes m The minimum height of the fork tree is ⌈ l o g m ( n ( m − 1 ) + 1 ) ⌉ \left \lceil log_{m}(n(m-1)+1) \right \rceil ⌈logm(n(m−1)+1)⌉
2、 The storage structure of the tree
2.1、 Parenting ( Sequential storage )
Store each node in sequence , Each node stores the information pointing to the parents “ The pointer ”.
- advantage : It is convenient to check the parents of the specified node
- shortcoming : The child who checks the specified node can only traverse from the beginning
- Empty data leads to slower traversal
#define MaxSize 100
typedef struct
{
ElemType data; // Store the value of the node
int parent; // Where to store your parents
}PTree[MaxSize]; //PTree Storage structure type for both parents
2.2、 Child representation ( The order + Chain store )
Store each node in sequence , Save the child chain header pointer in each node
- advantage : It's convenient to find children , It is convenient for computers to specify the degree of nodes
- shortcoming : It is inconvenient to find the parent node
MaxSons Number of nodes for the most children , Or the degree of the tree .
typedef struct
{
ElemType data; // The value of the node
struct node *sons[MaxSons]; // Point to the child node
}TSonNode; // Node type in child chain storage structure
2.3、 Child brother notation ( Chain store )
Store the tree with a binary linked list —— Left child right brother
Design for each node 3 Domains :
- A data element field
- The first child node to the left of the node ( eldest son ) A pointer to the domain
- A pointer field that points to the next sibling of the node
The storage structure of child brother chain is actually the storage structure that converts the tree into a binary tree
typedef struct tnode
{
ElemType data; // The value of the node
struct tnode *hp; // Point to brother
struct tnode *vp; // Point to the child node
}TSBNode; // The node type of child brother chain storage structure
2.4、 Trees 、 The conversion of forest and binary tree
The essence : Use a binary linked list to store the forest —— Left child right brother . The root nodes of each tree in the forest are regarded as brothers
2.4.1、 The forest 、 The tree turns into a binary tree
- Add a line between all the neighboring brothers in the tree
- For each node in the tree, only the connection between it and the eldest child is reserved , Delete the connection with other children
- Take the root node of the tree as the axis , Turn the whole tree clockwise 45°, Make it structured
2.4.2、 A binary tree is reduced to a forest 、 Trees
- If a node is the left child of its parents , Then the right child of the node 、 The right child of the right child is connected with the parent node of the node by connecting
- Delete the connection between all parent nodes and right child nodes in the original binary tree
- Sort out the tree obtained from the previous two steps , That is, take the root node as the axis , Turn it counter clockwise 45°, Make it structured
3、 Trees 、 The traversal of the forest
3.1、 Tree traversal
The traversal operation of the tree refers to accessing all nodes in the tree in a certain way, and each node is accessed only once . Tree traversal mainly includes root traversal 、 Post root traversal and hierarchical traversal . Be careful : The first root traversal and the second root traversal of the tree are recursive .
3.1.1、 First root through
If the tree is not empty , So let's go to the root , Then traverse the roots of each subtree in turn
3.1.2、 Back root traversal ( Depth-first traversal )
If the tree is not empty , First, traverse the root of each subtree in turn , Finally, access the root node . The back root traversal sequence of the tree is the same as the middle sequence of the corresponding binary tree of the tree
3.1.3、 Level traversal ( Breadth first traversal )
1. If the tree is not empty , Then the root node joins the queue ;2. If the queue is not empty , The team leader element goes out of the team and visits , At the same time, join the children of this element in turn ;3. repeat 2 Step until the queue is empty .( Implement... With queues )
3.2、 The traversal of the forest
3.2.1、 The first sequence traversal
If the forest is not empty , Then traverse according to the following rules :
Visit the root of the first tree in the forest ; Traversing the subtree forest of the root node of the first tree ; First, we traverse the forest made up of the remaining trees after the first tree is removed .
3.2.2、 In the sequence traversal
If the forest is not empty , Then traverse according to the following rules :
Traversing the subtree forest of the root node of the first tree in the forest ; Visit the root of the first tree ; The middle order traverses the forest made up of the remaining trees after the first tree is removed .
边栏推荐
- 长篇总结(代码有注释)数构(C语言)——第四章、串(上)
- 西瓜书--第六章.支持向量机(SVM)
- 十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
- 微服务实战|微服务网关Zuul入门与实战
- 微服务实战|Eureka注册中心及集群搭建
- Connect function and disconnect function of QT
- 【Go实战基础】gin 如何验证请求参数
- Leetcode sword finger offer brush questions - day 23
- 数构(C语言)——第四章、矩阵的压缩存储(下)
- win10使用docker拉取redis镜像报错read-only file system: unknown
猜你喜欢
Redis安装部署(Windows/Linux)
Micro service practice | introduction and practice of zuul, a micro service gateway
「Redis源码系列」关于源码阅读的学习与思考
win10使用docker拉取redis镜像报错read-only file system: unknown
【Go实战基础】gin 如何自定义和使用一个中间件
Matplotlib剑客行——布局指南与多图实现(更新)
Matplotlib剑客行——没有工具用代码也能画图的造型师
Matplotlib剑客行——容纳百川的艺术家教程
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
The channel cannot be viewed when the queue manager is running
随机推荐
聊聊消息队列高性能的秘密——零拷贝技术
Leetcode sword finger offer brush questions - day 22
数构(C语言)——第四章、矩阵的压缩存储(下)
Oracle 相关统计
查看was发布的应用程序的端口
微服务实战|Eureka注册中心及集群搭建
Webflux responsive programming
知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
Watermelon book -- Chapter 5 neural network
QT -- how to set shadow effect in QWidget
[go practical basis] how to verify request parameters in gin
NPOI 导出Word 字号对应
2022/2/14 summary
MYSQL安装出现问题(The service already exists)
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
Redis zadd导致的一次线上问题排查和处理
「面试高频题」难度大 1.5/5,经典「前缀和 + 二分」运用题
oracle修改数据库字符集
使用递归函数求解字符串的逆置问题