当前位置:网站首页>Preorder, inorder and postorder traversal of binary tree
Preorder, inorder and postorder traversal of binary tree
2022-07-07 12:29:00 【Xiaoding Chong duck!】
One 、 Binary tree
Binary tree (binary tree) Refers to the degree of nodes in the tree No more than 2 The ordered tree of , Its nodes are divided into left and right subtrees .
Full binary tree : If a binary tree has only a degree of 0 The node and degree of are 2 The node of , And the degree is 0 The nodes of are on the same layer , Then this binary tree is a full binary tree .
Perfect binary tree : Leaf nodes can only appear in the lowest and lower layers , And the lowest leaf nodes are concentrated on the left of the tree .
Two 、 The generation of binary tree
function NodeTree (value) {
this.value = value;
this.left = null;
this.right = null;
}
let root = new NodeTree('A');
let t1 = new NodeTree('B');
let t2 = new NodeTree('C');
let t3 = new NodeTree('D');
let t4 = new NodeTree('E');
let t5 = new NodeTree('F');
root.left = t1;
root.right = t2;
t1.left = t3;
t1.right = t4;
t2.left = t5;
console.log(root);The generated binary tree is as follows :


3、 ... and 、 Traversal of binary tree
1、 The former sequence traversal
Preorder traversal is to traverse the root node first , Then traverse the left subtree , Finally, traverse the right subtree , namely root —> Left —> Right
The code is as follows :
/**
* The former sequence traversal
* @param {NodeTree} root
*/
let formerSequenceTraversal = function (root) {
if (!root || root.value === null) {
return null;
}
console.log(root.value);
formerSequenceTraversal(root.left);
formerSequenceTraversal(root.right);
}The output is as follows :
A、B、D、E、C、F
2、 In the sequence traversal
Middle order traversal is to traverse the left subtree first , Then traverse the root node , Finally, traverse the right subtree , namely Left —> root —> Right
The code is as follows :
/**
* In the sequence traversal
* @param {NodeTree} root
*/
let middleSequenceTraversal = function (root) {
if (!root || root.value === null) {
return null;
}
middleSequenceTraversal(root.left);
console.log(root.value);
middleSequenceTraversal(root.right);
}The output is as follows :
D、B、E、A、F、C
3、 After the sequence traversal
Post order traversal is to traverse the left subtree first , Traversing right subtree again , Finally, traverse the root node , namely Left —> Right —> root
The code is as follows :
/**
* After the sequence traversal
* @param {NodeTree} root
*/
let afterSequenceTraversal = function (root) {
if (!root || root.value === null) {
return null;
}
afterSequenceTraversal(root.left);
afterSequenceTraversal(root.right);
console.log(root.value);
}The output is as follows :
D、E、B、F、C、A
边栏推荐
- 【统计学习方法】学习笔记——支持向量机(下)
- SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
- VSCode的学习使用
- Introduction and application of smoothstep in unity: optimization of dissolution effect
- Several methods of checking JS to judge empty objects
- 金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
- Attack and defense world ----- summary of web knowledge points
- Completion report of communication software development and Application
- 全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
- Is it safe to open an account in Ping An Securities mobile bank?
猜你喜欢

Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool

Unity中SmoothStep介绍和应用: 溶解特效优化

Processing strategy of message queue message loss and repeated message sending

SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
![Routing strategy of multi-point republication [Huawei]](/img/5c/2e3b739ce7199f0d2a4ddd7c3856fc.jpg)
Routing strategy of multi-point republication [Huawei]

【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器

"Series after reading" my God! It's so simple to understand throttling and anti shake~

30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记

《通信软件开发与应用》课程结业报告

【统计学习方法】学习笔记——支持向量机(下)
随机推荐
30. Feed shot named entity recognition with self describing networks reading notes
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
Unity中SmoothStep介绍和应用: 溶解特效优化
EPP+DIS学习之路(2)——Blink!闪烁!
Configure an encrypted web server
密码学系列之:在线证书状态协议OCSP详解
Vxlan 静态集中网关
Minimalist movie website
(to be deleted later) yyds, paid academic resources, please keep a low profile!
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
Epp+dis learning path (1) -- Hello world!
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
Customize the web service configuration file
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
Introduction to three methods of anti red domain name generation
Epp+dis learning road (2) -- blink! twinkle!
SQL blind injection (WEB penetration)
Review and arrangement of HCIA