当前位置:网站首页>Summary of common algorithms of binary tree
Summary of common algorithms of binary tree
2020-11-06 01:18:00 【Clamhub's blog】
Node definition
1 |
public static class BinaryNode<T> { |
Binary tree nodes include element values and left and right child node references .
Create nodes
1 |
BinaryNode g = new BinaryNode(7); |
Form like :
1、 front 、 in 、 After the sequence traversal
In front of the binary tree 、 in 、 Before in postorder traversal 、 in 、 The root node ;
Before the order : Output the root node first , And then the left and right nodes .
Middle preface : First left , Then output the root node , To the right .
In the following order : First left and right , Then output the root node .
1 |
public static void visit(BinaryNode p) { |
1.1、 The former sequence traversal
1 |
root -> Left -> Right |
1.1.1、 Recursive implementation
1 |
public static void recursivePreOrder(BinaryNode p) { |
If the node is null, Go straight back to ;
Print out the root node first , Then recursion left subtree , Then recurs the right subtree .
Just meet : First root , And then around .
1.1.2、 Non recursive implementation
1 |
public static void iterativePreOrder(BinaryNode p) { |
Make full use of the idea of stack , First in, then out .
When the node is not empty , Putting nodes on the stack , Iterate the elements in the stack , Pop up top element , Currently the root element , After that, press the right and left child nodes on the stack respectively . In this way, the order of the child nodes is left first and then right .
1.2、 In the sequence traversal
1 |
Left -> root -> Right |
1.2.1、 Recursive implementation
1 |
public static void recursiveInOrder(BinaryNode p) { |
1.2.2、 Non recursive implementation
1 |
public static void iterativeInOrder(BinaryNode p) { |
1.3、 After the sequence traversal
1 |
Left -> Right -> root |
1.3.1、 Recursive implementation
1 |
public static void recursivePostOrder(BinaryNode p) { |
1.3.2、 Non recursive implementation
1 |
public static void iterativePostOrder(BinaryNode p) { |
2、BFS And DFS
2.1、BFS Breadth first search
1 |
1234567 |
Breadth first traversal is to read node elements by layer , We need to use the data structure of the queue .
1 |
public static void levelOrderTraversal(BinaryNode node) { |
2.2、DFS Depth-first search
1 |
1245367 |
Starting from the root node , Select a branch to read all the elements , We need to use the data structure of the stack .
1 |
public static void depthTraversal(BinaryNode node) { |
3、 The depth of the binary tree
104. The maximum depth of a binary tree
3.1、 recursive
1 |
private static int calcDepth(BinaryNode node) { |
3.2、 Depth first
maxDepth Compare the length of each branch to update , Finally get the deepest branch length .
1 |
public static int maxDepthDFS(BinaryNode node) { |
3.3、 breadth-first
Search by layer , Every step into the floor , depth +1.
1 |
public static int maxDepthBFS(BinaryNode node) { |
4、 Binary tree image
Traverse by depth or breadth , Swap the left and right subtrees of nodes .
1 |
public static void mirror(BinaryNode root) { |
5、 Symmetric binary tree
101. Symmetric binary tree
This problem is a variation of binary tree mirror image , If two binary trees are symmetric , be :
- Two root nodes have the same value
- The left subtree of each tree , Are the same as the right subtree of another tree
Recursive implementation :1
2
3
4
5
6
7
8
9
10
11public boolean isSymmetric(BinaryNode root) {
return isMirror(root, root);
}
public boolean isMirror(BinaryNode t1, BinaryNode t2) {
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null) return false;
return (t1.val == t2.val)
&& isMirror(t1.right, t2.left)
&& isMirror(t1.left, t2.right);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public boolean isSymmetric(BinaryNode root) {
LinkedList<BinaryNode> q = new LinkedList<>();
q.add(root);
q.add(root);
while (!q.isEmpty()) {
BinaryNode t1 = q.poll();
BinaryNode t2 = q.poll();
if (t1 == null && t2 == null) continue;
if (t1 == null || t2 == null) return false;
if (t1.val != t2.val) return false;
q.add(t1.left);
q.add(t2.right);
q.add(t1.right);
q.add(t2.left);
}
return true;
}
summary
The algorithm of various topics of binary tree will think of recursion in the first consciousness , But when the recursion depth is too large, there will be stack overflow , So iterations will be used to implement accordingly , Accordingly, the data structure of queue or stack is introduced .
It feels like the most important algorithm is DFS And BFS, among DFS Depth first search uses the FIFO feature of the stack , and BFS Breadth first search uses the first in first out feature of the queue .
版权声明
本文为[Clamhub's blog]所创,转载请带上原文链接,感谢
边栏推荐
- 数字城市响应相关国家政策大力发展数字孪生平台的建设
- [event center azure event hub] interpretation of error information found in event hub logs
- 中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律
- IPFS/Filecoin合法性:保护个人隐私不被泄露
- How to select the evaluation index of classification model
- 全球疫情加速互联网企业转型,区块链会是解药吗?
- 小程序入门到精通(二):了解小程序开发4个重要文件
- 有关PDF417条码码制的结构介绍
- 怎么理解Python迭代器与生成器?
- Subordination judgment in structured data
猜你喜欢
Network programming NiO: Bio and NiO
你的财务报告该换个高级的套路了——财务分析驾驶舱
制造和新的自动化技术是什么?
从海外进军中国,Rancher要执容器云市场牛耳 | 爱分析调研
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律
Computer TCP / IP interview 10 even asked, how many can you withstand?
Using Es5 to realize the class of ES6
数据产品不就是报表吗?大错特错!这分类里有大学问
Swagger 3.0 天天刷屏,真的香嗎?
随机推荐
向北京集结!OpenI/O 2020启智开发者大会进入倒计时
How to get started with new HTML5 (2)
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
Calculation script for time series data
How to demote a domain controller in Windows Server 2012 and later
C language 100 question set 004 - statistics of the number of people of all ages
xmppmini 專案詳解:一步一步從原理跟我學實用 xmpp 技術開發 4.字串解碼祕笈與訊息包
带你学习ES5中新增的方法
在大规模 Kubernetes 集群上实现高 SLO 的方法
Don't go! Here is a note: picture and text to explain AQS, let's have a look at the source code of AQS (long text)
Every day we say we need to do performance optimization. What are we optimizing?
10 easy to use automated testing tools
Process analysis of Python authentication mechanism based on JWT
如何玩转sortablejs-vuedraggable实现表单嵌套拖拽功能
Troubleshooting and summary of JVM Metaspace memory overflow
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Microservices: how to solve the problem of link tracing
数字城市响应相关国家政策大力发展数字孪生平台的建设
This article will introduce you to jest unit test
做外包真的很难,身为外包的我也无奈叹息。