当前位置:网站首页>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
边栏推荐
- 30. Feed shot named entity recognition with self describing networks reading notes
- Static comprehensive experiment
- SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
- In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
- 2022 年第八届“认证杯”中国高校风险管理与控制能力挑战赛
- DOM parsing XML error: content is not allowed in Prolog
- (待会删)yyds,付费搞来的学术资源,请低调使用!
- Epp+dis learning road (2) -- blink! twinkle!
- 让数字管理好库存
- Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
猜你喜欢

数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
![[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer](/img/5f/75549fc328d7ac51f8b97eef2c059d.png)
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer

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

College entrance examination composition, high-frequency mention of science and Technology

What is an esp/msr partition and how to create an esp/msr partition

Processing strategy of message queue message loss and repeated message sending

AirServer自动接收多画面投屏或者跨设备投屏

wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6

Upgrade from a tool to a solution, and the new site with praise points to new value

解密GD32 MCU产品家族,开发板该怎么选?
随机推荐
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
The road to success in R & D efficiency of 1000 person Internet companies
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
Utiliser la pile pour convertir le binaire en décimal
EPP+DIS学习之路(1)——Hello world!
<No. 8> 1816. Truncate sentences (simple)
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
What is a LAN domain name? How to parse?
(to be deleted later) yyds, paid academic resources, please keep a low profile!
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Tutorial on the principle and application of database system (011) -- relational database
Static routing assignment of network reachable and telent connections
Inverted index of ES underlying principle
Idea 2021 Chinese garbled code
PowerShell cs-utf-16le code goes online
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
sql-lab (54-65)
浅谈估值模型 (二): PE指标II——PE Band
(待会删)yyds,付费搞来的学术资源,请低调使用!