当前位置:网站首页>Binary tree -- natural search semantics (1) Basics
Binary tree -- natural search semantics (1) Basics
2022-07-27 07:16:00 【mmmenxj】
1. Tree structure can search and search efficiently .( System files , It's a tree structure . Find specific files according to folders , Time complexity is actually the depth of the number of files logN)
Tree structure in life : Library system 、 Management of large enterprises
After storing the data in a tree structure , Search or find again , The efficiency is much higher than that of linear structure .
2. Common tree structure :
BST Binary search tree -- Element search of binary tree
Balanced binary search trees -- AVL( Strict balance ), Red and black trees ( Non strict equilibrium )
notice logN Associate with tree structure
3. Trees : The subtrees can't intersect .
Except for the root node , Each node has and only has one parent
One N The tree of nodes has N-1 side
Degree of node : The number of subtrees contained in the node is called the degree of the node
The degree of a tree : The degree of the largest node in the tree
leaf node ( Terminal nodes ): Degree is 0 The node of
Tree hierarchy : Calculate from the root
Binary tree : Each node has at most two subtrees , The degree of a node in a binary tree does not exceed 2, The two sub trees are divided into left and right .
Full binary tree : The degree of all non leaf nodes is 2.
For a binary tree , The height is k, Then the binary tree has at most 2 Of k Power -1 Nodes ( The most common case is full binary tree ).
The hierarchy is k, The first k There are at most 2 Of k-1 Sub nodes .
Side length = The number of nodes n-1
Set the degree as 0 The number of nodes is n0, Degree is 1 The number of nodes of is n1, Degree is 2 The number of nodes of is n2,
be n0 = n2 +1
n0 + n1+ n2 = n
Perfect binary tree : The full binary tree is missing the lower right corner
The node numbers of the complete binary tree correspond to the full binary tree one by one . The nodes of each layer try to reach the maximum , If the node of a certain layer does not reach the maximum , The nodes of this layer are arranged on the left .
In a complete binary tree , There is no node with only right subtree and no left subtree ( Nodes should be arranged on the left ).
Binary tree storage :
It's like a list , Sequential storage and chained storage ( quote )
Sequential storage , Store the binary tree in an array ( Only complete binary trees can be stored )
Ordinary binary trees are stored by reference .
Traversal of binary tree : Before the order 、 Middle preface 、 In the following order 、 level
When writing three traversal methods , With the help of stack structure , Ensure that it is not heavy or missing .
// The first sequence traversal : Root left and right
// Pass in the root node of a binary tree . You can output the node value in the way of preorder traversal
public static void preOrder(TreeNode root){
// The boundary conditions
if(root == null){
return ;
}
System.out.println(root.val + " ");
// Recursively access the left subtree in the way of preorder traversal
preOrder(root.left);
// Access the right tree in the way of preorder traversal
preOrder(root.right);
} // In the sequence traversal : Left middle right
// Pass in the root node of a binary tree , The result set can be output in the way of middle order traversal
public static void inOrder(TreeNode root){
if(root == null){
return ;
}
inOrder(root.left);
System.out.println(root.val + " ");
inOrder(root.right);
}Sequence traversal is a non recursive iterative method , Here is the code implementation :
public class Num102_LevleOrder {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<>();
if(root == null){
return ret;
}
// The traversal process is realized with the help of queues
Deque<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
// Continue to traverse when it is not empty
// Use one temp Array to hold the elements of the current level
List<Integer> curList = new ArrayList<>();
// Take out all the elements of the current layer and add them into curList in
int size = queue.size();
for (int i = 0; i <size; i++) {
TreeNode cur = queue.poll();
curList.add(cur.val);
if(cur.left!= null){
queue.offer(cur.left);
}
if(cur.right != null){
queue.offer(cur.right);
}
}
// The current layer element has been traversed
ret.add(curList);
}
return ret;
}
}边栏推荐
- Quartus:往别人的工程添加.v文件报错
- Bert and RESNET can also be trained on mobile phones?!
- py2exe qt界面风格变成了win98解决方案
- C4D动画如何提交云渲染农场快速渲染?
- Gbase 8C core technology
- The qualities that a technical manager should have (guess)
- 基于SSM音乐网站管理系统
- 如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
- Interpretation of deepsort source code (II)
- 用typescript实现排序-递增
猜你喜欢

Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较

Vscode connection remote server development

How MySQL executes query statements

How to learn C language? This article gives you the complete answer

Quartus: an error is reported when adding a.V file to someone else's project

泛型 -- 学会它,好处多多

把Excel转换成CSV/CSV UTF-8

Pan Aimin, chairman of instruction set, attended the 2022 ecug con to speak for China's technical forces

How to implement Devops with automation tools | including low code and Devops application practice

使用反射实现动态修改@Excel的注解属性
随机推荐
基于SSM学生学籍管理系统
MySQL2
Pan Aimin, chairman of instruction set, attended the 2022 ecug con to speak for China's technical forces
Interpretation of deepsort source code (V)
强网杯2021 pwn 赛题解析——babypwn
VIVO应用市场APP上架总结
A Competitive Swarm Optimizer for Large Scale Optimization
Linear table -- stack and queue
MySQL optimization SQL related (continuous supplement)
零号培训平台课程-1、SQL注入基础
想sink 到 redis-hash 里面 把 对象的属性和值都写进去 ,大佬们有Demo 吗?
MySQL quickly compares database table data
大疆livox定制的格式CustomMsg格式转换pointcloud2
从技术原理看元宇宙的可能性:Omniverse如何“造”火星
Gbase 8C - SQL reference 6 SQL syntax (15)
String类的用法
仿真模型简单介绍
(转帖)eureka、consul、nacos的对比2
Datascience: data generation adds a small amount of noise (customizable noise) to the original data to realize the construction of new data (dataframe format data storage case)
Student status management system based on SSM